<template>
  <div class="flex justify-center items-center h-dvh p-[30px] relative">
    <GmTools v-if="isLoaded && gameStore.character?.role === 'gm'" />
    <GmPanel v-if="isLoaded && gameStore.character?.role === 'gm'" />

    <Game :config="gameConfig" @create="createGame" v-if="!zoneEditorStore.active">
      <Scene name="main" @preload="preloadScene" @create="createScene">
        <div class="flex absolute justify-between left-0 right-0 top-[48px] mx-[48px] my-0" v-if="isLoaded">
          <Hud />
        </div>
        <div v-if="isLoaded">
          <World :key="gameStore.character?.zoneId" />
        </div>
        <div class="flex absolute justify-between left-0 right-0 bottom-[100px] h-[100px] mx-[48px] my-0" v-if="isLoaded">
          <Chat />
          <Menubar />
        </div>
      </Scene>
    </Game>
    <Game :config="gameConfig" @create="createGame" v-if="zoneEditorStore.active">
      <Scene name="main" @preload="preloadScene" @create="createScene">
        <ZoneEditor v-if="isLoaded" :key="zoneEditorStore.zone?.id ?? 0" />
      </Scene>
    </Game>
  </div>
</template>

<script setup lang="ts">
import config from '@/config'
import 'phaser'
import { onUnmounted, watch, ref } from 'vue'
import { storeToRefs } from 'pinia'
import { Game, Scene } from 'phavuer'
import { useGameStore } from '@/stores/game'
import { useZoneStore } from '@/stores/zone'
import { useZoneEditorStore } from '@/stores/zoneEditor'
import { useAssetStore } from '@/stores/assets'
import World from '@/components/World.vue'
import Hud from '@/components/gui/Hud.vue'
import Chat from '@/components/gui/Chat.vue'
import Menubar from '@/components/gui/Menu.vue'
import GmTools from '@/components/utilities/GmTools.vue'
import ZoneEditor from '@/components/utilities/zoneEditor/ZoneEditor.vue'
import GmPanel from '@/components/utilities/GmPanel.vue'

const gameStore = useGameStore()
const zoneEditorStore = useZoneEditorStore()
const assetStore = useAssetStore()
const isLoaded = ref(false)
const { assets } = storeToRefs(assetStore)

onUnmounted(() => {
  gameStore.disconnectSocket()
})

// On page close
addEventListener('beforeunload', () => {
  gameStore.disconnectSocket()
})

const gameConfig = {
  name: 'New Quest',
  width: window.innerWidth,
  height: window.innerHeight,
  type: Phaser.AUTO,
  resolution: 3,
  pixelArt: true
}

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

const preloadScene = (scene: Phaser.Scene) => {
  /**
   * 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('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('complete', function () {
    progressBar.destroy()
    progressBox.destroy()
    loadingText.destroy()
    isLoaded.value = true
  })

  /**
   * Load the assets into the Phaser scene
   */
  assets.value.forEach((asset) => {
    if (asset.type === 'link') {
      scene.load.image(asset.key, config.server_endpoint + asset.value)
    }
    if (asset.type === 'base64') {
      scene.textures.addBase64(asset.key, asset.value)
    }
  })

  scene.load.image('BLOCK', '/assets/zone/bt_tile.png')
  scene.load.image('WARP', '/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+DCuILruHXdbnYkBimkdFdouwFBdWKCWHHkoChragmAo9pKG3ltQGWrGHkEpzS0t7Mgn2EiIYcvQiLurBIF0RYYtbFiK6Lhqyiy4BBXt4+8/OzHvz3ryN/eCx782bN9/8//z/P//M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M5JlqQav9jyEi9UIo3oO7o0PKVbRWaNMJzHydqNmFUHwPZm8Q96QzRF+zhXz0I4PPCAKRrLXInRXnx6anJ1re5QtHMFGsvwdqANph7VrQ3oxvQuv/F43zishHwzBvAak/HmJi+kl9Aeo807seyTqBl8BIW3PeSWlDiDrn9ldBWo4PYwAmrKyskWIsue4Eq+B6jk9w2yTyu8T7D0vv92u9uVoPAAAAAElFTkSuQmCC'
  )
  scene.load.spritesheet('characterW', '/assets/avatar/default/walk.png', { frameWidth: 36, frameHeight: 94 })
}

const createScene = (scene: Phaser.Scene) => {
  scene.anims.create({
    key: 'walk',
    frameRate: 7,
    frames: scene.anims.generateFrameNumbers('characterW', { start: 0, end: 3 }),
    repeat: -1
  })

  /**
   * Watch for changes in assets and reload them
   */
  watch(assets, (newAssets) => {
    newAssets.forEach((asset) => {
      if (asset.type === 'link') {
        scene.load.image(asset.key, config.server_endpoint + '/assets' + asset.value + '.png')
      }
      if (asset.type === 'base64') {
        scene.textures.addBase64(asset.key, asset.value)
      }
    })

    scene.load.start()

    scene.load.once('complete', () => {
      console.log('assets re-loaded')
    })
  })
}

// watch(() => zoneEditorStore.zone, () => {
//   isLoaded.value = false
// }, {deep:true});
</script>
