<script setup lang="ts">
import { computed, ref } from 'vue';
import InputText from 'primevue/inputtext';
import Select from 'primevue/select';
import ToggleSwitch from 'primevue/toggleswitch';
import * as path from '@tauri-apps/api/path';
import { readTextFile, writeTextFile } from '@tauri-apps/plugin-fs';
import OptionCategory from '../OptionCategory.vue';
import OptionRow from '../OptionRow.vue';
import { usePkgStore, usePrfStore } from '../../stores';
import { Feature } from '../../types';
import { hasFeature, pkgKey } from '../../util';

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

const aimeCode = ref('');

prf.reload();

const aimeCodeModel = computed({
    get() {
        return aimeCode.value;
    },
    async set(value: string) {
        aimeCode.value = value;
        if (value.match(/^[0-9]{20}$/) || value.length === 0) {
            const aime_path = await path.join(await prf.configDir, 'aime.txt');
            await writeTextFile(aime_path, aimeCode.value);
        }
    },
});

const aimeCodePaste = (ev: ClipboardEvent) => {
    aimeCodeModel.value =
        ev.clipboardData
            ?.getData('text/plain')
            .split('')
            .filter((c) => c >= '0' && c <= '9')
            .join('') ?? '';
};

(async () => {
    const aime_path = await path.join(await prf.configDir, 'aime.txt');
    aimeCode.value = await readTextFile(aime_path).catch(() => '');
})();
</script>

<template>
    <OptionCategory title="Aime">
        <OptionRow
            title="Aime emulation"
            tooltip="Aime plugins can be downloaded from the package store."
        >
            <Select
                v-model="prf.current!.data.sgt.aime"
                :options="[
                    { title: 'none', value: 'Disabled' },
                    { title: 'segatools built-in', value: 'BuiltIn' },
                    ...pkgs.byFeature(Feature.Aime).map((p) => {
                        return {
                            title: pkgKey(p),
                            value: hasFeature(p, Feature.AMNet)
                                ? { AMNet: pkgKey(p) }
                                : { Other: pkgKey(p) },
                        };
                    }),
                ]"
                placeholder="none"
                option-label="title"
                option-value="value"
            ></Select>
        </OptionRow>
        <OptionRow title="Aime code">
            <InputText
                class="shrink"
                size="small"
                :disabled="prf.current!.data.sgt.aime === 'Disabled'"
                :maxlength="20"
                placeholder="00000000000000000000"
                v-model="aimeCodeModel"
                @paste="aimeCodePaste"
            />
        </OptionRow>
        <div v-if="prf.current!.data.sgt.aime?.hasOwnProperty('AMNet')">
            <OptionRow title="Server name">
                <InputText
                    class="shrink"
                    size="small"
                    placeholder="CHUNI-PENGUIN"
                    :maxlength="50"
                    v-model="prf.current!.data.sgt.amnet.name"
                />
            </OptionRow>
            <OptionRow title="Server address">
                <InputText
                    class="shrink"
                    size="small"
                    placeholder="http://+:6070"
                    :maxlength="50"
                    v-model="prf.current!.data.sgt.amnet.addr"
                />
            </OptionRow>
            <OptionRow
                title="Use AiMeDB for physical cards"
                tooltip="Whether physical cards should use AiMeDB to retrieve access codes. If the game is using a hosted network, enable this option to load the same account data/profile as you would get on a physical cab."
            >
                <ToggleSwitch v-model="prf.current!.data.sgt.amnet.physical" />
            </OptionRow>
        </div>
    </OptionCategory>
</template>
