<template>
  <div class="flex h-full w-full flex-col text-primary">
    <h1 class="mb-10 mt-20 text-center text-5xl font-bold">Welkom, {{ user.discord.username }}</h1>
    <Whitelist v-if="!user.minecraft.uuid" />
    <div v-else class="flex w-full max-w-3xl flex-wrap justify-center gap-4">
      <img :src="'https://api.mineatar.io/face/' + user.minecraft.uuid + '?scale=16'" class="w-24 rounded shadow">
      <div class="flex w-full max-w-md rounded border-2 border-primary p-4">
        <ul class="my-auto">
          <li>Username: <b>{{ user.minecraft.username }}</b></li>
          <li>UUID: <b>{{ user.minecraft.uuid }}</b></li>
        </ul>
      </div>
      <div class="mt-2 flex w-full justify-center gap-4">
        <Button type="danger" @click="removeWhitelist">
          Remove from whitelist
        </Button>
        <Button @click="refreshUsername">
          Refresh Username
        </Button>
      </div>
    </div>
  </div>
</template>

<script setup>
definePageMeta({
  middleware: ["auth"]
})

const user = useState('user')

const refreshUsername = async () => {
  try {
    const response = await $fetch('/api/minecraft/refreshusername')

    user.value.minecraft.username = response.username

    useToast().success('Username is ververst')
  } catch (e) {
    console.log(e)

    useToast().error(e.statusMessage)
  }
}

const removeWhitelist = async () => {
  try {
    await $fetch('/api/minecraft/removewhitelist')

    user.value.minecraft.uuid = null
    user.value.minecraft.username = null

    useToast().success('Minecraft is niet meer gekoppeld')
  } catch (e) {
    console.log(e)
    useToast().error(e.statusMessage)
  }
}
</script>
