import type { LevelData, Vec2 } from "../simulation/types";

interface LevelBlueprint extends Omit<LevelData, "walls"> {
  interiorWalls: Vec2[];
}

function withBoundaryWalls(level: LevelBlueprint): LevelData {
  const boundary: Vec2[] = [];
  for (let x = 0; x < level.width; x += 1) {
    boundary.push({ x, y: 0 });
    boundary.push({ x, y: level.height - 1 });
  }
  for (let y = 1; y < level.height - 1; y += 1) {
    boundary.push({ x: 0, y });
    boundary.push({ x: level.width - 1, y });
  }

  const wallsByKey = new Map<string, Vec2>();
  for (const wall of [...boundary, ...level.interiorWalls]) {
    wallsByKey.set(`${wall.x},${wall.y}`, wall);
  }

  return {
    id: level.id,
    title: level.title,
    width: level.width,
    height: level.height,
    playerStart: level.playerStart,
    emitter: level.emitter,
    emitters: level.emitters,
    detectors: level.detectors,
    requiredDetectors: level.requiredDetectors,
    portal: level.portal,
    devices: level.devices,
    walls: [...wallsByKey.values()]
  };
}

export const CAMPAIGN_LEVELS: LevelData[] = [
  withBoundaryWalls({
    id: "l1-mirror-and-prism",
    title: "Зеркало и призма",
    width: 13,
    height: 9,
    playerStart: { x: 2, y: 7 },
    emitter: { x: 1, y: 4, dir: "right", color: "white" },
    emitters: [
      { x: 1, y: 4, dir: "right", color: "white" }
    ],
    detectors: [
      { x: 4, y: 1, requiredColor: "red", minRays: 1 }
    ],
    requiredDetectors: 1,
    portal: { x: 11, y: 7 },
    devices: [
      { id: "mirror-2", type: "mirror", x: 8, y: 4, rot: 0 },
      { id: "prism-1", type: "prism", x: 8, y: 1, rot: 0, tint: "white" }
    ],
    interiorWalls: [
      { x: 6, y: 2 },
      { x: 6, y: 3 },
      { x: 10, y: 3 },
      { x: 10, y: 4 },
      { x: 10, y: 5 },
      { x: 4, y: 6 },
      { x: 5, y: 6 },
      { x: 6, y: 6 }
    ]
  }),
  withBoundaryWalls({
    id: "l2-beam-splitter",
    title: "Разделитель",
    width: 13,
    height: 9,
    playerStart: { x: 2, y: 7 },
    emitter: { x: 1, y: 4, dir: "right", color: "white" },
    emitters: [
      { x: 1, y: 4, dir: "right", color: "white" }
    ],
    detectors: [
      { x: 8, y: 1, requiredColor: "red", minRays: 1 },
      { x: 4, y: 1, requiredColor: "green", minRays: 1 }
    ],
    requiredDetectors: 2,
    portal: { x: 11, y: 7 },
    devices: [
      { id: "splitter-1", type: "splitter", x: 5, y: 7, rot: 2 },
      { id: "prism-1", type: "prism", x: 1, y: 1, rot: 1, tint: "white" },
      { id: "mirror-1", type: "mirror", x: 5, y: 4, rot: 1 },
      { id: "prism-3", type: "prism", x: 11, y: 3, rot: 0, tint: "white" }
    ],
    interiorWalls: [
      { x: 6, y: 1 },
      { x: 6, y: 2 },
      { x: 6, y: 3 },
      { x: 7, y: 3 },
      { x: 8, y: 3 },
      { x: 9, y: 3 },
      { x: 6, y: 4 },
      { x: 6, y: 5 }
    ]
  }),
  withBoundaryWalls({
    id: "l3-color-mix",
    title: "Смешение цветов",
    width: 13,
    height: 9,
    playerStart: { x: 2, y: 7 },
    emitter: { x: 1, y: 4, dir: "up", color: "blue" },
    emitters: [
      { x: 1, y: 4, dir: "up", color: "blue" },
      { x: 1, y: 5, dir: "right", color: "yellow" }
    ],
    detectors: [
      { x: 4, y: 1, requiredColor: "magenta", minRays: 2 }
    ],
    requiredDetectors: 1,
    portal: { x: 11, y: 7 },
    devices: [
      { id: "prism-3", type: "prism", x: 1, y: 2, rot: 2, tint: "white" },
      { id: "mirror-1", type: "mirror", x: 9, y: 5, rot: 2 },
      { id: "prism-1", type: "prism", x: 9, y: 3, rot: 2, tint: "white" }
    ],
    interiorWalls: [
      { x: 9, y: 1 },
      { x: 10, y: 1 },
      { x: 11, y: 1 },
      { x: 9, y: 2 },
      { x: 10, y: 2 },
      { x: 11, y: 2 },
      { x: 3, y: 3 },
      { x: 4, y: 3 },
      { x: 5, y: 3 },
      { x: 5, y: 6 },
      { x: 5, y: 7 }
    ]
  }),
  withBoundaryWalls({
    id: "l4-symmetry",
    title: "Симметрия",
    width: 13,
    height: 9,
    playerStart: { x: 2, y: 7 },
    emitter: { x: 1, y: 4, dir: "right", color: "white" },
    emitters: [
      { x: 1, y: 4, dir: "right", color: "white" }
    ],
    detectors: [
      { x: 2, y: 6, requiredColor: "red", minRays: 1 },
      { x: 2, y: 2, requiredColor: "yellow", minRays: 1 },
      { x: 9, y: 4, requiredColor: "white", minRays: 2 }
    ],
    requiredDetectors: 3,
    portal: { x: 8, y: 4 },
    devices: [
      { id: "splitter-1", type: "splitter", x: 4, y: 4, rot: 0 },
      { id: "prism-5", type: "prism", x: 5, y: 2, rot: 0, tint: "white" },
      { id: "splitter-2", type: "splitter", x: 7, y: 1, rot: 0 },
      { id: "splitter-3", type: "splitter", x: 7, y: 7, rot: 1 },
      { id: "prism-1", type: "prism", x: 5, y: 6, rot: 1, tint: "white" }
    ],
    interiorWalls: [
      { x: 7, y: 3 },
      { x: 7, y: 4 },
      { x: 7, y: 5 }
    ]
  }),
  withBoundaryWalls({
    id: "l5-light-split",
    title: "Разложение света",
    width: 13,
    height: 9,
    playerStart: { x: 2, y: 7 },
    emitter: { x: 1, y: 4, dir: "right", color: "magenta" },
    emitters: [
      { x: 1, y: 4, dir: "right", color: "magenta" }
    ],
    detectors: [
      { x: 5, y: 7, requiredColor: "red", minRays: 1 },
      { x: 4, y: 7, requiredColor: "blue", minRays: 1 }
    ],
    requiredDetectors: 2,
    portal: { x: 11, y: 7 },
    devices: [
      { id: "splitter-1", type: "splitter", x: 7, y: 4, rot: 0 },
      { id: "prism-2", type: "prism", x: 4, y: 1, rot: 0, tint: "white" },
      { id: "mirror-1", type: "mirror", x: 7, y: 1, rot: 1 },
      { id: "prism-1", type: "prism", x: 9, y: 4, rot: 1, tint: "white" }
    ],
    interiorWalls: [
      { x: 5, y: 2 },
      { x: 6, y: 2 },
      { x: 5, y: 3 },
      { x: 6, y: 3 }
    ]
  }),
  withBoundaryWalls({
    id: "l6-light-split-2",
    title: "Разложение света 2",
    width: 13,
    height: 9,
    playerStart: { x: 2, y: 2 },
    emitter: { x: 1, y: 4, dir: "right", color: "yellow" },
    emitters: [
      { x: 1, y: 4, dir: "right", color: "yellow" }
    ],
    detectors: [
      { x: 4, y: 1, requiredColor: "red", minRays: 1 },
      { x: 6, y: 7, requiredColor: "green", minRays: 1 }
    ],
    requiredDetectors: 2,
    portal: { x: 11, y: 7 },
    devices: [
      { id: "mirror-1", type: "mirror", x: 4, y: 4, rot: 0 },
      { id: "mirror-2", type: "mirror", x: 3, y: 5, rot: 0 },
      { id: "splitter-1", type: "splitter", x: 8, y: 5, rot: 0 },
      { id: "prism-1", type: "prism", x: 9, y: 7, rot: 0, tint: "white" },
      { id: "mirror-4", type: "mirror", x: 6, y: 4, rot: 1 },
      { id: "mirror-3", type: "mirror", x: 10, y: 4, rot: 1 },
      { id: "prism-2", type: "prism", x: 6, y: 1, rot: 0, tint: "white" }
    ],
    interiorWalls: [
      { x: 7, y: 1 },
      { x: 8, y: 1 },
      { x: 9, y: 1 },
      { x: 10, y: 1 },
      { x: 11, y: 1 },
      { x: 7, y: 2 },
      { x: 8, y: 2 },
      { x: 9, y: 2 },
      { x: 10, y: 2 },
      { x: 11, y: 2 },
      { x: 1, y: 3 },
      { x: 2, y: 3 },
      { x: 3, y: 3 },
      { x: 4, y: 3 },
      { x: 5, y: 3 },
      { x: 1, y: 6 },
      { x: 2, y: 6 },
      { x: 3, y: 6 },
      { x: 4, y: 6 },
      { x: 5, y: 6 },
      { x: 1, y: 7 },
      { x: 2, y: 7 },
      { x: 3, y: 7 },
      { x: 4, y: 7 },
      { x: 5, y: 7 }
    ]
  }),
  withBoundaryWalls({
    id: "l7-intersection",
    title: "Перекресток",
    width: 13,
    height: 9,
    playerStart: { x: 2, y: 7 },
    emitter: { x: 1, y: 4, dir: "right", color: "white" },
    emitters: [
      { x: 1, y: 4, dir: "right", color: "white" }
    ],
    detectors: [
      { x: 6, y: 6, requiredColor: "green", minRays: 2 },
      { x: 3, y: 2, requiredColor: "yellow", minRays: 2 }
    ],
    requiredDetectors: 2,
    portal: { x: 11, y: 7 },
    devices: [
      { id: "prism-1", type: "prism", x: 2, y: 6, rot: 0, tint: "white" },
      { id: "splitter-2", type: "splitter", x: 10, y: 5, rot: 0 },
      { id: "prism-3", type: "prism", x: 11, y: 4, rot: 0, tint: "white" },
      { id: "splitter-1", type: "splitter", x: 9, y: 7, rot: 1 },
      { id: "splitter-3", type: "splitter", x: 10, y: 1, rot: 0 },
      { id: "prism-2", type: "prism", x: 3, y: 1, rot: 1, tint: "white" },
      { id: "prism-4", type: "prism", x: 10, y: 3, rot: 0, tint: "white" }
    ],
    interiorWalls: [
      { x: 1, y: 1 },
      { x: 1, y: 2 },
      { x: 5, y: 2 },
      { x: 6, y: 2 },
      { x: 7, y: 2 },
      { x: 8, y: 2 }
    ]
  })
];
