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

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

export const useZoneEditorStore = defineStore('zoneEditor', {
  state: () => ({
    active: false,
    zone: null as Zone | null,
    tool: 'move',
    drawMode: 'tile',
    eraserMode: 'tile',
    zoneList: [] as Zone[],
    tileList: [] as Tile[],
    objectList: [] as Object[],
    selectedTile: null as Tile | null,
    selectedObject: null as Object | null,
    selectedZoneObject: null as ZoneObject | null,
    objectDepth: 0,
    isTileListModalShown: false,
    isObjectListModalShown: false,
    isZoneListModalShown: false,
    isCreateZoneModalShown: false,
    isSettingsModalShown: false,
    teleportSettings: {
      toZoneId: 0,
      toPositionX: 0,
      toPositionY: 0
    }
  }),
  actions: {
    toggleActive() {
      if (this.active) this.reset()
      this.active = !this.active
    },
    setZone(zone: Zone) {
      this.zone = zone
    },
    setZoneName(name: string) {
      if (this.zone) {
        this.zone.name = name
      }
    },
    setZoneWidth(width: number) {
      if (this.zone) {
        this.zone.width = width
      }
    },
    setZoneHeight(height: number) {
      if (this.zone) {
        this.zone.height = height
      }
    },
    setZonePvp(pvp: boolean) {
      if (this.zone) {
        this.zone.pvp = pvp
      }
    },
    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: Tile) {
      this.selectedTile = tile
    },
    setSelectedObject(object: any) {
      this.selectedObject = object
    },
    setSelectedZoneObject(zoneObject: ZoneObject) {
      const gameStore = useGameStore() // Access the gameStore
      if (gameStore.isMovingCamera) return // Step 2: Check isMovingCamera before proceeding

      this.selectedZoneObject = zoneObject
    },
    setObjectDepth(depth: number) {
      this.objectDepth = depth
    },
    toggleSettingsModal() {
      this.isSettingsModalShown = !this.isSettingsModalShown
    },
    toggleZoneListModal() {
      this.isZoneListModalShown = !this.isZoneListModalShown
      this.isCreateZoneModalShown = false
    },
    toggleCreateZoneModal() {
      this.isCreateZoneModalShown = !this.isCreateZoneModalShown
    },
    setTeleportSettings(teleportSettings: TeleportSettings) {
      this.teleportSettings = teleportSettings
    },
    reset() {
      this.zoneList = []
      this.tileList = []
      this.objectList = []
      this.tool = 'move'
      this.drawMode = 'tile'
      this.selectedTile = null
      this.selectedObject = null
      this.selectedZoneObject = null
      this.objectDepth = 0
      this.isSettingsModalShown = false
      this.isZoneListModalShown = false
      this.isCreateZoneModalShown = false
    }
  }
})
