<template>
<div class="_formRoot">
	<FormRange v-model="masterVolume" :min="0" :max="1" :step="0.05" :text-converter="textConverter" class="_formBlock">
		<template #label>{{ i18n.ts.masterVolume }}</template>
	</FormRange>

	<FormSection>
		<template #label>{{ i18n.ts.sounds }}</template>
		<FormLink v-for="type in Object.keys(sounds)" :key="type" style="margin-bottom: 8px;" @click="edit(type)">
			{{ i18n.t('_sfx.' + type) }}
			<template #suffix>{{ sounds[type].type || i18n.ts.none }}</template>
			<template #suffixIcon><i class="fas fa-chevron-down"></i></template>
		</FormLink>
	</FormSection>

	<MkButton danger class="_formBlock" @click="reset()"><i class="fas fa-redo"></i> {{ i18n.ts.default }}</MkButton>
</div>
</template>

<script lang="ts" setup>
import { computed, ref } from 'vue';
import FormRange from '@/components/form/range.vue';
import MkButton from '@/components/ui/button.vue';
import FormLink from '@/components/form/link.vue';
import FormSection from '@/components/form/section.vue';
import * as os from '@/os';
import { ColdDeviceStorage } from '@/store';
import { playFile } from '@/scripts/sound';
import { i18n } from '@/i18n';
import { definePageMetadata } from '@/scripts/page-metadata';

const masterVolume = computed({
	get: () => {
		return ColdDeviceStorage.get('sound_masterVolume');
	},
	set: (value) => {
		ColdDeviceStorage.set('sound_masterVolume', value);
	},
});

const volumeIcon = computed(() => masterVolume.value === 0 ? 'fas fa-volume-mute' : 'fas fa-volume-up');

const sounds = ref({
	note: ColdDeviceStorage.get('sound_note'),
	noteMy: ColdDeviceStorage.get('sound_noteMy'),
	notification: ColdDeviceStorage.get('sound_notification'),
	chat: ColdDeviceStorage.get('sound_chat'),
	chatBg: ColdDeviceStorage.get('sound_chatBg'),
	antenna: ColdDeviceStorage.get('sound_antenna'),
	channel: ColdDeviceStorage.get('sound_channel'),
});

const soundsTypes = [
	null,
	'syuilo/up',
	'syuilo/down',
	'syuilo/pope1',
	'syuilo/pope2',
	'syuilo/waon',
	'syuilo/popo',
	'syuilo/triple',
	'syuilo/poi1',
	'syuilo/poi2',
	'syuilo/pirori',
	'syuilo/pirori-wet',
	'syuilo/pirori-square-wet',
	'syuilo/square-pico',
	'syuilo/reverved',
	'syuilo/ryukyu',
	'syuilo/kick',
	'syuilo/snare',
	'syuilo/queue-jammed',
	'aisha/1',
	'aisha/2',
	'aisha/3',
	'noizenecio/kick_gaba',
	'noizenecio/kick_gaba2',
];

const textConverter = (v) => `${Math.floor(v * 100)}%`;

async function edit(type) {
	const { canceled, result } = await os.form(i18n.t('_sfx.' + type), {
		type: {
			type: 'enum',
			enum: soundsTypes.map(x => ({
				value: x,
				label: x ?? i18n.ts.none,
			})),
			label: i18n.ts.sound,
			default: sounds.value[type].type,
		},
		volume: {
			type: 'range',
			min: 0,
			max: 1,
			step: 0.05,
			textConverter,
			label: i18n.ts.volume,
			default: sounds.value[type].volume,
		},
		listen: {
			type: 'button',
			content: i18n.ts.listen,
			action: (_, values) => {
				playFile(values.type, values.volume);
			},
		},
	});
	if (canceled) return;

	const v = {
		type: result.type,
		volume: result.volume,
	};

	ColdDeviceStorage.set('sound_' + type, v);
	sounds.value[type] = v;
}

function reset() {
	for (const sound of Object.keys(sounds.value)) {
		const v = ColdDeviceStorage.default['sound_' + sound];
		ColdDeviceStorage.set('sound_' + sound, v);
		sounds.value[sound] = v;
	}
}

definePageMetadata({
	title: i18n.ts.sounds,
	icon: 'fas fa-music',
});
</script>
