<template>
	<v-app :theme="theme">
		<v-app-bar :elevation="2">
			<v-app-bar-title>SHOTGUN</v-app-bar-title>
			<template #append>
				<v-btn :icon="themeIcon" @click="toggleDark()"></v-btn>
			</template>
		</v-app-bar>
		<v-main>
			<v-container v-if="!playing && !loading">
				<TheLanding />
				<v-row v-if="!loadingMe && !me" class="d-flex justify-center">
					<TheLogin />
				</v-row>
				<v-row v-else-if="!loadingMe" class="d-flex justify-center">
					<v-btn @click="clickPlay" class="mx-4" color="primary">Play</v-btn>
					<v-btn class="mx-4">Log Out</v-btn>
				</v-row>
			</v-container>
			<v-container v-else-if="loading" class="fill-height">
				<v-row class="d-flex justify-center">
					<v-progress-circular class="pa-8" indeterminate size="128"></v-progress-circular>
				</v-row>
			</v-container>
			<Game v-else :game="testGame" :dealer="true" @action="(evt: Action) => action(evt)" />
		</v-main>
	</v-app>
</template>

<script setup lang="ts">
import { useDark, useToggle } from '@vueuse/core';
import { computed, onMounted, ref } from 'vue';
import type { Action, Game } from '@/lib/game';

const dark = useDark();
const toggleDark = useToggle(dark);
const theme = computed(() => dark.value ? 'dark' : 'light');
const themeIcon = computed(() => dark.value ? 'mdi-moon-waxing-crescent' : 'mdi-white-balance-sunny');

const playing = ref(false);
const loading = ref(false);
function clickPlay() {
	loading.value = true;
	setTimeout(() => {
		playing.value = true;
		loading.value = false;
	}, 1000);
}

const testGame = ref<Game>({
	players: [
		{ hp: 4, items: ['🔍', '🔪', '', '', '', '', '', ''] },
		{ hp: 3, items: ['👮', '🚬', '', '', '', '', '', ''] },
	],
	round: 1,
	dealer: true,
	damage: 1,
	previous: null,
	live: 3,
	blank: 4,
});

function action(evt: Action) {
	console.log(evt);
}

const loadingMe = ref(true);
const me = ref<string | null>(null);
onMounted(async () => {
	loadingMe.value = true;
	try {
		const resp = await fetch('/user/me', {
			method: 'GET',
			mode: 'same-origin',
			credentials: 'same-origin',
		})
		loadingMe.value = false;
		if (!resp.ok) {
			me.value = null;
			return;
		}
		const { id } = await resp.json();
		me.value = id as string;
	} catch {
		me.value = null;
	}
	me.value = 'a';
});
</script>
