function wallRect(x, y, w, h) {
  const cells = [];
  for (let j = 0; j < h; j++) for (let i = 0; i < w; i++) cells.push([x + i, y + j]);
  return cells;
}

function scatter(list) {
  return list;
}

export const LEVELS = [
  {
    id: 1,
    name: 'First Pressure',
    blurb: 'Lay a path. The flooz waits for no one.',
    cols: 7,
    rows: 7,
    delay: 16,
    speed: 0.52,
    minDist: 8,
    start: { x: 0, y: 3, dir: 'E' },
    end: null,
    wrap: false,
    walls: [],
    crosses: false,
    tanks: false,
    oneWays: false,
    tutorial: true,
  },
  {
    id: 2,
    name: 'Elbow Room',
    blurb: 'Corners are not optional.',
    cols: 7,
    rows: 7,
    delay: 14,
    speed: 0.6,
    minDist: 10,
    start: { x: 3, y: 0, dir: 'S' },
    end: null,
    wrap: false,
    walls: scatter([
      [1, 1],
      [5, 1],
      [1, 5],
      [5, 5],
    ]),
    crosses: false,
    tanks: false,
    oneWays: false,
  },
  {
    id: 3,
    name: 'Cross Traffic',
    blurb: 'A junction carries two lives. Use both.',
    cols: 8,
    rows: 7,
    delay: 13,
    speed: 0.68,
    minDist: 12,
    start: { x: 0, y: 3, dir: 'E' },
    end: null,
    wrap: false,
    walls: [
      [3, 1],
      [4, 1],
      [3, 5],
      [4, 5],
    ],
    crosses: true,
    tanks: false,
    oneWays: false,
  },
  {
    id: 4,
    name: 'Blocked Arteries',
    blurb: 'The plant was not built for you.',
    cols: 8,
    rows: 8,
    delay: 12,
    speed: 0.74,
    minDist: 14,
    start: { x: 0, y: 1, dir: 'E' },
    end: null,
    wrap: false,
    walls: [
      ...wallRect(3, 0, 1, 3),
      ...wallRect(5, 5, 1, 3),
      [2, 4],
      [6, 3],
    ],
    crosses: true,
    tanks: false,
    oneWays: false,
  },
  {
    id: 5,
    name: 'The Sink',
    blurb: 'Reach the drain. Length still counts.',
    cols: 8,
    rows: 8,
    delay: 12,
    speed: 0.78,
    minDist: 14,
    start: { x: 0, y: 2, dir: 'E' },
    end: { x: 7, y: 5, dir: 'W' },
    wrap: false,
    walls: [
      [3, 3],
      [4, 3],
      [3, 4],
      [4, 4],
    ],
    crosses: true,
    tanks: false,
    oneWays: false,
  },
  {
    id: 6,
    name: 'Holding Tanks',
    blurb: 'Reservoirs buy seconds. Spend them.',
    cols: 9,
    rows: 8,
    delay: 11,
    speed: 0.84,
    minDist: 16,
    start: { x: 0, y: 4, dir: 'E' },
    end: { x: 8, y: 3, dir: 'W' },
    wrap: false,
    walls: [
      [4, 0],
      [4, 1],
      [4, 6],
      [4, 7],
      [2, 3],
      [6, 4],
    ],
    crosses: true,
    tanks: true,
    oneWays: false,
  },
  {
    id: 7,
    name: 'Tight Quarters',
    blurb: 'Less time. Same laws of plumbing.',
    cols: 9,
    rows: 9,
    delay: 10,
    speed: 0.9,
    minDist: 18,
    start: { x: 4, y: 0, dir: 'S' },
    end: null,
    wrap: false,
    walls: [
      [1, 2],
      [2, 2],
      [6, 2],
      [7, 2],
      [1, 6],
      [2, 6],
      [6, 6],
      [7, 6],
      [4, 4],
    ],
    crosses: true,
    tanks: true,
    oneWays: false,
  },
  {
    id: 8,
    name: 'Möbius Floor',
    blurb: 'Edges are suggestions. Wrap the grid.',
    cols: 9,
    rows: 9,
    delay: 10,
    speed: 0.95,
    minDist: 18,
    start: { x: 0, y: 4, dir: 'E' },
    end: null,
    wrap: true,
    walls: [
      [4, 1],
      [4, 2],
      [4, 6],
      [4, 7],
      [1, 4],
      [2, 4],
      [6, 4],
      [7, 4],
    ],
    crosses: true,
    tanks: true,
    oneWays: false,
  },
  {
    id: 9,
    name: 'One-Way Street',
    blurb: 'Arrows do not negotiate.',
    cols: 9,
    rows: 9,
    delay: 9,
    speed: 1.02,
    minDist: 20,
    start: { x: 0, y: 1, dir: 'E' },
    end: { x: 8, y: 7, dir: 'W' },
    wrap: false,
    walls: [
      [4, 0],
      [4, 1],
      [4, 2],
      [4, 6],
      [4, 7],
      [4, 8],
      [2, 4],
      [6, 4],
    ],
    crosses: true,
    tanks: true,
    oneWays: true,
  },
  {
    id: 10,
    name: 'Rush Job',
    blurb: 'The valve is already turning.',
    cols: 10,
    rows: 9,
    delay: 8,
    speed: 1.16,
    minDist: 22,
    start: { x: 0, y: 4, dir: 'E' },
    end: { x: 9, y: 4, dir: 'W' },
    wrap: false,
    walls: [
      [3, 1],
      [3, 2],
      [6, 6],
      [6, 7],
      [5, 0],
      [5, 8],
      [2, 5],
      [7, 3],
    ],
    crosses: true,
    tanks: true,
    oneWays: true,
  },
  {
    id: 11,
    name: 'The Labyrinth',
    blurb: 'A long way through a small building.',
    cols: 10,
    rows: 10,
    delay: 8,
    speed: 1.22,
    minDist: 24,
    start: { x: 0, y: 0, dir: 'S' },
    end: { x: 9, y: 9, dir: 'N' },
    wrap: true,
    walls: [
      ...wallRect(2, 2, 2, 1),
      ...wallRect(6, 2, 2, 1),
      ...wallRect(2, 7, 2, 1),
      ...wallRect(6, 7, 2, 1),
      [5, 4],
      [5, 5],
      [4, 5],
      [1, 5],
      [8, 4],
    ],
    crosses: true,
    tanks: true,
    oneWays: true,
  },
  {
    id: 12,
    name: 'Overload',
    blurb: 'Everything you learned. At once. Faster.',
    cols: 10,
    rows: 10,
    delay: 7,
    speed: 1.38,
    minDist: 28,
    start: { x: 0, y: 5, dir: 'E' },
    end: { x: 9, y: 4, dir: 'W' },
    wrap: true,
    walls: [
      [2, 1],
      [2, 2],
      [2, 3],
      [7, 6],
      [7, 7],
      [7, 8],
      [4, 4],
      [5, 5],
      [4, 8],
      [5, 1],
      [8, 2],
      [1, 7],
    ],
    crosses: true,
    tanks: true,
    oneWays: true,
  },
];

export const DIFFICULTY = {
  casual: { delay: 1.35, speed: 0.78, min: 0.85, label: 'Casual' },
  normal: { delay: 1, speed: 1, min: 1, label: 'Normal' },
  expert: { delay: 0.72, speed: 1.28, min: 1.12, label: 'Expert' },
};

export function applyDifficulty(level, diffKey) {
  const d = DIFFICULTY[diffKey] || DIFFICULTY.normal;
  return {
    ...level,
    delay: Math.max(4, level.delay * d.delay),
    speed: level.speed * d.speed,
    minDist: Math.max(5, Math.round(level.minDist * d.min)),
  };
}

export function medalFor(score, par) {
  if (score >= par * 1.35) return 'gold';
  if (score >= par * 0.95) return 'silver';
  if (score >= par * 0.55) return 'bronze';
  return 'none';
}

export function parScore(level) {
  return level.minDist * 120 + 800;
}
