#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include "bucket_finder.h"
#include "finder.h"
#include "grouped_finder.h"
#include "linear_finder.h"
#include "parallel_finder.h"
#include "sorted_linear_finder.h"
#include "timer.h"
#include "tree_finder.h"

#include <sstream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);

  ui->resultView->setModel(&result_model_);
  ui->wordListSourceSelector->setModel(&word_list_sources_);
  ui->searchAlgorithmSelector->setModel(&search_algorithms_);

  setupAlgorithmSelector();
  setupWordListSourceSelector();
  setupWordList();
}

MainWindow::~MainWindow() { delete ui; }

void MainWindow::setupAlgorithmSelector() {
  search_algorithms_.appendRow(new QStandardItem("Linear search"));
  search_algorithms_.appendRow(new QStandardItem("Sorted linear search"));
  search_algorithms_.appendRow(new QStandardItem("Parallel search"));
  search_algorithms_.appendRow(new QStandardItem("Tree search"));
  search_algorithms_.appendRow(new QStandardItem("Grouped search"));
  search_algorithms_.appendRow(new QStandardItem("Bucket search"));
}

void MainWindow::setupWordListSourceSelector() {
  word_list_sources_.appendRow(new QStandardItem("All 4-caps words"));
  word_list_sources_.appendRow(new QStandardItem("English words"));
}

void MainWindow::setupWordList() {
  switch (ui->wordListSourceSelector->currentIndex()) {
  case 0:
  default:
    generateWordList();
    break;
  case 1:
    loadWordList("words.txt");
    break;
  }
}

void MainWindow::generateWordList() {
  Timer timer;
  word_list_ = WordList::fourCaps()
                   .multiply(ui->wordListSizeSelector->value())
                   .shuffle();
  timer.stop();

  std::stringstream status_message;
  status_message << "generated " << word_list_.size() << " words in " << timer;
  ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
}

void MainWindow::loadWordList(std::filesystem::path path) {
  Timer timer;
  word_list_ = WordList::fromFile("words.txt")
                   .multiply(ui->wordListSizeSelector->value())
                   .shuffle();
  timer.stop();

  std::stringstream status_message;
  status_message << "loaded " << word_list_.size() << " words in " << timer;
  ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
}

void MainWindow::search(const QString &search_term) {
  if (search_term.isEmpty()) {
    result_model_.setStringList(QStringList());
    ui->mainStatusBar->clearMessage();
    return;
  }

  Timer timer;
  const auto results = finder_->find_prefix(search_term.toStdString());
  timer.stop();

  std::stringstream status_message;
  status_message << "search took " << timer << ", found "
                 << std::distance(results.begin(), results.end()) << " results";
  ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));

  showResults(results);
}

void MainWindow::createSelectedFinder() {
  auto selectedFinder = ui->searchAlgorithmSelector->currentIndex();

  switch (selectedFinder) {
  case 0:
  default:
    finder_ = std::make_unique<LinearFinder>(word_list_);
    break;
  case 1:
    finder_ = std::make_unique<SortedLinearFinder>(word_list_);
    break;
  case 2:
    finder_ = std::make_unique<ParallelFinder>(word_list_);
    break;
  case 3:
    finder_ = std::make_unique<TreeFinder>(word_list_);
    break;
  case 4:
    finder_ = std::make_unique<GroupedFinder>(word_list_);
    break;
  case 5:
    finder_ = std::make_unique<BucketFinder>(word_list_);
    break;
  }
}

void MainWindow::showResults(
    const std::forward_list<const std::string *> &results) {
  QStringList ui_results;
  for (auto word : results) {
    ui_results.append(QString::fromStdString(*word));
  }

  result_model_.setStringList(ui_results);
}

void MainWindow::on_searchInput_textChanged(const QString &search_term) {
  QGuiApplication::setOverrideCursor(Qt::WaitCursor);
  search(search_term);
  QGuiApplication::restoreOverrideCursor();
}

void MainWindow::on_searchAlgorithmSelector_currentIndexChanged(int) {
  QGuiApplication::setOverrideCursor(Qt::WaitCursor);
  createSelectedFinder();
  search(ui->searchInput->displayText());
  QGuiApplication::restoreOverrideCursor();
}

void MainWindow::on_wordListSourceSelector_currentIndexChanged(int) {
  QGuiApplication::setOverrideCursor(Qt::WaitCursor);
  setupWordList();
  createSelectedFinder();
  search(ui->searchInput->displayText());
  QGuiApplication::restoreOverrideCursor();
}

void MainWindow::on_wordListSizeSelector_valueChanged(int) {
  QGuiApplication::setOverrideCursor(Qt::WaitCursor);
  setupWordList();
  createSelectedFinder();
  search(ui->searchInput->displayText());
  QGuiApplication::restoreOverrideCursor();
}
