#include "factory/core/puzzles.h"
#include <array>
#include <stdexcept>

namespace factory {
std::string puzzleTitle(int index, bool german) {
  constexpr std::array en{"First delivery", "Around the obstacle", "Two loading bays",
                          "Busy crossing", "Warehouse bottleneck"};
  constexpr std::array de{"Erste Lieferung", "Um das Hindernis", "Zwei Ladeplätze",
                          "Belebte Kreuzung", "Engpass im Lager"};
  return (german ? de : en).at(static_cast<std::size_t>(index));
}
Project makePuzzle(int index, bool solution) {
  if (index < 0 || index >= PuzzleCount)
    throw std::out_of_range("Puzzle index");
  Project p;
  p.name = puzzleTitle(index, false);
  p.width = 16;
  p.height = 12;
  p.tiles.assign(static_cast<std::size_t>(p.width) * static_cast<std::size_t>(p.height),
                 Tile::Floor);
  p.puzzleId = index;
  p.tickLimit = 300 + index * 100;
  p.roadBudget = 38 + index * 12;
  p.vehicleBudget = index >= 2 ? 2 : 1;
  const int lanes = index >= 2 ? 2 : 1;
  for (int lane = 0; lane < lanes; ++lane) {
    const int y = 3 + lane * 5;
    p.setTile({1, y}, Tile::Source);
    p.setTile({14, y}, Tile::Sink);
    p.setTile({2, y + 1}, Tile::Road);
    p.vehicles.push_back({lane + 1, {2, y + 1}});
    for (int order = 0; order < 3 + index; ++order)
      p.orders.push_back({static_cast<int>(p.orders.size()) + 1, {1, y}, {14, y}});
    if (solution) {
      for (int x = 2; x < 14; ++x)
        p.setTile({x, y}, Tile::Road);
      // A parallel return lane and side access keep waiting vehicles out of the loading ports.
      for (int x = 1; x <= 14; ++x)
        p.setTile({x, y + 1}, Tile::Road);
    }
    if (index == 1 || index == 4) {
      p.setTile({7, y}, Tile::Wall);
      if (solution)
        for (int x = 6; x <= 8; ++x)
          p.setTile({x, y - 1}, Tile::Road);
    }
  }
  if (index == 3 && solution) {
    for (int y = 3; y <= 8; ++y)
      p.setTile({8, y}, Tile::Road);
    // Opposite-lane deliveries exercise the shared crossing.
  }
  if (index == 3) {
    for (auto& order : p.orders)
      order.delivery.y = order.pickup.y == 3 ? 8 : 3;
  }
  if (index == 4) {
    p.setTile({10, 3}, Tile::Storage);
    p.orders.push_back({static_cast<int>(p.orders.size()) + 1, {10, 3}, {14, 8}});
    if (solution)
      for (int y = 4; y < 8; ++y)
        p.setTile({11, y}, Tile::Road);
  }
  return p;
}
bool isOfficialPuzzle(const Project& p) {
  if (p.puzzleId < 0 || p.puzzleId >= PuzzleCount)
    return false;
  const auto base = makePuzzle(p.puzzleId);
  if (p.width != base.width || p.height != base.height || p.orders != base.orders ||
      p.tickLimit != base.tickLimit || p.roadBudget != base.roadBudget ||
      p.vehicleBudget != base.vehicleBudget || p.tiles.size() != base.tiles.size())
    return false;
  for (int y = 0; y < p.height; ++y)
    for (int x = 0; x < p.width; ++x) {
      const Position cell{x, y};
      if (base.tile(cell) != Tile::Floor && base.tile(cell) != Tile::Road &&
          p.tile(cell) != base.tile(cell))
        return false;
      if ((base.tile(cell) == Tile::Floor || base.tile(cell) == Tile::Road) &&
          p.tile(cell) != Tile::Floor && p.tile(cell) != Tile::Road)
        return false;
    }
  return true;
}
} // namespace factory
