import { defineStore } from 'pinia'
import { useGameStore } from '@/stores/gameStore'
import type { Zone, Object, Tile, ZoneEffect, ZoneObject } from '@/types'

export type TeleportSettings = {
  toZoneId: number
  toPositionX: number
  toPositionY: number
  toRotation: number
}

export const useZoneEditorStore = defineStore('zoneEditor', {
  state: () => {
    return {
      active: false,
      zone: null as Zone | null,
      tool: 'move',
      drawMode: 'tile',
      eraserMode: 'tile',
      zoneList: [] as Zone[],
      tileList: [] as Tile[],
      objectList: [] as Object[],
      selectedTile: '',
      selectedObject: null as Object | null,
      isTileListModalShown: false,
      isObjectListModalShown: false,
      isZoneListModalShown: false,
      isCreateZoneModalShown: false,
      isSettingsModalShown: false,
      shouldClearTiles: false,
      zoneSettings: {
        name: '',
        width: 0,
        height: 0,
        pvp: false,
        zoneEffects: [] as ZoneEffect[]
      },
      teleportSettings: {
        toZoneId: 0,
        toPositionX: 0,
        toPositionY: 0,
        toRotation: 0
      } as TeleportSettings
    }
  },
  actions: {
    toggleActive() {
      const gameStore = useGameStore()
      if (!this.active) gameStore.connection?.emit('zone:character:leave')
      if (this.active) this.reset()
      this.active = !this.active
    },
    setZone(zone: Zone | null) {
      this.zone = zone
    },
    setZoneName(name: string) {
      this.zoneSettings.name = name
    },
    setZoneWidth(width: number) {
      this.zoneSettings.width = width
    },
    setZoneHeight(height: number) {
      this.zoneSettings.height = height
    },
    setZonePvp(pvp: boolean) {
      if (!this.zone) return
      this.zone.pvp = pvp
    },
    setZoneEffects(zoneEffects: ZoneEffect[]) {
      if (!this.zone) return
      this.zoneSettings.zoneEffects = zoneEffects
    },
    setTool(tool: string) {
      this.tool = tool
    },
    setDrawMode(mode: string) {
      this.drawMode = mode
    },
    setEraserMode(mode: string) {
      this.eraserMode = mode
    },
    setZoneList(zones: Zone[]) {
      this.zoneList = zones
    },
    setTileList(tiles: Tile[]) {
      this.tileList = tiles
    },
    setObjectList(objects: Object[]) {
      this.objectList = objects
    },
    setSelectedTile(tile: string) {
      this.selectedTile = tile
    },
    setSelectedObject(object: Object) {
      this.selectedObject = object
    },
    toggleSettingsModal() {
      this.isSettingsModalShown = !this.isSettingsModalShown
    },
    toggleZoneListModal() {
      this.isZoneListModalShown = !this.isZoneListModalShown
      this.isCreateZoneModalShown = false
    },
    toggleCreateZoneModal() {
      this.isCreateZoneModalShown = !this.isCreateZoneModalShown
    },
    setTeleportSettings(teleportSettings: TeleportSettings) {
      this.teleportSettings = teleportSettings
    },
    triggerClearTiles() {
      this.shouldClearTiles = true
    },

    resetClearTilesFlag() {
      this.shouldClearTiles = false
    },
    reset(resetZone = false) {
      if (resetZone) this.zone = null
      this.zoneList = []
      this.tileList = []
      this.objectList = []
      this.tool = 'move'
      this.drawMode = 'tile'
      this.selectedTile = ''
      this.selectedObject = null
      this.isTileListModalShown = false
      this.isObjectListModalShown = false
      this.isSettingsModalShown = false
      this.isZoneListModalShown = false
      this.isCreateZoneModalShown = false
      this.shouldClearTiles = false
    }
  }
})
