<template>
    <div v-if="currentFile" class="select-none items-center px-3.5 py-4 lg:grid lg:grid-cols-3 lg:py-3">
        <div class="flex items-center justify-between lg:w-auto lg:justify-start">
            <!--Close icon-->
            <div @click="closeFullPreview" class="order-last -m-3 cursor-pointer p-3 lg:order-none">
                <x-icon size="16" class="vue-feather" />
            </div>

            <!--Item name-->
            <div class="flex items-center">
                <div class="mr-3 ml-0 flex items-center lg:mx-3">
                    <span
                        class="inline-block max-w-[230px] overflow-hidden text-ellipsis whitespace-nowrap text-sm font-bold"
                    >
                        {{ currentFile.data.attributes.name }}
                    </span>

                    <span v-if="!fastPreview" class="ml-1 text-sm font-bold">
                        ({{ showingImageIndex + ' ' + $t('pronouns.of') + ' ' + files.length }})
                    </span>
                </div>

                <!--Context menu handler-->
                <PopoverWrapper>
                    <!--Icon-->
                    <span @click.stop="showItemContextMenu" class="-m-3 p-3">
                        <div
                            class="inline-block rounded-md bg-light-background py-0.5 px-1.5 align-middle transition-all duration-200 dark:bg-dark-foreground lg:bg-transparent"
                        >
                            <more-horizontal-icon size="14" />
                        </div>
                    </span>

                    <!--Desktop context menu-->
                    <PopoverItem name="file-preview-contextmenu" side="right">
                        <OptionGroup>
                            <Option
                                @click.native="$renameFileOrFolder(currentFile)"
                                :title="$t('context_menu.rename')"
                                icon="rename"
                            />
                            <Option
                                @click.native="$moveFileOrFolder(currentFile)"
                                :title="$t('context_menu.move')"
                                icon="move-item"
                            />
                            <Option
                                @click.native="$shareFileOrFolder(currentFile)"
                                :title="sharingTitle"
                                icon="share"
                                v-if="!$isThisRoute(['Public', 'RequestUpload', 'SharedWithMe'])"
                            />
                            <Option
                                @click.native="$deleteFileOrFolder(currentFile)"
                                :title="$t('context_menu.delete')"
                                icon="trash"
                                class="menu-option"
                            />
                        </OptionGroup>
                        <OptionGroup v-if="!$isThisRoute(['RequestUpload'])">
                            <Option @click.native="downloadItem" :title="$t('context_menu.download')" icon="download" />
                        </OptionGroup>
                    </PopoverItem>
                </PopoverWrapper>
            </div>
        </div>

        <!--Item info-->
        <small class="hidden text-center text-tiny font-normal text-gray-600 lg:block">
            {{ currentFile.data.attributes.filesize }}, {{ currentFile.data.attributes.created_at }}
        </small>

        <!--Actions-->
        <div class="hidden items-center lg:flex lg:justify-end">
            <div v-if="isPdf">
                <ToolbarButton @click.native="decreaseSizeOfPDF" source="zoom-out" :action="$t('pdf_zoom_out')" />
                <ToolbarButton @click.native="increaseSizeOfPDF" source="zoom-in" :action="$t('pdf_zoom_in')" />
            </div>

            <div class="ml-5">
                <ToolbarButton
                    v-if="!$isThisRoute(['RequestUpload'])"
                    @click.native="downloadItem"
                    source="download"
                    :action="$t('actions.download')"
                />
                <ToolbarButton
                    v-if="canShareItem"
                    @click.native="$shareFileOrFolder(currentFile)"
                    :class="{ 'is-inactive': !canShareItem }"
                    source="share"
                    :action="$t('actions.share')"
                />
                <ToolbarButton
                    v-if="isImage"
                    @click.native="printMethod()"
                    source="print"
                    :action="$t('actions.print')"
                />
            </div>
        </div>
    </div>
</template>

<script>
import PopoverWrapper from '../Desktop/PopoverWrapper'
import PopoverItem from '../Desktop/PopoverItem'
import OptionGroup from '../FilesView/OptionGroup'
import Option from '../FilesView/Option'

import ToolbarButton from '../FilesView/ToolbarButton'
import { XIcon, MoreHorizontalIcon } from 'vue-feather-icons'
import { mapGetters } from 'vuex'
import { events } from '../../bus'

export default {
    name: 'FilePreviewToolbar',
    components: {
        MoreHorizontalIcon,
        PopoverWrapper,
        ToolbarButton,
        PopoverItem,
        OptionGroup,
        Option,
        XIcon,
    },
    computed: {
        ...mapGetters(['fastPreview', 'clipboard', 'entries']),
        currentFile() {
            return this.fastPreview ? this.fastPreview : this.clipboard[0]
        },
        sharingTitle() {
            return this.currentFile.data.relationships.shared
                ? this.$t('context_menu.share_edit')
                : this.$t('context_menu.share')
        },
        isImage() {
            return this.currentFile.data.type === 'image'
        },
        isPdf() {
            return this.currentFile.data.attributes.mimetype === 'pdf'
        },
        files() {
            let files = []

            this.entries.map((element) => {
                if (this.currentFile.data.attributes.mimetype === 'pdf') {
                    if (element.data.attributes.mimetype === 'pdf') files.push(element)
                } else {
                    if (element.data.type === this.currentFile.data.type) files.push(element)
                }
            })

            return files
        },
        showingImageIndex() {
            let activeIndex = undefined

            this.files.forEach((element, index) => {
                if (element.data.id === this.currentFile.data.id) {
                    activeIndex = index + 1
                }
            })

            return activeIndex
        },
        canShareItem() {
            return this.$isThisRoute(['Files', 'RecentUploads', 'MySharedItems'])
        },
    },
    methods: {
        showItemContextMenu() {
            if (this.$isMobile()) {
                events.$emit('mobile-context-menu:show', this.currentFile)
                this.$showMobileMenu('file-menu')
            } else {
                events.$emit('popover:open', 'file-preview-contextmenu')
            }
        },
        increaseSizeOfPDF() {
            events.$emit('document-zoom:in')
        },
        decreaseSizeOfPDF() {
            events.$emit('document-zoom:out')
        },
        printMethod() {
            let tab = document.getElementById('printable-file')
            let win = window.open('', '', 'height=700,width=700')

            win.document.write(tab.outerHTML)
            win.document.close()
            win.print()
        },
        downloadItem() {
            this.$downloadFile(
                this.currentFile.data.attributes.file_url,
                this.currentFile.data.attributes.name + '.' + this.currentFile.data.attributes.mimetype
            )
        },
        closeFullPreview() {
            events.$emit('file-preview:hide')
        },
    },
}
</script>
