import config from '@/config'
import Tilemap = Phaser.Tilemaps.Tilemap
import TilemapLayer = Phaser.Tilemaps.TilemapLayer
import Tileset = Phaser.Tilemaps.Tileset
import type { Zone } from '@/types'

export function getTile(x: number, y: number, layer: Phaser.Tilemaps.TilemapLayer): Phaser.Tilemaps.Tile | undefined {
  const tile: Phaser.Tilemaps.Tile = layer.getTileAtWorldXY(x, y)
  if (!tile) return undefined
  return tile
}

export function tileToWorldXY(layer: Phaser.Tilemaps.TilemapLayer, pos_x: number, pos_y: number) {
  const worldPoint = layer.tileToWorldXY(pos_x, pos_y)
  const positionX = worldPoint.x + config.tile_size.y
  const positionY = worldPoint.y

  return { positionX, positionY }
}

export function tileToWorldX(layer: Phaser.Tilemaps.TilemapLayer, pos_x: number, pos_y: number): number {
  const worldPoint = layer.tileToWorldXY(pos_x, pos_y)
  return worldPoint.x + config.tile_size.x / 2
}

export function tileToWorldY(layer: Phaser.Tilemaps.TilemapLayer, pos_x: number, pos_y: number): number {
  const worldPoint = layer.tileToWorldXY(pos_x, pos_y)
  return worldPoint.y + config.tile_size.y * 1.5
}

export function placeTile(zone: Tilemap, layer: TilemapLayer, x: number, y: number, tileName: string) {
  const tileImg = zone.getTileset(tileName) as Tileset
  if (!tileImg) return
  layer.putTileAt(tileImg.firstgid, x, y)
}

export function setAllTiles(zone: Tilemap, layer: TilemapLayer, tiles: string[][]) {
  tiles.forEach((row, y) => {
    row.forEach((tile, x) => {
      placeTile(zone, layer, x, y, tile)
    })
  })
}

export const createZoneData = (width: number, height: number, tileWidth: number, tileHeight: number) => {
  return new Phaser.Tilemaps.MapData({
    width,
    height,
    tileWidth,
    tileHeight,
    orientation: Phaser.Tilemaps.Orientation.ISOMETRIC,
    format: Phaser.Tilemaps.Formats.ARRAY_2D
  })
}

export const createTilesetImages = (zoneTilemap: Tilemap, assets: any[], tileSize: { x: number; y: number }) => {
  const tilesetImages: Tileset[] = []
  let tileCount = 1

  assets.forEach((asset) => {
    if (asset.group !== 'tiles') return
    tilesetImages.push(zoneTilemap.addTilesetImage(asset.key, asset.key, tileSize.x, tileSize.y, 0, 0, tileCount++) as Tileset)
  })
  tilesetImages.push(zoneTilemap.addTilesetImage('blank_tile', 'blank_tile', tileSize.x, tileSize.y, 0, 0, 0) as Tileset)

  return tilesetImages
}

export const initializeZoneTiles = (zoneTilemap: Tilemap, tiles: Phaser.Tilemaps.TilemapLayer, width: number, height: number) => {
  const exampleTilesArray = Array.from({ length: width }, () => Array.from({ length: height }, () => 'blank_tile'))
  exampleTilesArray.forEach((row, y) => row.forEach((tile, x) => placeTile(zoneTilemap, tiles, x, y, 'blank_tile')))
  return exampleTilesArray
}

export const updateZoneTiles = (zoneTilemap: Tilemap, tiles: Phaser.Tilemaps.TilemapLayer, zone: Zone) => {
  if (zone.tiles) {
    setAllTiles(zoneTilemap, tiles, zone.tiles)
    const zoneTiles = zone.tiles

    // Ensure zoneTiles matches the current zone dimensions, filling new spaces with 'blank_tile'
    for (let y = 0; y < zone.height; y++) {
      zoneTiles[y] = zoneTiles[y] || [] // Ensure the row exists
      for (let x = 0; x < zone.width; x++) {
        zoneTiles[y][x] = zoneTiles[y][x] || 'blank_tile' // Fill missing tiles with 'blank_tile'
      }
    }

    // Update the tilemap with any new 'blank_tile' entries
    zoneTiles.forEach((row: any, y: number) => {
      row.forEach((tileId, x) => {
        placeTile(zoneTilemap, tiles, x, y, tileId)
      })
    })

    return zoneTiles
  }
  return []
}

export const calculateDepth = (x: number, y: number, mapWidth: number) => {
  return y * mapWidth + x
}

export const sortByDepth = <T extends { positionX: number; positionY: number } | { x: number; y: number }>(items: T[], mapWidth: number) => {
  return [...items].sort((a, b) => {
    const aX = 'positionX' in a ? a.positionX : a.x
    const aY = 'positionY' in a ? a.positionY : a.y
    const bX = 'positionX' in b ? b.positionX : b.x
    const bY = 'positionY' in b ? b.positionY : b.y
    return calculateDepth(aX, aY, mapWidth) - calculateDepth(bX, bY, mapWidth)
  })
}
