import { describe, expect, test } from 'bun:test'
import { resolve } from '../src/core/gameStyle'
import { boxResize, sliceLayer, tintImage } from '../tools/style-slices'
import { decodePng, encodePng, type Image } from '../tools/bench/png'

const atlas: Image = { width: 5, height: 5, data: new Uint8Array(5 * 5 * 4) }
for (let y = 0; y < 5; y++) {
  for (let x = 0; x < 5; x++) atlas.data.set([x * 40, y * 40, 100, 255], (y * 5 + x) * 4)
}

describe('style slices on a synthetic atlas', () => {
  test('cuts nine correct regions, downsizing corners in both axes and edge thickness only', () => {
    const l = resolve({ raw: { box: { graphical_set: { position: [0, 0], corner_size: 2 } } } }, 'box').layers[0]!
    const pieces = sliceLayer(l, () => atlas)
    expect(pieces.map(p => [p.name, p.image.width, p.image.height])).toEqual([
      ['left_top', 1, 1], ['top', 1, 1], ['right_top', 1, 1], ['left', 1, 1], ['center', 1, 1], ['right', 1, 1],
      ['left_bottom', 1, 1], ['bottom', 1, 1], ['right_bottom', 1, 1],
    ])
    expect([...pieces[0]!.image.data]).toEqual([20, 20, 100, 255])
    expect([...pieces[2]!.image.data]).toEqual([140, 20, 100, 255])
    expect([...pieces[8]!.image.data]).toEqual([140, 140, 100, 255])
    expect(pieces[0]!.axes).toEqual(['fixed', 'fixed'])
    expect(pieces[1]!.axes).toEqual(['stretch', 'fixed'])
    expect(pieces[3]!.axes).toEqual(['fixed', 'stretch'])
    expect(pieces[4]!.axes).toEqual(['stretch', 'stretch'])
    expect(pieces[4]!.screenSize).toEqual([0.5, 0.5])
    expect(decodePng(encodePng(pieces[0]!.image))).toEqual(pieces[0]!.image)
  })

  test('a longer stretched axis is retained, while tiled axes are scaled', () => {
    const l = resolve({ raw: { box: { graphical_set: { top: { position: [0, 0], size: [4, 2] }, top_tiling: false } } } }, 'box').layers[0]!
    expect(sliceLayer(l, () => atlas)[0]!.image).toMatchObject({ width: 4, height: 1 })
    l.tiling.top_tiling = true
    const tiled = sliceLayer(l, () => atlas)[0]!
    expect(tiled.axes).toEqual(['tile', 'fixed'])
    expect(tiled.image).toMatchObject({ width: 2, height: 1 })
  })

  test('whole checkbox sprites scale in both axes and retain fixed placement', () => {
    const l = resolve({ raw: { checkbox: { type: 'checkbox_style', default_graphical_set: { position: [0, 0], size: [4, 4] } } } }, 'checkbox').layers[0]!
    const p = sliceLayer(l, () => atlas)[0]!
    expect(p.image).toMatchObject({ width: 2, height: 2 })
    expect(p.screenSize).toEqual([2, 2])
    expect(p.axes).toEqual(['fixed', 'fixed'])
  })

  test('keeps fractional logical sizes separately from integer raster sizes', () => {
    const l = resolve({ raw: { box: { graphical_set: { corner_size: 2, position: [0, 0], scale: 0.75 } } } }, 'box').layers[0]!
    const p = sliceLayer(l, () => atlas)[0]!
    expect(p.screenSize).toEqual([1.5, 1.5])
    expect(p.image).toMatchObject({ width: 2, height: 2 })
  })

  test('invalid scale and out-of-atlas rectangles fail instead of producing blank pieces', () => {
    const layer = (scale: number, position = [0, 0]) => resolve({ raw: { box: { graphical_set: { corner_size: 2, position, scale } } } }, 'box').layers[0]!
    expect(() => sliceLayer(layer(0), () => atlas)).toThrow('Invalid sprite geometry')
    expect(() => sliceLayer(layer(0.5, [4, 4]), () => atlas)).toThrow('outside')
    expect(() => boxResize(atlas, 0, 1)).toThrow('Invalid box-filter')
  })
})

describe('box filter and straight-alpha tint', () => {
  test('exact 2:1 atlas filtering truncates RGB and alpha instead of rounding', () => {
    const opaque: Image = { width: 2, height: 2, data: new Uint8Array([
      10, 20, 30, 255, 11, 21, 31, 255,
      11, 21, 31, 255, 11, 21, 31, 255,
    ]) }
    expect([...boxResize(opaque, 1, 1).data]).toEqual([10, 20, 30, 255])
    const translucent: Image = { width: 2, height: 2, data: new Uint8Array([
      100, 50, 25, 127, 100, 50, 25, 128,
      100, 50, 25, 128, 100, 50, 25, 128,
    ]) }
    expect([...boxResize(translucent, 1, 1).data]).toEqual([100, 50, 25, 127])
  })

  test('area weights handle a non-integer ratio, rather than picking nearest pixels', () => {
    const image: Image = { width: 3, height: 1, data: new Uint8Array([0, 0, 0, 255, 90, 90, 90, 255, 180, 180, 180, 255]) }
    expect([...boxResize(image, 2, 1).data]).toEqual([30, 30, 30, 255, 150, 150, 150, 255])
  })

  test('transparent RGB cannot bleed into opaque samples', () => {
    const image: Image = { width: 2, height: 1, data: new Uint8Array([255, 0, 0, 255, 0, 0, 255, 0]) }
    expect([...boxResize(image, 1, 1).data]).toEqual([255, 0, 0, 128])
  })

  test('tint multiplies RGB and alpha independently; opacity changes only alpha', () => {
    const image: Image = { width: 1, height: 1, data: new Uint8Array([200, 100, 50, 128]) }
    expect([...tintImage(image, [128, 255, 0, 128], 0.5).data]).toEqual([100, 100, 0, 32])
    expect([...image.data]).toEqual([200, 100, 50, 128])
  })
})
