<template>
  <div class="flex justify-center items-center h-dvh relative">
    <Game :config="gameConfig" @create="createGame">
      <Scene name="main" @preload="preloadScene">
        <div v-if="!isLoaded" class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 text-white text-3xl font-ui">Loading...</div>
        <div v-else>
          <Map :key="mapEditor.currentMap.value?.id" />
          <Toolbar ref="toolbar" @save="save" @clear="clear" @open-maps="mapModal?.open" @open-settings="mapSettingsModal?.open"
                   @close-editor="mapEditor.toggleActive" @close-lists="tileModal?.close" @closeLists="objectModal?.close"
                   @open-tile-list="tileModal?.open"
                   @open-map-object-list="objectModal?.open"/>
          <MapList ref="mapModal" @open-create-map="mapSettingsModal?.open"/>
          <TileList ref="tileModal"/>
          <ObjectList ref="objectModal"/>
          <MapSettings ref="mapSettingsModal" />
          <TeleportModal ref="teleportModal" />
        </div>
      </Scene>
    </Game>
  </div>
</template>

<script setup lang="ts">
import config from '@/application/config'
import 'phaser'
import type { Map as MapT } from '@/application/types'
import Map from '@/components/gameMaster/mapEditor/Map.vue'
import MapList from '@/components/gameMaster/mapEditor/partials/MapList.vue'
import ObjectList from '@/components/gameMaster/mapEditor/partials/MapObjectList.vue'
import MapSettings from '@/components/gameMaster/mapEditor/partials/MapSettings.vue'
import TeleportModal from '@/components/gameMaster/mapEditor/partials/TeleportModal.vue'
import TileList from '@/components/gameMaster/mapEditor/partials/TileList.vue'
import Toolbar from '@/components/gameMaster/mapEditor/partials/Toolbar.vue'
import { loadAllTilesIntoScene } from '@/composables/mapComposable'
import { useMapEditorComposable } from '@/composables/useMapEditorComposable'
import { MapStorage } from '@/storage/storages'
import { useGameStore } from '@/stores/gameStore'
import { Game, Scene } from 'phavuer'
import { ref, useTemplateRef, watch } from 'vue'

const mapStorage = new MapStorage()
const mapEditor = useMapEditorComposable()
const gameStore = useGameStore()

const toolbar = useTemplateRef("toolbar")
const mapModal = useTemplateRef("mapModal")
const tileModal = useTemplateRef("tileModal")
const objectModal = useTemplateRef("objectModal")
const mapSettingsModal = useTemplateRef("mapSettingsModal")
const teleportModal = useTemplateRef("teleportModal")

const isLoaded = ref(false)

const gameConfig = {
  name: config.name,
  width: window.innerWidth,
  height: window.innerHeight,
  type: Phaser.AUTO, // AUTO, CANVAS, WEBGL, HEADLESS
  resolution: 5
}

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

const preloadScene = async (scene: Phaser.Scene) => {
  // Load the base assets into the Phaser scene
  scene.load.image('BLOCK', '/assets/map/bt_tile.png')
  scene.load.image('TELEPORT', '/assets/map/tp_tile.png')
  scene.load.image('blank_tile', '/assets/map/blank_tile.png')
  scene.load.image('waypoint', '/assets/waypoint.png')

  // Get all tiles from IndexedDB and load them into the scene
  await loadAllTilesIntoScene(scene)

  // Wait for all assets to be loaded before continuing
  await new Promise<void>((resolve) => {
    scene.load.on(Phaser.Loader.Events.COMPLETE, () => {
      resolve()
    })
    isLoaded.value = true
  })
}

function save() {
  const currentMap = mapEditor.currentMap.value
  if (!currentMap) return

  const data = {
    mapId: currentMap.id,
    name: currentMap.name,
    width: currentMap.width,
    height: currentMap.height,
    tiles: currentMap.tiles,
    pvp: currentMap.pvp,
    mapEffects: currentMap.mapEffects?.map(({ id, effect, strength }) => ({ id, effect, strength })) ?? [],
    mapEventTiles: currentMap.mapEventTiles?.map(({ id, type, positionX, positionY, teleport }) => ({ id, type, positionX, positionY, teleport })) ?? [],
    placedMapObjects: currentMap.placedMapObjects?.map(({ id, mapObject, depth, isRotated, positionX, positionY }) => ({ id, mapObject, depth, isRotated, positionX, positionY })) ?? []
  }

  gameStore.connection?.emit('gm:map:update', data, (response: MapT) => {
    mapStorage.update(response.id, response)
  })
}

function clear() {
  if (!mapEditor.currentMap.value) return

  // Clear placed objects, event tiles and tiles
  mapEditor.clearMap()
}
</script>
