<template>
    <div id="desktop-toolbar" v-if="! $isMinimalScale()">
        <div class="toolbar-wrapper">

            <!-- Go back-->
            <div class="toolbar-go-back" v-if="homeDirectory">
                <div @click="goBack" class="go-back-button">
                    <FontAwesomeIcon
                            v-if="browseHistory.length > 0"
                            class="icon-back"
                            icon="chevron-left"
                    ></FontAwesomeIcon>
                    <span class="back-directory-title">
                        {{ directoryName }}
                    </span>
                </div>
            </div>

            <!-- Tools-->
            <div class="toolbar-tools">

                <!--Search bar-->
                <div class="toolbar-button-wrapper">
                    <SearchBar/>
                </div>

                <!--Files controlls-->
                <div class="toolbar-button-wrapper" v-if="$checkPermission(['master', 'editor'])">
                    <ToolbarButtonUpload
                            source="upload"
                            action="Upload file"
                    />
                    <ToolbarButton
                            source="trash-alt"
                            action="Delete"
                            @click.native="deleteItems"
                    />
                    <ToolbarButton
                            @click.native="createFolder"
                            source="folder-plus"
                            action="Create folder"
                    />
                </div>

                <!--View options-->
                <div class="toolbar-button-wrapper">
                    <ToolbarButton
                            :source="preview"
                            action=""
                            @click.native="$store.dispatch('changePreviewType')"
                    />
                    <ToolbarButton
                            :class="{ active: fileInfoVisible }"
                            @click.native="$store.dispatch('fileInfoToggle')"
                            source="info"
                    />
                </div>
            </div>
        </div>
        <UploadProgress />
    </div>
</template>

<script>
    import ToolbarButtonUpload from '@/components/VueFileManagerComponents/FilesView/ToolbarButtonUpload'
    import UploadProgress from '@/components/VueFileManagerComponents/FilesView/UploadProgress'
    import ToolbarButton from '@/components/VueFileManagerComponents/FilesView/ToolbarButton'
    import SearchBar from '@/components/VueFileManagerComponents/FilesView/SearchBar'
    import {mapGetters} from 'vuex'
    import {events} from '@/bus'

    export default {
        name: 'ToolBar',
        components: {
            ToolbarButtonUpload,
            UploadProgress,
            ToolbarButton,
            SearchBar
        },
        computed: {
            ...mapGetters([
                'fileInfoVisible',
                'fileInfoDetail',
                'currentFolder',
                'browseHistory',
                'homeDirectory',
                'preview_type',
            ]),
            directoryName() {
                return this.currentFolder ? this.currentFolder.name : this.homeDirectory.name
            },
            previousFolder() {
                const length = this.browseHistory.length - 2

                return this.browseHistory[length] ? this.browseHistory[length] : this.homeDirectory
            },
            preview() {
                return this.preview_type === 'list' ? 'th' : 'th-list'
            },
            isTrash() {
                return this.currentFolder.location === 'trash' || this.currentFolder.location === 'trash-root'
            }
        },
        data() {
            return {
                isSidebarMenu: false,
            }
        },
        methods: {
            showSidebarMenu() {
                this.isSidebarMenu = ! this.isSidebarMenu
                events.$emit('show:sidebar')
            },
            goBack() {

                if (this.previousFolder.location === 'trash-root') {
                    this.$store.dispatch('getTrash')
                    this.$store.commit('FLUSH_BROWSER_HISTORY')

                } else {

                    if ( this.$isThisLocation('public') ) {
                        this.$store.dispatch('browseShared', [this.previousFolder, true])
                    } else {
                        this.$store.dispatch('goToFolder', [this.previousFolder, true])
                    }
                }
            },
            deleteItems() {
                events.$emit('items:delete')
            },
            createFolder() {
                if (! this.isTrash) this.$createFolder()
            }
        },
        created() {
            // Listen for hide sidebar
            events.$on('show:content', () => {
                if (this.isSidebarMenu) this.isSidebarMenu = false
            })
        }
    }
</script>

<style scoped lang="scss">
    @import "@assets/app.scss";


    .toolbar-wrapper {
        padding-top: 15px;
        padding-bottom: 15px;
        display: flex;
        position: relative;
        z-index: 2;

        > div {
            flex-grow: 1;
            align-self: center;
            white-space: nowrap;
        }
    }

    .directory-name {
        vertical-align: middle;
        @include font-size(17);
        color: $text;
        font-weight: 700;
        max-width: 220px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: inline-block;
    }

    .icon-back {
        vertical-align: middle;
        cursor: pointer;
        margin-right: 12px;
    }

    .toolbar-go-back {
        cursor: pointer;

        .back-directory-title {
            line-height: 1;
            font-weight: 700;
            overflow: hidden;
            text-overflow: ellipsis;
            display: inline-block;
            vertical-align: middle;
            color: $text;
        }
    }

    .toolbar-position {
        text-align: center;

        span {
            @include font-size(17);
            font-weight: 600;
        }
    }

    .toolbar-tools {
        text-align: right;

        .toolbar-button-wrapper {
            margin-left: 75px;
            display: inline-block;
            vertical-align: middle;

            &:first-child {
                margin-left: 0 !important;
            }
        }

        button {
            margin-left: 20px;

            &:first-child {
                margin-left: 0;
            }
        }
    }

    @media (prefers-color-scheme: dark) {
        .toolbar .directory-name {
            color: $dark_mode_text_primary;
        }

        .toolbar-go-back {

            .back-directory-title {
                color: $dark_mode_text_primary;
            }
        }
    }
</style>
