<template>
  <div class="flex justify-center items-center h-dvh relative">
    <GmTools v-if="gameStore.character?.role === 'gm'" />
    <GmPanel v-if="gameStore.character?.role === 'gm'" />

    <div v-if="!zoneEditorStore.active">
      <Game :config="gameConfig" @create="createGame">
        <Scene name="main" @preload="preloadScene" @create="createScene">
          <div v-if="isLoaded">
            <Inventory />
            <div class="fixed inset-x-0 top-0 flex justify-start items-end p-10 pointer-events-none">
              <div class="pointer-events-auto">
                <Hud />
              </div>
            </div>
            <Zone />
            <div class="fixed inset-x-0 bottom-0 flex justify-between gap-5 items-end py-10 px-5 xxs:p-10 pointer-events-none max-md:flex-wrap max-md:flex-col-reverse">
              <div class="pointer-events-auto w-full">
                <Chat />
              </div>
              <div class="pointer-events-auto max-xs:m-auto mr-auto">
                <Menubar />
              </div>
            </div>
          </div>
        </Scene>
      </Game>
    </div>
    <div v-if="zoneEditorStore.active">
      <Game :config="gameConfig" @create="createGame">
        <Scene name="main" @preload="preloadScene" @create="createScene">
          <ZoneEditor v-if="isLoaded" :key="JSON.stringify(zoneEditorStore.zone)" />
        </Scene>
      </Game>
    </div>
  </div>
</template>

<script setup lang="ts">
import 'phaser'
import { ref, onBeforeUnmount } from 'vue'
import { Game, Scene } from 'phavuer'
import { useGameStore } from '@/stores/gameStore'
import { useZoneEditorStore } from '@/stores/zoneEditorStore'
import Hud from '@/components/gui/Hud.vue'
import Zone from '@/components/zone/Zone.vue'
import Chat from '@/components/gui/Chat.vue'
import Menubar from '@/components/gui/Menu.vue'
import GmTools from '@/components/gameMaster/GmTools.vue'
import ZoneEditor from '@/components/gameMaster/zoneEditor/ZoneEditor.vue'
import GmPanel from '@/components/gameMaster/GmPanel.vue'
import Inventory from '@/components/gui/UserPanel.vue'
import { loadAssets } from '@/composables/zoneComposable'

const gameStore = useGameStore()
const zoneEditorStore = useZoneEditorStore()
const isLoaded = ref(false)

const gameConfig = {
  name: 'New Quest',
  width: window.innerWidth,
  height: window.innerHeight,
  type: Phaser.AUTO, // AUTO, CANVAS, WEBGL, HEADLESS
  mode: Phaser.Scale.RESIZE,
  resolution: 3,
  render: { pixelArt: true, antialias: false }
}

const createGame = (game: Phaser.Game) => {
  /**
   * Resize the game when the window is resized
   */
  addEventListener('resize', () => {
    game.scale.resize(window.innerWidth, window.innerHeight)
  })

  // We don't support canvas mode, only WebGL
  if (game.renderer.type === Phaser.CANVAS) {
    gameStore.addNotification({
      title: 'Warning',
      message: 'Your browser does not support WebGL. Please use a modern browser like Chrome, Firefox, or Edge.'
    })
    gameStore.disconnectSocket()
  }
}

const preloadScene = async (scene: Phaser.Scene) => {
  isLoaded.value = false

  /**
   * Create loading bar
   */
  const width = scene.cameras.main.width
  const height = scene.cameras.main.height

  const progressBox = scene.add.graphics()
  const progressBar = scene.add.graphics()
  progressBox.fillStyle(0x222222, 0.8)
  progressBox.fillRect(width / 2 - 180, height / 2, 320, 50)

  const loadingText = scene.make.text({
    x: width / 2,
    y: height / 2 - 50,
    text: 'Loading...',
    style: {
      font: '20px monospace',
      fill: '#ffffff'
    }
  })
  loadingText.setOrigin(0.5, 0.5)

  scene.load.on(Phaser.Loader.Events.PROGRESS, function (value: any) {
    progressBar.clear()
    progressBar.fillStyle(0x368f8b, 1)
    progressBar.fillRect(width / 2 - 180 + 10, height / 2 + 10, 300 * value, 30)
  })

  scene.load.on(Phaser.Loader.Events.COMPLETE, function () {
    progressBar.destroy()
    progressBox.destroy()
    loadingText.destroy()
    isLoaded.value = true
  })

  /**
   * Load the base assets into the Phaser scene
   */
  scene.load.image('BLOCK', '/assets/zone/bt_tile.png')
  scene.load.image('TELEPORT', '/assets/zone/tp_tile.png')
  scene.load.image('blank_tile', '/assets/zone/blank_tile.png')
  scene.load.image('blank_object', '/assets/zone/blank_tile.png')
  scene.load.image('waypoint', '/assets/waypoint.png')
  scene.textures.addBase64(
    'character',
    'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAABeCAYAAAAwnXTzAAAHWUlEQVR4nLVaQUhcRxj+DCuILruHXdbnYkBim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O06+5kMv0OTLuwMvmfzODdoS8u4ANLdsA5oM3u+CcyM8l/PD/wPnRqi7R+CL0K1R3Uzg3CTUzQLOhZCOGXR2ObT+wENwU5vuloq2hHTI7JVa3B0dQsfM5JlqQav9jyEi9UIo3oO7o0PKVbRWaNMJzHydqNmFUHwPZm8Q96QzRF+zhXz0I4PPCAKRrLXInRXnx6anJ1re5QtHMFGsvwdqANph7VrQ3oxvQuv/F43zishHwzBvAak/HmJi+kl9Aeo807seyTqBl8BIW3PeSWlDiDrn9ldBWo4PYwAmrKyskWIsue4Eq+B6jk9w2yTyu8T7D0vv92u9uVoPAAAAAElFTkSuQmCC'
  )

  /**
   * Load the assets into the Phaser scene
   */
  await loadAssets(scene)
}

const createScene = async (scene: Phaser.Scene) => {
  /**
   * Create sprite animations
   * This is done here because phaser forces us to
   */
  gameStore.assets.forEach((asset) => {
    if (asset.group !== 'sprite_animations') return

    scene.anims.create({
      key: asset.key,
      frameRate: 7,
      frames: scene.anims.generateFrameNumbers(asset.key, { start: 0, end: asset.frameCount! - 1 }),
      repeat: -1
    })
  })
}

onBeforeUnmount(() => {
  isLoaded.value = false
  gameStore.disconnectSocket()
})
</script>
