<script setup lang="ts">
import {useI18n} from "vue-i18n";
import {WindowMinimise} from "../wailsjs/runtime";
import {ForceClose, GetConfig} from "../wailsjs/go/core/App";
import {useGlobalConfig} from "@/stores/globalConfig";
import {watch,onBeforeUnmount } from "vue";
import {useMagicKeys, whenever} from "@vueuse/core";
import {destroyDebugger, switchDebugger} from "@/utils/debugger/eruda";
import {initApi} from "@/api";

const {t, availableLocales: languages, locale} = useI18n();
let globalConfig = useGlobalConfig();
const elementUIConfig = globalConfig.ui

GetConfig().then((config)=>{
    let host = config.server.host;
    if(host === "0.0.0.0"){
        host = "127.0.0.1"
    }
    initApi(`http://${host}:${config.server.port}`)
})


watch(locale,(value)=>{
    if(elementUIConfig.supportLocale[value]){
        elementUIConfig.locale = elementUIConfig.supportLocale[value]
    } else {
        elementUIConfig.locale = elementUIConfig.supportLocale["zh-CN"]
    }
}, {
    immediate: true
})


const {ctrl_alt_shift_x} = useMagicKeys()
whenever(ctrl_alt_shift_x,()=>{
    switchDebugger()
})

onBeforeUnmount(()=>{
    destroyDebugger()
})

const onclickLanguageHandle = (item: string) => {
    item !== locale.value ? (locale.value = item) : false;
};

const onclickMinimise = () => {
    WindowMinimise()
};

const onclickQuit = () => {
    ForceClose()
};

document.body.addEventListener("click", function (event) {
    event.preventDefault();
});
</script>

<template>
    <!-- Header -->
    <div class="header">
        <!-- navigation -->
        <div class="nav"></div>
        <!-- Menu -->
        <div class="menu">
            <div class="language">
                <div
                    v-for="item in languages"
                    :key="item"
                    :class="{ active: item === locale }"
                    @click="onclickLanguageHandle(item)"
                    class="lang-item"
                >
                    {{ t("languages." + item) }}
                </div>
            </div>
            <div class="bar">
                <div class="bar-btn" @click="onclickMinimise">
                    {{ t("topbar.minimise") }}
                </div>
                <div class="bar-btn" @click="onclickQuit">{{ t("topbar.quit") }}</div>
            </div>
        </div>
    </div>

    <div class="mx-1">
        <el-config-provider v-bind="elementUIConfig">
            <el-select></el-select>
            <router-view v-slot="{ Component, route  }">
                <component v-if="route.matched && route.matched.length > 0 && route.matched[0].children.length === 0" :is="Component"/>
                <transition v-else name="fade" mode="out-in">
                    <div style="max-height: 100vh" class="max-h-full overflow-auto">
                        <component
                            v-if="route.meta.keepAlive != null && (route.meta.groups ? true: route.meta.keepAlive === false)"
                            :is="Component"
                            :key="route.meta.groups ? route.meta.groups : route.path"/>
                        <keep-alive v-else>
                            <component :is="Component" :key="route.meta.groups ? route.meta.groups : route.path"/>
                        </keep-alive>
                    </div>
                </transition>
            </router-view>
        </el-config-provider>
    </div>
</template>

<style lang="scss">
@import url("./assets/css/reset.css");
@import url("./assets/css/font.css");

html {
    width: 100%;
    height: 100%;
}

body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    font-family: "JetBrainsMono";
    background-color: transparent;
}

#app {
    position: static;
    // width: 900px;
    // height: 520px;
    max-height: 100vh;
    background-color: rgba(255, 255, 255, 0.9);
    overflow: auto;
}

.header {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: space-between;
    height: 50px;
    padding: 0 10px;
    background-color: rgba(0, 152, 255, 0.9);

    .nav {
        a {
            display: inline-block;
            min-width: 50px;
            height: 30px;
            line-height: 30px;
            padding: 0 5px;
            margin-right: 8px;
            background-color: #0098ff;
            border-radius: 2px;
            text-align: center;
            text-decoration: none;
            color: #000000;
            font-size: 14px;
            white-space: nowrap;

            &:hover,
            &.router-link-exact-active {
                background-color: #00acff;
                color: #ffffff;
            }
        }
    }

    .menu {
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: center;
        justify-content: space-between;

        .language {
            margin-right: 20px;
            border-radius: 2px;
            background-color: #c3c3c3;
            overflow: hidden;

            .lang-item {
                display: inline-block;
                min-width: 50px;
                height: 30px;
                line-height: 30px;
                padding: 0 5px;
                background-color: transparent;
                text-align: center;
                text-decoration: none;
                color: #000000;
                font-size: 14px;

                &:hover {
                    background-color: #00ccff;
                    cursor: pointer;
                }

                &.active {
                    background-color: #00ccff;
                    color: #ffffff;
                    cursor: not-allowed;
                }
            }
        }

        .bar {
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;
            align-items: center;
            justify-content: flex-end;
            min-width: 150px;

            .bar-btn {
                display: inline-block;
                min-width: 80px;
                height: 30px;
                line-height: 30px;
                padding: 0 5px;
                margin-left: 8px;
                background-color: #00ccff;
                border-radius: 2px;
                text-align: center;
                text-decoration: none;
                color: #000000;
                font-size: 14px;

                &:hover {
                    background-color: #00bfff;
                    color: #ffffff;
                    cursor: pointer;
                }
            }
        }
    }
}

.view {
    position: absolute;
    top: 50px;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
}
</style>
