#include "factory/core/puzzles.h"
#include "factory/desktop/main_window.h"
#include <QAction>
#include <QComboBox>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>
#include <QTimer>
#include <QtTest>

using namespace factory;
class DesktopTests : public QObject {
  Q_OBJECT
private slots:
  void initTestCase() {
    QCoreApplication::setOrganizationName("FactoryForgeTests");
    QCoreApplication::setApplicationName("DesktopTests");
    QSettings().clear();
  }
  void paintUndoRedoAndProtectStations() {
    MainWindow window;
    window.show();
    QTest::qWait(30);
    auto* view = window.board();
    const auto click = [&](Position p) {
      const auto point = view->mapFromScene(p.x * 36 + 18, p.y * 36 + 18);
      QTest::mouseClick(view->viewport(), Qt::LeftButton, Qt::NoModifier, point);
    };
    click({3, 3});
    QCOMPARE(window.document().project().tile({3, 3}), Tile::Road);
    QVERIFY(window.document().dirty());
    window.findChild<QAction*>("undo")->trigger();
    QCOMPARE(window.document().project().tile({3, 3}), Tile::Floor);
    window.findChild<QAction*>("redo")->trigger();
    QCOMPARE(window.document().project().tile({3, 3}), Tile::Road);
    click({1, 3});
    QCOMPARE(window.document().project().tile({1, 3}), Tile::Source);
    window.setProject(makePuzzle(0));
  }
  void simulationFreezesEditorAndAllowsSingleSteps() {
    MainWindow window;
    window.setProject(makePuzzle(0, true));
    QVERIFY(window.startSimulation(false));
    window.simulation().setPlaying(false);
    window.refresh();
    QVERIFY(!window.findChild<QComboBox*>("tools")->isEnabled());
    QTest::mouseClick(window.findChild<QPushButton*>("step"), Qt::LeftButton);
    QCOMPARE(window.simulation().snapshot().tick, 1);
    QTest::mouseClick(window.findChild<QPushButton*>("edit"), Qt::LeftButton);
    QVERIFY(!window.simulation().active());
    QVERIFY(window.findChild<QComboBox*>("tools")->isEnabled());
  }
  void languageSwitchPreservesDraft() {
    MainWindow window;
    window.document().edit([](Project& p) { p.name = "Keep me"; });
    window.setGerman(false);
    QCOMPARE(window.findChild<QAction*>("undo")->text(), QString("Undo"));
    window.setGerman(true);
    QCOMPARE(window.document().project().name, std::string("Keep me"));
    QVERIFY(window.document().dirty());
    window.setProject(makePuzzle(0));
  }
  void invalidLayoutCannotStart() {
    MainWindow window;
    QVERIFY(!window.startSimulation(false));
    QVERIFY(!window.simulation().active());
  }
  void typingMarksDirtyAndCancellingClosePreservesInput() {
    MainWindow window;
    window.setProject(Project{});
    window.show();
    auto* name = window.findChild<QLineEdit*>("projectName");
    name->setFocus();
    name->selectAll();
    QTest::keyClicks(name, "Unsaved warehouse");
    QVERIFY(window.document().dirty());
    QTimer::singleShot(0, [] {
      auto* dialog = qobject_cast<QMessageBox*>(QApplication::activeModalWidget());
      if (dialog)
        dialog->done(QMessageBox::Cancel);
    });
    QVERIFY(!window.close());
    QCOMPARE(window.document().project().name, std::string("Unsaved warehouse"));
    window.setProject(Project{});
  }
};
QTEST_MAIN(DesktopTests)
#include "desktop_test.moc"
