<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 v-if="mapEditor.currentMap.value" :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="list?.close"
            @open-lists="list?.open"
          />
          <MapList ref="mapModal" @open-create-map="mapSettingsModal?.open" />
          <ListPanel ref="list" />
          <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 ListPanel from '@/components/gameMaster/mapEditor/partials/ListPanel.vue'
import MapSettings from '@/components/gameMaster/mapEditor/partials/MapSettings.vue'
import TeleportModal from '@/components/gameMaster/mapEditor/partials/TeleportModal.vue'
import Toolbar from '@/components/gameMaster/mapEditor/partials/Toolbar.vue'
import { useMapEditorComposable } from '@/composables/useMapEditorComposable'
import { loadAllTileTextures } from '@/services/mapService'
import { MapStorage } from '@/storage/storages'
import { useGameStore } from '@/stores/gameStore'
import { Game, Scene } from 'phavuer'
import { ref, useTemplateRef } from 'vue'

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

const mapModal = useTemplateRef('mapModal')
const list = useTemplateRef('list')
const mapSettingsModal = useTemplateRef('mapSettingsModal')

const isLoaded = ref(false)

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

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 loadAllTileTextures(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,
    mapEventTiles: currentMap.mapEventTiles,
    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()
  mapEditor.triggerClearTiles()
}
</script>
