<template>
  <div class="game-container">
    <GmTools />

    <Game class="game" :config="gameConfig" @create="createGame" v-if="!zoneEditorStore.active">
      <Scene name="main" @preload="preloadScene" @create="createScene">
          <div class="top-ui">
            <Hud />
          </div>
          <div class="center-ui">
            <World />
          </div>
          <div class="bottom-ui">
            <Chat />
            <Menubar />
          </div>
      </Scene>
    </Game>
    <Game class="game" :config="gameConfig" @create="createGame" v-else>
      <Scene name="main" @preload="preloadScene" @create="createScene">
        <ZoneEditor />
      </Scene>
    </Game>
  </div>
</template>

<script setup lang="ts">
import 'phaser'
import { Game, Scene } from 'phavuer'
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 { onUnmounted } from 'vue'
import GmTools from '@/components/utilities/GmTools.vue'
import { useSocketStore } from '@/stores/socket'
import { useZoneEditorStore } from '@/stores/zoneEditor'
import ZoneEditor from '@/components/utilities/zoneEditor/ZoneEditor.vue'

const socket = useSocketStore()
const zoneEditorStore = useZoneEditorStore()

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

// on page close
addEventListener('beforeunload', () => {
  socket.disconnectSocket()
})

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

const createGame = (game: Phaser.Game) => {
  addEventListener('resize', () => {
    game.scale.resize(window.innerWidth, window.innerHeight)
  })
}

const preloadScene = (scene: Phaser.Scene) => {
  /**
   * @TODO
   * Write logic that downloads all assets from out websocket or http server in base64 format
   * Don't forget to check how intensive that operation is with sockets for performance
   */
  scene.load.image('tiles', '/assets/zone/tiles.png')
  scene.load.image('waypoint', '/assets/waypoint.png')
  scene.textures.addBase64(
    'character',
    'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAABeCAYAAAAwnXTzAAAHWUlEQVR4nLVaQUhcRxj+DCuILruHXdbnYkBimkdFdouwFBdWKCWHHkoChragmAo9pKG3ltQGWrGHkEp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cyM8l/PD/wPnRqi7R+CL0K1R3Uzg3CTUzQLOhZCOGXR2ObT+wENwU5vuloq2hHTI7JVa3B0dQsfM5JlqQav9jyEi9UIo3oO7o0PKVbRWaNMJzHydqNmFUHwPZm8Q96QzRF+zhXz0I4PPCAKRrLXInRXnx6anJ1re5QtHMFGsvwdqANph7VrQ3oxvQuv/F43zishHwzBvAak/HmJi+kl9Aeo807seyTqBl8BIW3PeSWlDiDrn9ldBWo4PYwAmrKyskWIsue4Eq+B6jk9w2yTyu8T7D0vv92u9uVoPAAAAAElFTkSuQmCC'
  )
  scene.load.spritesheet('characterW', '/assets/avatar/default/walk.png', { frameWidth: 36, frameHeight: 94 })
}

const playScene = (scene: Phaser.Scene) => {}

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

  // const grid = scene.add.grid(0, 0, window.innerWidth, window.innerHeight, 64, 32, 0, 0, 0xff0000, 0.5).setOrigin(0, 0);
  //
  // window.addEventListener('resize', () => {
  //   grid.setSize(window.innerWidth, window.innerHeight);
  // });
}
</script>

<style scoped lang="scss">
.game-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  padding: 30px;
  position: relative;
}

.top-ui,
.bottom-ui {
  display: flex;
  position: absolute;
  justify-content: space-between;
  left: 0;
  right: 0;
  margin: 0 48px;
}

.top-ui {
  top: 48px;
}

.bottom-ui {
  align-items: center;
  bottom: 100px;
  height: 100px;
}
</style>
