import { defineStore } from 'pinia'
import type { Zone, Object, Tile } from '@/types'

export const useZoneEditorStore = defineStore('zoneEditor', {
  state: () => ({
    active: false,
    zone: null as Zone | null,
    tool: 'move',
    drawMode: 'tile',
    zoneList: [] as Zone[],
    tileList: [] as Tile[],
    objectList: [] as Object[],
    selectedTile: null as Tile | null,
    selectedObject: null as Object | null,
    objectDepth: 0,
    isZoneListModalShown: false,
    isCreateZoneModalShown: false,
    isSettingsModalShown: false
  }),
  actions: {
    toggleActive() {
      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
      }
    },
    setTool(tool: string) {
      this.tool = tool
    },
    setDrawMode(mode: string) {
      this.drawMode = 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
    },
    setObjectDepth(depth: number) {
      this.objectDepth = depth
    },
    toggleSettingsModal() {
      this.isSettingsModalShown = !this.isSettingsModalShown
    },
    toggleZoneListModal() {
      this.isZoneListModalShown = !this.isZoneListModalShown
      this.isCreateZoneModalShown = false
    },
    toggleCreateZoneModal() {
      this.isCreateZoneModalShown = !this.isCreateZoneModalShown
    },
    reset() {
      this.zoneList = []
      this.tileList = []
      this.objectList = []
      this.tool = 'move'
      this.drawMode = 'tile'
      this.selectedTile = null
      this.selectedObject = null
      this.objectDepth = 0
      this.isSettingsModalShown = false
      this.isZoneListModalShown = false
      this.isCreateZoneModalShown = false
    }
  }
})
