#include "EngineTab.hpp"

EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine,
                     std::string engine_path_or_name)
    : TabEngine(parent), TabInfos(TabInfos::ENGINE),
      enginePath(engine_path_or_name), engine(engine) {
  SetLabel("New Engine");

  engine_location->SetValue(engine_path_or_name);
  CONFIG_OPEN(conf);
  // conf->DeleteGroup(confGroup);
  engineName = "NewEngine";
  confGroup = "engines/" + engineName;
  std::uint32_t key = 2;
  while (conf->HasGroup(confGroup)) {
    engineName = "NewEngine" + std::to_string(key);
    confGroup = "engines/" + engineName;
    key++;
  }
  engine_name->SetValue(engineName);

  // conf->Write(confGroup + "/path", wxString(engine_path_or_name));
  CONFIG_CLOSE(conf);
  InitConfiguration();
  LoadConfiguration();
  RefreshItemList();

  Bind(wxEVT_BUTTON, &EngineTab::OnSave, this, ENGINE_SAVE_CONF_BUTTON);
  Bind(wxEVT_BUTTON, &EngineTab::OnDelete, this, ENGINE_DELETE_CONF_BUTTON);
}

EngineTab::EngineTab(wxWindow *parent, std::string name)
    : TabEngine(parent), TabInfos(TabInfos::ENGINE), engine(nullptr) {
  SetLabel(name);
  engineName = name;
  confGroup = "engines/" + engineName;
  engine_name->SetValue(engineName);
  CONFIG_OPEN(conf);
  engine_location->SetValue(conf->Read(confGroup + "/path"));
  CONFIG_CLOSE(conf);
  LoadConfiguration();

  Bind(wxEVT_BUTTON, &EngineTab::OnSave, this, ENGINE_SAVE_CONF_BUTTON);
  Bind(wxEVT_BUTTON, &EngineTab::OnDelete, this, ENGINE_DELETE_CONF_BUTTON);
}

EngineTab::~EngineTab() {
  if (engine != nullptr) {
    wxLogDebug("EngineTab destructor: destroying engine!");
    engine->quit();
    delete engine;
  }
}

void EngineTab::OnDelete(wxCommandEvent &event) {
  CONFIG_OPEN(conf);
  conf->DeleteGroup(confGroup);
  CONFIG_CLOSE(conf);
  RefreshItemList();

  wxCommandEvent closeTabEvent(CLOSE_TAB_EVENT, GetId());
  closeTabEvent.SetEventObject(this);
  ProcessEvent(closeTabEvent);
}

void EngineTab::RefreshItemList() {
  wxCommandEvent refreshEngineList(REFRESH_ENGINE_LIST, GetId());
  refreshEngineList.SetEventObject(this);
  ProcessEvent(refreshEngineList);
}

void EngineTab::LoadConfiguration() {
  // Build wxPropertyGrid according to engine configuration
  CONFIG_OPEN(conf2);
  long index;
  std::string optsPath = confGroup + "/options";
  conf2->SetPath(optsPath);
  wxString opt_name;
  if (conf2->GetFirstGroup(opt_name, index)) {
    do {
      wxString optPath = opt_name + "/";
      wxString type = conf2->Read(optPath + "type");
      wxString default_value_wxString = conf2->Read(optPath + "value");
      std::string default_value = default_value_wxString.ToStdString();
      if (type == "check") {
        engine_parameters->Append(
            new wxBoolProperty(opt_name, wxPG_LABEL, default_value == "true"));
      } else if (type == "spin") {
        engine_parameters->Append(
            new wxIntProperty(opt_name, wxPG_LABEL, std::stoi(default_value)));
      } else if (type == "string" || type == "button") {
        engine_parameters->Append(
            new wxStringProperty(opt_name, wxPG_LABEL, default_value));
      }
    } while (conf2->GetNextGroup(opt_name, index));
  }
  CONFIG_CLOSE(conf2);
}

void EngineTab::OnSave(wxCommandEvent &event) {
  CONFIG_OPEN(conf2);
  wxString new_engine_name = engine_name->GetValue();
  if (new_engine_name != engineName) {
    conf2->SetPath("engines/");
    conf2->RenameGroup(engineName, new_engine_name);
    engineName = new_engine_name;
    confGroup = "engines/" + engineName;
    conf2->SetPath("..");
  }
  long index;
  std::string optsPath = confGroup + "/options";
  conf2->SetPath(optsPath);
  wxString opt_name;
  if (conf2->GetFirstGroup(opt_name, index)) {
    do {
      wxString optPath = opt_name + "/";
      wxString type = conf2->Read(optPath + "type");
      wxPGProperty *property = engine_parameters->GetProperty(opt_name);
      wxVariant value = property->GetValue();
      if (value.IsType(wxPG_VARIANT_TYPE_BOOL)) {
        conf2->Write(optPath + "/value", value.GetBool());
      } else if (value.IsType(wxPG_VARIANT_TYPE_LONG)) {
        conf2->Write(optPath + "/value", value.GetLong());
      } else if (value.IsType(wxPG_VARIANT_TYPE_STRING)) {
        conf2->Write(optPath + "/value", value.GetString());
      }
    } while (conf2->GetNextGroup(opt_name, index));
  }
  CONFIG_CLOSE(conf2);
  RefreshItemList();
}

void EngineTab::InitConfiguration() {
  wxLogDebug("Called!");
  CONFIG_OPEN(conf);
  conf->Write(confGroup + "/path", wxString(enginePath));
  conf->Write(confGroup + "/name", wxString(engine->GetName()));
  conf->Write(confGroup + "/authors", wxString(engine->GetAuthor()));
  std::vector<uciadapter::Option> opts = engine->GetOptions();
  for (uciadapter::Option &opt : opts) {
    std::string optPath = confGroup + "/options/" + opt.name;
    conf->Write(wxString(optPath + "/type"), wxString(opt.type));
    if (opt.type == "check") {
      conf->Write(wxString(optPath + "/value"), opt.default_value == "true");
    } else if (opt.type == "spin") {
      conf->Write(wxString(optPath + "/value"), std::stoi(opt.default_value));
      conf->Write(wxString(optPath + "/min"), std::stoi(opt.min));
      conf->Write(wxString(optPath + "/max"), std::stoi(opt.max));
    } else if (opt.type == "string") {
      conf->Write(wxString(optPath + "/value"), wxString(opt.default_value));
    } else if (opt.type == "button") {
      conf->Write(wxString(optPath + "/name"), wxString(opt.default_value));
    }
  }
  CONFIG_CLOSE(conf);
}