#include "factory/core/pathfinder.h"
#include "factory/core/puzzles.h"
#include "factory/core/simulation.h"
#include <gtest/gtest.h>
#include <set>

using namespace factory;

TEST(Project, RejectsMalformedAndDuplicateVehicles) {
  auto p = makePuzzle(0, true);
  EXPECT_TRUE(p.validate().empty());
  p.vehicles.push_back(p.vehicles.front());
  EXPECT_FALSE(p.validate().empty());
  p.width = 129;
  EXPECT_FALSE(p.validate().empty());
}
TEST(Project, RejectsBudgetAndInvalidOrderEndpoints) {
  auto p = makePuzzle(0, true);
  p.roadBudget = 0;
  EXPECT_FALSE(p.validate().empty());
  p.roadBudget = 200;
  p.orders.front().pickup = {0, 0};
  EXPECT_FALSE(p.validate().empty());
}
TEST(Pathfinder, ShortestPathIncludesBothEndpoints) {
  auto p = makePuzzle(0, true);
  const auto path = findPath(p, {1, 3}, {14, 3});
  ASSERT_EQ(path.size(), 14U);
  EXPECT_EQ(path.front(), (Position{1, 3}));
  EXPECT_EQ(path.back(), (Position{14, 3}));
}
TEST(Pathfinder, GoesAroundWallAndRejectsDisconnectedTarget) {
  auto p = makePuzzle(1, true);
  const auto path = findPath(p, {1, 3}, {14, 3});
  EXPECT_FALSE(path.empty());
  for (auto pos : path)
    EXPECT_NE(p.tile(pos), Tile::Wall);
  EXPECT_TRUE(findPath(makePuzzle(0), {1, 3}, {14, 3}).empty());
}
TEST(Reservations, BlocksVertexAndOppositeEdge) {
  Reservations r;
  r.reserve({{1, 3}, {2, 3}}, 12);
  EXPECT_FALSE(r.available({3, 3}, {2, 3}, 1));
  EXPECT_FALSE(r.available({2, 3}, {1, 3}, 1));
  EXPECT_TRUE(r.available({4, 4}, {5, 4}, 1));
}
TEST(Reservations, DistinguishesCoordinatesAndLogicalTimesAtMapLimit) {
  Reservations r;
  r.reserve({{127, 127}, {126, 127}}, 12);
  EXPECT_FALSE(r.available({125, 127}, {126, 127}, 1));
  EXPECT_TRUE(r.available({125, 126}, {126, 126}, 1));
  EXPECT_TRUE(r.available({125, 127}, {126, 127}, 13));
  EXPECT_FALSE(r.available({126, 127}, {127, 127}, 1));
}
TEST(Simulation, AllFiveSolutionsWinWithoutCollisions) {
  for (int level = 0; level < PuzzleCount; ++level) {
    auto p = makePuzzle(level, true);
    SCOPED_TRACE(level);
    ASSERT_TRUE(p.validate().empty());
    Simulation sim(p);
    while (sim.snapshot().outcome == Outcome::Running) {
      const auto previous = sim.snapshot().vehicles;
      sim.step();
      std::set<Position> occupied;
      for (std::size_t i = 0; i < sim.snapshot().vehicles.size(); ++i) {
        const auto current = sim.snapshot().vehicles[i].position;
        EXPECT_TRUE(occupied.insert(current).second);
        for (std::size_t j = i + 1; j < previous.size(); ++j)
          EXPECT_FALSE(current == previous[j].position &&
                       sim.snapshot().vehicles[j].position == previous[i].position);
      }
    }
    EXPECT_EQ(sim.snapshot().outcome, Outcome::Won);
    EXPECT_EQ(sim.snapshot().delivered, static_cast<int>(p.orders.size()));
  }
}
TEST(Simulation, SpeedAndRepeatedRunsDoNotChangeResults) {
  Simulation a(makePuzzle(3, true)), b(makePuzzle(3, true));
  while (a.snapshot().outcome == Outcome::Running)
    a.step();
  while (b.snapshot().outcome == Outcome::Running)
    for (int n = 0; n < 4; ++n)
      b.step();
  EXPECT_EQ(a.snapshot().tick, b.snapshot().tick);
  EXPECT_EQ(a.snapshot().waitingTicks, b.snapshot().waitingTicks);
}
TEST(Simulation, UnreachableOrdersLoseWithDiagnostics) {
  Simulation sim(makePuzzle(0));
  while (sim.snapshot().outcome == Outcome::Running)
    sim.step();
  EXPECT_EQ(sim.snapshot().outcome, Outcome::Lost);
  EXPECT_EQ(sim.snapshot().delivered, 0);
  EXPECT_FALSE(sim.snapshot().diagnostics.empty());
}
TEST(Puzzle, ModifiedObjectivesDoNotAwardOfficialProgress) {
  auto p = makePuzzle(0, true);
  EXPECT_TRUE(isOfficialPuzzle(p));
  p.tickLimit *= 2;
  EXPECT_FALSE(isOfficialPuzzle(p));
}
