<template>
    <Tooltip :infos="[t('archive.0')]" width="12rem">
        <img class="menu-img" :src="menu_icons.exportGame" @click="showMenu">
    </Tooltip>

    <Dialog :title="t('archive.0')" v-model="showArchive" top="15%" left='8%'>
        <div class="archive">
            <span class="tip">* {{ t('archive.1') }}</span>
            <textarea class="textarea" v-model="archive"></textarea>
        </div>
        <div class="footer">
            <button class="button" @click="copyArchive">{{ t('copyArchive.0') }}</button>
            <button class="button" @click="pasteArchive">{{ t('pasteArchive.0') }}</button>
            <button class="button" @click="importArchive">{{ t('importArchive.0') }}</button>
        </div>
    </Dialog>

</template>

<script lang="ts" setup>
import { useStore } from "vuex";
import { computed, onMounted, ref, onBeforeUnmount } from "vue";
import { useI18n } from "vue3-i18n";
import { Tooltip, Dialog } from "@/components"
import { menu_icons, archive_name } from "@/config";
import { Base64 } from "js-base64";

const { t } = useI18n();
const { state, commit, dispatch } = useStore();
const showArchive = computed(() => {
    return state.curMenu == 'archive';
});
const archive = ref('');

const showMenu = () => {
    if (showArchive.value) {
        state.curMenu = null;
    } else {
        state.curMenu = 'archive';
        archive.value = localStorage.getItem(archive_name) || '';
    }
}

const copyArchive = () => {
    navigator.clipboard.writeText(archive.value).then(() => {
        commit('set_sys_info', { msg: t('copyArchive.1'), type: 'win' })
    }, () => {
        commit('set_sys_info', { msg: t('copyArchive.2'), type: 'waring' })
    });
    showMenu();
}

const pasteArchive = () => {
    navigator.clipboard.readText().then((text) => {
        archive.value = text;
        commit('set_sys_info', { msg: t('pasteArchive.1'), type: 'win' })
    }, () => {
        commit('set_sys_info', { msg: t('pasteArchive.2'), type: 'waring' })
    });
}

const importArchive = () => {
    try {
        const data = JSON.parse(Base64.decode(Base64.decode(archive.value)));
        if (!data || data == 'undfind') {
            commit('set_sys_info', { msg: t('loadEmpty'), type: 'warning' });
        }
        dispatch('loadGame', data);
        localStorage.setItem(archive_name, archive.value);
        showMenu();
    } catch (error) {
        commit('set_sys_info', { msg: t('loadError'), type: 'warning' });
    }
}

const keydown = (e) => {
    if (e.keyCode == 65 && !e.ctrlKey) {
        showMenu();
    }
}
onMounted(() => {
    document.addEventListener('keydown', keydown)
});
onBeforeUnmount(() => {
    document.removeEventListener('keydown', keydown)
})

onMounted(() => { });
</script>
<style lang="scss" scoped>
.archive {
    padding: 0.7rem;
    display: flex;
    flex-direction: column;

    .tip {
        color: white;
        font-size: 0.8rem;
        margin: 0.25rem 0;
    }
}

.textarea {
    width: 25rem;
    height: 15rem;
    user-select: text;
    background: rgba($color: #ffffff, $alpha: 0.8);
}

.footer {
    border-top: 1px solid #ccc;
    padding: 0.4rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

@media only screen and (max-width: 768px) {
    .archive {
        padding: 0.3rem;

        .tip {
            color: white;
            font-size: 0.8rem;
            margin: 0.25rem 0;
        }
    }

    .textarea {
        width: 22rem;
        height: 20rem;
    }

}
</style>