<script setup lang="ts">
import { computed } from 'vue';
import InputNumber from 'primevue/inputnumber';
import SelectButton from 'primevue/selectbutton';
import ToggleSwitch from 'primevue/toggleswitch';
import FileEditor from './FileEditor.vue';
import OptionCategory from './OptionCategory.vue';
import OptionRow from './OptionRow.vue';
import AimeOptions from './options/Aime.vue';
import DisplayOptions from './options/Display.vue';
import KeyboardOptions from './options/Keyboard.vue';
import MiscOptions from './options/Misc.vue';
import NetworkOptions from './options/Network.vue';
import SegatoolsOptions from './options/Segatools.vue';
import StartlinerOptions from './options/Startliner.vue';
import { usePrfStore } from '../stores';

const prf = usePrfStore();

const blacklistMinModel = computed({
    get() {
        if (prf.current?.data.mu3_ini?.blacklist === undefined) {
            return null;
        }
        return prf.current?.data.mu3_ini?.blacklist[0];
    },
    set(value: number) {
        prf.current!.data.mu3_ini!.blacklist = [
            value,
            prf.current!.data.mu3_ini!.blacklist?.[1] ?? 19999,
        ];
    },
});

const blacklistMaxModel = computed({
    get() {
        if (prf.current?.data.mu3_ini?.blacklist === undefined) {
            return null;
        }
        return prf.current?.data.mu3_ini.blacklist[1];
    },
    set(value: number) {
        prf.current!.data.mu3_ini!.blacklist = [
            prf.current!.data.mu3_ini!.blacklist?.[0] ?? 10000,
            value,
        ];
    },
});

prf.reload();
</script>

<template>
    <SegatoolsOptions />
    <DisplayOptions />
    <NetworkOptions />
    <AimeOptions />
    <MiscOptions />
    <OptionCategory
        title="Extensions"
        v-if="prf.current!.meta.game === 'chunithm'"
    >
        <OptionRow title="Saekawa config">
            <FileEditor
                filename="saekawa.toml"
                promptname="saekawa config file"
                extension="toml"
            /> </OptionRow
    ></OptionCategory>
    <OptionCategory
        title="Extensions"
        v-if="prf.current!.meta.game === 'ongeki'"
    >
        <OptionRow title="Inohara config">
            <FileEditor
                filename="inohara.cfg"
                promptname="inohara config file"
                extension="cfg"
            />
        </OptionRow>
        <OptionRow title="BepInEx console">
            <!-- @vue-expect-error -->
            <ToggleSwitch v-model="prf.current!.data.bepinex.console" />
        </OptionRow>

        <OptionRow
            title="Audio mode"
            tooltip="Exclusive 2-channel mode requires 7EVENDAYSHOLIDAYS-ExclusiveAudio"
        >
            <SelectButton
                v-model="prf.current!.data.mu3_ini!.audio"
                :options="[
                    { title: 'Shared', value: 'Shared' },
                    { title: 'Exclusive 6-channel', value: 'Excl6Ch' },
                    { title: 'Exclusive 2-channel', value: 'Excl2Ch' },
                ]"
                :allow-empty="false"
                option-label="title"
                option-value="value"
        /></OptionRow>

        <OptionRow
            title="Sample rate"
            v-if="
                prf.current?.data.mods.includes(
                    '7EVENDAYSHOLIDAYS-ExclusiveAudio'
                )
            "
        >
            <SelectButton
                v-model="prf.current!.data.mu3_ini!.sample_rate"
                :disabled="prf.current!.data.mu3_ini!.audio === 'Shared'"
                :options="[
                    { title: '44.1KHz', value: 44100 },
                    { title: '48KHz', value: 48000 },
                    { title: '96KHz', value: 96000 },
                    { title: '192KHz', value: 192000 },
                ]"
                :allow-empty="false"
                option-label="title"
                option-value="value"
        /></OptionRow>

        <OptionRow
            v-if="
                prf.current?.data.mods.includes('7EVENDAYSHOLIDAYS-Blacklist')
            "
            class="number-input"
            title="Song ID Blacklist"
            tooltip="Scores on charts within this ID range will not be saved nor uploaded"
            ><InputNumber
                class="shrink"
                size="small"
                :min="9000"
                :max="99999"
                placeholder="10000"
                :use-grouping="false"
                :allow-empty="false"
                v-model="blacklistMinModel" />
            ~
            <InputNumber
                class="shrink"
                size="small"
                :min="10000"
                :max="99999"
                placeholder="19999"
                :use-grouping="false"
                :allow-empty="false"
                v-model="blacklistMaxModel"
        /></OptionRow>
        <OptionRow
            class="number-input"
            title="GP"
            v-if="
                prf.current?.data.mods.includes('7EVENDAYSHOLIDAYS-DisableGP')
            "
            ><InputNumber
                class="shrink"
                size="small"
                :min="0"
                :max="9999"
                :use-grouping="false"
                :allow-empty="false"
                v-model="prf.current!.data.mu3_ini!.gp"
            />
        </OptionRow>
        <OptionRow
            title="Unlock Bonus Tracks"
            tooltip="Disabling this option can help declutter the song list"
            v-if="
                prf.current?.data.mods.includes(
                    '7EVENDAYSHOLIDAYS-UnlockAllMusic'
                )
            "
        >
            <ToggleSwitch
                v-model="prf.current!.data.mu3_ini!.enable_bonus_tracks"
            />
        </OptionRow>
    </OptionCategory>
    <KeyboardOptions />
    <StartlinerOptions />
</template>

<style>
.number-input .p-inputnumber-input {
    width: 4rem;
}

.p-inputtext {
    width: 40vw;
    font-family: monospace !important;
}

.p-select {
    width: 40vw;
}
</style>
