// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

#include "wpi/halsim/gui/HALSimGui.hpp"

#include <memory>
#include <utility>

#include <imgui.h>

#include "wpi/glass/Context.hpp"
#include "wpi/glass/Storage.hpp"
#include "wpi/gui/wpigui.hpp"

using namespace halsimgui;

wpi::glass::MainMenuBar HALSimGui::mainMenu;
std::unique_ptr<wpi::glass::WindowManager> HALSimGui::manager;
std::unique_ptr<HALProvider> HALSimGui::halProvider;
std::unique_ptr<wpi::glass::NetworkTablesProvider> HALSimGui::ntProvider;

void HALSimGui::GlobalInit() {
  manager = std::make_unique<wpi::glass::WindowManager>(
      wpi::glass::GetStorageRoot().GetChild("SimWindow"));
  manager->GlobalInit();
  halProvider = std::make_unique<HALProvider>(
      wpi::glass::GetStorageRoot().GetChild("HALProvider"));
  halProvider->GlobalInit();
  ntProvider = std::make_unique<wpi::glass::NetworkTablesProvider>(
      wpi::glass::GetStorageRoot().GetChild("NTProvider"));
  ntProvider->GlobalInit();

  wpi::gui::AddLateExecute([] { mainMenu.Display(); });

  wpi::glass::AddStandardNetworkTablesViews(*ntProvider);
}

namespace halsimgui {

void AddGuiInit(std::function<void()> initialize) {
  wpi::gui::AddInit(std::move(initialize));
}

void AddGuiEarlyExecute(std::function<void()> execute) {
  wpi::gui::AddEarlyExecute(std::move(execute));
}

void AddGuiLateExecute(std::function<void()> execute) {
  wpi::gui::AddLateExecute(std::move(execute));
}

void GuiExit() {
  wpi::gui::Exit();
}

}  // namespace halsimgui
