import config from '@/application/config'
import { getTile, tileToWorldXY } from '@/composables/mapComposable'
import { useMapEditorComposable } from '@/composables/useMapEditorComposable'
import { useGameStore } from '@/stores/gameStore'
import { computed, ref, type Ref } from 'vue'

export function useMapEditorPointerHandlers(scene: Phaser.Scene, layer: Phaser.Tilemaps.TilemapLayer, waypoint: Ref<{ visible: boolean; x: number; y: number }>, camera: Phaser.Cameras.Scene2D.Camera) {
  const gameStore = useGameStore()
  const mapEditor = useMapEditorComposable()
  const isMoveTool = computed(() => mapEditor.tool.value === 'move')
  const pointerStartPosition = ref({ x: 0, y: 0 })
  const dragThreshold = 5 // pixels

  function updateWaypoint(worldX: number, worldY: number) {
    const pointerTile = getTile(layer, worldX, worldY)
    if (!pointerTile) {
      waypoint.value.visible = false
      return
    }
    const worldPoint = tileToWorldXY(layer, pointerTile.x, pointerTile.y)
    if (!worldPoint.worldPositionX || !worldPoint.worldPositionX) return

    waypoint.value = {
      visible: true,
      x: worldPoint.worldPositionX,
      y: worldPoint.worldPositionY + config.tile_size.height + 15
    }
  }

  function handlePointerDown(pointer: Phaser.Input.Pointer) {
    pointerStartPosition.value = { x: pointer.x, y: pointer.y }
    if (isMoveTool.value || pointer.event.shiftKey) {
      gameStore.setPlayerDraggingCamera(true)
    }
  }

  function dragMap(pointer: Phaser.Input.Pointer) {
    if (!gameStore.game.isPlayerDraggingCamera) return

    const distance = Phaser.Math.Distance.Between(pointerStartPosition.value.x, pointerStartPosition.value.y, pointer.x, pointer.y)

    // If the distance is less than the drag threshold, return
    // We do this to prevent the camera from scrolling too quickly

    if (distance <= dragThreshold) return

    camera.setScroll(camera.scrollX - (pointer.x - pointer.prevPosition.x) / camera.zoom, camera.scrollY - (pointer.y - pointer.prevPosition.y) / camera.zoom)
  }

  function handlePointerMove(pointer: Phaser.Input.Pointer) {
    if (isMoveTool.value || pointer.event.shiftKey) {
      dragMap(pointer)
    }
    updateWaypoint(pointer.worldX, pointer.worldY)
  }

  function handlePointerUp(pointer: Phaser.Input.Pointer) {
    gameStore.setPlayerDraggingCamera(false)
  }

  function handleZoom(pointer: Phaser.Input.Pointer) {
    if (pointer.event instanceof WheelEvent && pointer.event.shiftKey) {
      const deltaY = pointer.event.deltaY
      const zoomLevel = camera.zoom - deltaY * 0.005
      if (zoomLevel > 0 && zoomLevel < 3) {
        camera.setZoom(zoomLevel)
      }
    }
  }

  const setupPointerHandlers = () => {
    scene.input.on(Phaser.Input.Events.POINTER_DOWN, handlePointerDown)
    scene.input.on(Phaser.Input.Events.POINTER_MOVE, handlePointerMove)
    scene.input.on(Phaser.Input.Events.POINTER_UP, handlePointerUp)
    scene.input.on(Phaser.Input.Events.POINTER_WHEEL, handleZoom)
  }

  const cleanupPointerHandlers = () => {
    scene.input.off(Phaser.Input.Events.POINTER_DOWN, handlePointerDown)
    scene.input.off(Phaser.Input.Events.POINTER_MOVE, handlePointerMove)
    scene.input.off(Phaser.Input.Events.POINTER_UP, handlePointerUp)
    scene.input.off(Phaser.Input.Events.POINTER_WHEEL, handleZoom)
  }

  return { setupPointerHandlers, cleanupPointerHandlers }
}
