import { expect } from "@esm-bundle/chai";
import { RustedWallTextureGenerator } from "../../src/generation/textures/RustedWallTextureGenerator.js";

describe("System: Procedural Textures (Rusted Wall)", () => {
  it("CoA 1: Should generate valid OffscreenCanvas maps for all PBR channels", () => {
    const gen = new RustedWallTextureGenerator(123);
    const maps = gen.generateWall(64);

    expect(maps.diffuse).to.be.instanceOf(OffscreenCanvas);
    expect(maps.normal).to.be.instanceOf(OffscreenCanvas);
    expect(maps.roughness).to.be.instanceOf(OffscreenCanvas);
    expect(maps.bump).to.be.instanceOf(OffscreenCanvas);
  });

  it("CoA 2: Should populate pixel data including bolts and rust", () => {
    const gen = new RustedWallTextureGenerator(123);
    const maps = gen.generateWall(64);
    const ctx = maps.diffuse.getContext("2d");
    const data = ctx.getImageData(0, 0, 64, 64).data;

    // Sampling a Bolt location (Top of panel, x offset 8)
    // Panel Height 16. Bolt is at y=2, x=8 (center of bolt zone)
    const boltIndex = (2 * 64 + 8) * 4;

    expect(data[boltIndex + 3]).to.equal(255); // Opaque
    // Bolts are bright metal
    expect(data[boltIndex]).to.be.greaterThan(80);
  });

  it("CoA 3: Should generate variant patterns with different seeds", () => {
    const gen = new RustedWallTextureGenerator(123);

    const map1 = gen.generateWall(64, 111);
    const map2 = gen.generateWall(64, 222);

    // Sample middle of a panel (y=8) to avoid horizontal seams.
    // Use x=24 to ensure we are well within the noise field and away from structural features
    const y = 8;
    const x = 24;

    const data1 = map1.diffuse.getContext("2d").getImageData(x, y, 1, 1).data;
    const data2 = map2.diffuse.getContext("2d").getImageData(x, y, 1, 1).data;

    // Color should differ due to rust/grime noise
    // Check Red, Green, and Blue channels for any difference
    const isIdentical =
      data1[0] === data2[0] && data1[1] === data2[1] && data1[2] === data2[2];

    expect(isIdentical).to.be.false;
  });
});
