import type { Map, MapObject, PlacedMapObject, UUID } from '@/application/types'
import { ref } from 'vue'

export type TeleportSettings = {
  toMapId: string
  toPositionX: number
  toPositionY: number
  toRotation: number
}

const currentMap = ref<Map | null>(null)
const active = ref(false)
const tool = ref('move')
const drawMode = ref('tile')
const inputMode = ref('tap')
const selectedTile = ref('')
const selectedMapObject = ref<MapObject | null>(null)
const movingPlacedObject = ref<PlacedMapObject | null>(null)
const selectedPlacedObject = ref<PlacedMapObject | null>(null)
const shouldClearTiles = ref(false)
const teleportSettings = ref<TeleportSettings>({
  toMapId: '1000',
  toPositionX: 0,
  toPositionY: 0,
  toRotation: 0
})

/**
 * We can update origin X and Y in src/components/gameMaster/mapEditor/partials/SelectedPlacedMapObject.vue
 * and this will trigger a refresh for spawned mao objects
 */
const refreshMapObject = ref(0)

export function useMapEditorComposable() {
  const loadMap = (map: Map) => {
    currentMap.value = map
  }

  const updateProperty = <K extends keyof Map>(property: K, value: Map[K]) => {
    if (currentMap.value) {
      currentMap.value[property] = value
    }
  }

  const clearMap = () => {
    if (!currentMap.value) return
    currentMap.value.placedMapObjects = []
    currentMap.value.mapEventTiles = []
  }

  const toggleActive = () => {
    if (active.value) reset()
    active.value = !active.value
  }

  const setTool = (newTool: string) => {
    tool.value = newTool
  }

  const setDrawMode = (mode: string) => {
    drawMode.value = mode
  }

  const setInputMode = (mode: string) => {
    inputMode.value = mode
  }

  const setSelectedTile = (tile: string) => {
    selectedTile.value = tile
  }

  const setSelectedMapObject = (mapObject: MapObject) => {
    selectedMapObject.value = mapObject
  }

  const setTeleportSettings = (settings: TeleportSettings) => {
    teleportSettings.value = settings
  }

  const triggerClearTiles = () => {
    shouldClearTiles.value = true
  }

  const resetClearTilesFlag = () => {
    shouldClearTiles.value = false
  }

  function triggerMapObjectRefresh() {
    refreshMapObject.value++ // Increment to trigger watchers
  }

  const reset = () => {
    tool.value = 'move'
    drawMode.value = 'tile'
    inputMode.value = 'tap'
    selectedTile.value = ''
    selectedMapObject.value = null
    shouldClearTiles.value = false
    refreshMapObject.value = 0
  }

  return {
    // State
    currentMap,
    active,
    tool,
    drawMode,
    inputMode,
    selectedTile,
    selectedMapObject,
    movingPlacedObject,
    selectedPlacedObject,
    shouldClearTiles,
    teleportSettings,
    refreshMapObject,

    // Methods
    loadMap,
    updateProperty,
    clearMap,
    toggleActive,
    setTool,
    setDrawMode,
    setInputMode,
    setSelectedTile,
    setSelectedMapObject,
    setTeleportSettings,
    triggerClearTiles,
    resetClearTilesFlag,
    triggerMapObjectRefresh,
    reset
  }
}
