#include "info.hpp"

#include "../../util/color.hpp"
#include "../../util/imgui.hpp"
#include "../../util/imgui/style.hpp"
#include "../../util/imgui/widget.hpp"
#include "../../util/measurement.hpp"
#include "../../util/vector.hpp"

using namespace game::util;
using namespace game::util::imgui;
using namespace game::util::measurement;
using namespace game::resource;

namespace game::state::select
{
  void Info::update(Resources& resources, int characterIndex)
  {
    if (!vector::in_bounds(resources.characterPreviews, characterIndex)) return;

    auto& style = ImGui::GetStyle();
    auto viewport = ImGui::GetMainViewport();
    auto* character = resources.characterPreviews[characterIndex].root();
    if (!character) return;

    auto size = ImVec2(viewport->Size.x / 2.0f - (style.WindowPadding.x * 2.0f),
                       (viewport->Size.y / 2.0f) - (style.WindowPadding.y * 2.0f));
    auto pos = ImVec2(style.WindowPadding.x, (viewport->Size.y / 2.0f) + style.WindowPadding.y);

    ImGui::SetNextWindowSize(size);
    ImGui::SetNextWindowPos(pos);
    imgui::style::color_set(character->color);

    if (ImGui::Begin("##Info", nullptr,
                     ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse |
                         ImGuiWindowFlags_NoTitleBar))
    {
      auto& save = resources.characterSaves.at(characterIndex);
      auto* settings = resources.settings.root();
      auto system = settings->measurementSystem == "Imperial" ? IMPERIAL : METRIC;
      auto* saveRoot = save.root();
      auto isSaveValid = saveRoot && saveRoot->type == resource::xml::Schema::Element::SAVE;
      auto* savedCharacter = isSaveValid ? save.child_get(*saveRoot, resource::xml::Schema::Element::CHARACTER) : nullptr;
      auto weight = savedCharacter ? (double)savedCharacter->weightKilograms : (double)character->weight;

      ImGui::PushFont(ImGui::GetFont(), Font::HEADER_3);

      auto childSize = imgui::size_without_footer_get();

      if (ImGui::BeginChild("##Info Child", childSize))
      {
        ImGui::PushFont(ImGui::GetFont(), Font::HEADER_3);
        ImGui::TextUnformatted(character->name.c_str());
        ImGui::PopFont();

        ImGui::PushFont(ImGui::GetFont(), Font::NORMAL);
        if (ImGui::BeginTabBar("##Preview Tabs"))
        {
          if (ImGui::BeginTabItem("Overview"))
          {
            if (!character->description.empty())
            {
              ImGui::Separator();

              ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetColorU32(imgui::to_imvec4(color::GRAY)));
              ImGui::PushFont(ImGui::GetFont(), Font::HEADER_2);
              ImGui::TextWrapped("%s", character->description.c_str());
              ImGui::PopFont();

              ImGui::PopStyleColor();
            }

            ImGui::Separator();

            ImGui::PushFont(ImGui::GetFont(), Font::HEADER_2);

            ImGui::Text("Weight: %0.2f %s", system == IMPERIAL ? weight * KG_TO_LB : weight,
                        system == IMPERIAL ? "lbs" : "kg");
            auto stages = resources.characterPreviews[characterIndex].get_all(resource::xml::Schema::Element::STAGE);
            ImGui::Text("Stages: %i", (int)stages.size() + 1);

            ImGui::PopFont();
            ImGui::EndTabItem();
          }

          if (ImGui::BeginTabItem("Credits"))
          {
            ImGui::Separator();
            ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetColorU32(imgui::to_imvec4(color::GRAY)));
            ImGui::PushFont(ImGui::GetFont(), Font::HEADER_2);
            if (!character->credits.empty())
              ImGui::TextWrapped("%s", character->credits.c_str());
            else
              ImGui::TextUnformatted("No credits listed.");
            ImGui::PopFont();
            ImGui::PopStyleColor();
            ImGui::EndTabItem();
          }

          ImGui::EndTabBar();
        }
        ImGui::PopFont();
      }
      ImGui::EndChild();

      auto widgetSize = row_widget_size_get(isSaveValid ? 2 : 1);

      if (isSaveValid)
      {
        if (WIDGET_FX(ImGui::Button("Continue", widgetSize))) isContinue = true;
        ImGui::PushFont(ImGui::GetFont(), Font::NORMAL);
        ImGui::SetItemTooltip("%s", "Continue from a saved game.");
        ImGui::PopFont();
        ImGui::SameLine();
      }

      if (WIDGET_FX(ImGui::Button("New Game", widgetSize)))
      {
        if (save.is_valid())
        {
          ImGui::OpenPopup("New Game Warning");
          isNewGameWarning = true;
        }
        else
          isNewGame = true;
      }
      ImGui::PushFont(ImGui::GetFont(), Font::NORMAL);
      ImGui::SetItemTooltip("%s", "Start a new game.\nThis will delete progress!");
      ImGui::PopFont();

      ImGui::PopFont();

      ImGui::SetNextWindowSize(ImVec2(viewport->Size.x * 0.5f, 0), ImGuiCond_Always);
      ImGui::SetNextWindowPos(ImVec2(viewport->GetCenter()), ImGuiCond_Always, ImVec2(0.5f, 0.5f));

      if (ImGui::BeginPopupModal("New Game Warning", &isNewGameWarning,
                                 ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize))
      {
        auto popupWidgetSize = row_widget_size_get(isSaveValid ? 2 : 1);
        ImGui::TextWrapped("This will delete saved progress! Are you sure?");
        if (WIDGET_FX(ImGui::Button("Yes", popupWidgetSize))) isNewGame = true;
        ImGui::SameLine();
        if (WIDGET_FX(ImGui::Button("No", popupWidgetSize))) isNewGameWarning = false;
        ImGui::EndPopup();
      }
    }
    ImGui::End();
    imgui::style::color_set(resources.settings.root()->color);
  }
}
