<script setup lang="ts">
import { Ref, computed, ref } from 'vue';
import Select from 'primevue/select';
import { useConfirm } from 'primevue/useconfirm';
import { emit } from '@tauri-apps/api/event';
import * as path from '@tauri-apps/api/path';
import FilePicker from '../FilePicker.vue';
import OptionCategory from '../OptionCategory.vue';
import OptionRow from '../OptionRow.vue';
import { invoke } from '../../invoke';
import { usePkgStore, usePrfStore } from '../../stores';
import { Feature } from '../../types';
import { pkgKey } from '../../util';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();

const prf = usePrfStore();
const pkgs = usePkgStore();
const confirmDialog = useConfirm();

const capabilities: Ref<string[]> = ref([]);

invoke('list_platform_capabilities').then(async (v: unknown) => {
    if (Array.isArray(v)) {
        capabilities.value.push(...v);
    }
});

const names = computed(() => {
    switch (prf.current?.meta.game) {
        case 'ongeki': {
            return {
                exe: 'mu3.exe',
                hook: 'mu3hook',
                io: 'mu3io',
            };
        }
        case 'chunithm': {
            return {
                exe: 'chusanApp.exe',
                hook: 'chusanhook',
                io: 'chuniio',
            };
        }
    }
});

const checkSegatoolsIni = async (target: string) => {
    const iniPath = await path.join(target, '../segatools.ini');
    if (await invoke('file_exists', { path: iniPath })) {
        confirmDialog.require({
            message: 'Would you like to load the existing configuration data?',
            header: 'segatools.ini found',
            accept: async () => {
                await invoke('load_segatools_ini', { path: iniPath });
                await prf.reload();
                await emit('reload-aime-code');
            },
        });
    }
};
</script>

<template>
    <OptionCategory :title="t('cfg.segatools.general')">
        <OptionRow
            :title="names?.exe"
            :tooltip="t('cfg.segatools.targetTooltip')"
        >
            <FilePicker
                :directory="false"
                :promptname="names?.exe"
                extension="exe"
                :value="prf.current?.data.sgt.target"
                :callback="
                    (value: string) => (
                        (prf.current!.data.sgt.target = value),
                        checkSegatoolsIni(value)
                    )
                "
            ></FilePicker>
        </OptionRow>

        <OptionRow title="amfs">
            <FilePicker
                :directory="true"
                placeholder="amfs"
                :value="prf.current?.data.sgt.amfs"
                :callback="
                    (value: string) => (prf.current!.data.sgt.amfs = value)
                "
            ></FilePicker>
        </OptionRow>
        <OptionRow title="option">
            <FilePicker
                :directory="true"
                placeholder="option"
                :value="prf.current!.data.sgt.option"
                :callback="
                    (value: string) => (prf.current!.data.sgt.option = value)
                "
            ></FilePicker>
        </OptionRow>
        <OptionRow title="appdata">
            <FilePicker
                :directory="true"
                :value="prf.current!.data.sgt.appdata"
                :callback="
                    (value: string) => (prf.current!.data.sgt.appdata = value)
                "
            ></FilePicker>
        </OptionRow>
        <OptionRow
            :title="names?.hook"
            :tooltip="
                t('cfg.segatools.installTooltip', {
                    thing: t('cfg.segatools.hooks'),
                })
            "
        >
            <Select
                v-model="prf.current!.data.sgt.hook"
                :options="
                    pkgs
                        .byFeature(
                            prf.current?.meta.game === 'ongeki'
                                ? Feature.Mu3Hook
                                : Feature.ChusanHook
                        )
                        .map((p) => {
                            return { title: pkgKey(p), value: pkgKey(p) };
                        })
                "
                placeholder="none"
                option-label="title"
                option-value="value"
            ></Select>
        </OptionRow>
        <OptionRow
            :title="names?.io"
            :tooltip="`${t('cfg.segatools.ioModulesDesc')}
                ${t('cfg.segatools.installTooltip', {
                    thing: t('cfg.segatools.ioModules'),
                })}`"
        >
            <Select
                v-model="prf.current!.data.sgt.io2"
                :options="[
                    { title: 'native io4', value: 'hardware' },
                    {
                        title: 'segatools built-in (keyboard)',
                        value: 'segatools_built_in',
                    },
                    ...pkgs
                        .byFeature(
                            prf.current?.meta.game === 'ongeki'
                                ? Feature.Mu3IO
                                : Feature.ChuniIO
                        )
                        .map((p) => {
                            return {
                                title: pkgKey(p),
                                value: { custom: pkgKey(p) },
                            };
                        }),
                ]"
                option-label="title"
                option-value="value"
            ></Select>
        </OptionRow>
        <OptionRow
            v-if="capabilities.includes('wine')"
            :title="t('cfg.wine.runtime')"
        >
            <FilePicker
                :directory="false"
                :value="prf.current!.data.wine.runtime"
                :callback="
                    (value: string) => (prf.current!.data.wine.runtime = value)
                "
            ></FilePicker>
        </OptionRow>
        <OptionRow
            v-if="capabilities.includes('wine')"
            :title="t('cfg.wine.prefix')"
        >
            <FilePicker
                :directory="true"
                :value="prf.current!.data.wine.prefix"
                :callback="
                    (value: string) => (prf.current!.data.wine.prefix = value)
                "
            ></FilePicker>
        </OptionRow>
    </OptionCategory>
</template>
