import test from "node:test";
import assert from "node:assert/strict";
import {
	wetSandBlendWeight,
	wetSandEdgeDistance,
	wetSandWetness,
	mixWetSandPixel,
	WET_SAND_BAND_FRACTION,
	WET_SAND_NEIGHBOR_DIRS
} from "../../shared/render/wetSand.js";
import {
	GroundType,
	GROUND_DEFINITIONS,
	isLiquidWaterGround
} from "../../shared/definitions/tiles.js";
import { SeasonId } from "../../shared/world/season.js";
import {
	DEFAULT_GRAPHICS_SETTINGS,
	WetSandBand,
	wetSandBandFraction,
	normalizeGraphicsSettings
} from "../../shared/graphics/settings.js";

test("sand is blendable wet-shore ground with year-round sprites", () =>
{
	const sand = GROUND_DEFINITIONS[GroundType.sand];
	assert.ok(sand);
	assert.equal(sand.key, "sand");
	assert.equal(sand.blendable, true);
	assert.equal(sand.wetShore, true);
	assert.equal(sand.outdoor, false);
	assert.equal(sand.passable, true);
	assert.equal(sand.water, false);
});

test("wet sand band defaults to 60% and is a graphics setting", () =>
{
	assert.equal(WET_SAND_BAND_FRACTION, 0.6);
	assert.equal(WET_SAND_NEIGHBOR_DIRS.length, 8);
	assert.equal(DEFAULT_GRAPHICS_SETTINGS.wetSand, WetSandBand.medium);
	assert.equal(wetSandBandFraction(WetSandBand.medium), 0.6);
	assert.equal(wetSandBandFraction(WetSandBand.off), 0);
	assert.equal(wetSandBandFraction(WetSandBand.narrow), 0.3);
	assert.equal(wetSandBandFraction(WetSandBand.wide), 1);
	assert.equal(normalizeGraphicsSettings({}).wetSand, "60");
	assert.equal(normalizeGraphicsSettings({ wetSand: "30" }).wetSand, "30");
	assert.equal(normalizeGraphicsSettings({ wetSand: "nope" }).wetSand, "60");
});

test("east water: edge fully wet, beyond band dry", () =>
{
	const band = WET_SAND_BAND_FRACTION * 16;
	assert.equal(wetSandBlendWeight(15, 8, 1, 0), 1);
	assert.ok(wetSandBlendWeight(14, 8, 1, 0) > 0);
	assert.ok(wetSandBlendWeight(14, 8, 1, 0) < 1);
	assert.equal(wetSandBlendWeight(15 - Math.ceil(band), 8, 1, 0), 0);
	assert.equal(wetSandBlendWeight(0, 8, 1, 0), 0);
	assert.equal(wetSandBlendWeight(10, 8, 1, 0, 16, 0), 0);
	assert.equal(wetSandBlendWeight(0, 8, 1, 0, 16, 1), 1 / 16);
});

test("southeast water wets the corner fan", () =>
{
	assert.equal(wetSandEdgeDistance(15, 15, 1, 1), 0);
	assert.equal(wetSandBlendWeight(15, 15, 1, 1), 1);
	// Far from the SE corner along the east edge stays dry under the default 60% band.
	assert.equal(wetSandBlendWeight(15, 0, 1, 1), 0);
	assert.ok(wetSandBlendWeight(14, 14, 1, 1) > 0);
});

test("wetness takes the max across water directions", () =>
{
	const dirs = [{ ndx: 1, ndy: 0 }, { ndx: 0, ndy: 1 }];
	assert.equal(wetSandWetness(15, 8, dirs), 1);
	assert.equal(wetSandWetness(8, 15, dirs), 1);
	assert.equal(wetSandWetness(0, 0, dirs), 0);
});

test("mixWetSandPixel lerps dry toward wet", () =>
{
	const dry = { r: 0, g: 0, b: 0, a: 255 };
	const wet = { r: 100, g: 200, b: 50, a: 255 };
	assert.deepEqual(mixWetSandPixel(dry, wet, 0), dry);
	assert.deepEqual(mixWetSandPixel(dry, wet, 1), wet);
	assert.deepEqual(mixWetSandPixel(dry, wet, 0.5), { r: 50, g: 100, b: 25, a: 255 });
});

test("winter wet shore uses only liquid water (deep), not frozen shallows", () =>
{
	assert.equal(isLiquidWaterGround(GroundType.water, SeasonId.summer), true);
	assert.equal(isLiquidWaterGround(GroundType.water, SeasonId.winter), false);
	assert.equal(isLiquidWaterGround(GroundType.deepWater, SeasonId.winter), true);
	assert.equal(isLiquidWaterGround(GroundType.sand, SeasonId.winter), false);
});
