// MassBuilderSaveTool
// Copyright (C) 2021-2024 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#include <Corrade/version.h>

#include <Magnum/version.h>
#include <Magnum/versionIntegration.h>
#include <Magnum/ImGuiIntegration/Integration.h>

#include <zipconf.h>

#include <curl/curlver.h>

#include "../FontAwesome/IconsFontAwesome5.h"
#include "../FontAwesome/IconsFontAwesome5Brands.h"

#include "Application.h"

extern const ImVec2 center_pivot;

namespace mbst {

void
Application::drawAbout() {
    ImGui::SetNextWindowPos(ImVec2{Vector2{windowSize() / 2.0f}}, ImGuiCond_Always, center_pivot);
    ImGui::SetNextWindowSize({float(windowSize().x()) * 0.8f, float(windowSize().y()) * 0.75f}, ImGuiCond_Always);

    ImGui::OpenPopup("About##AboutPopup");
    if(!ImGui::BeginPopupModal("About##AboutPopup", &_aboutPopup,
                               ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoCollapse))
    {
        return;
    }

    if(ImGui::BeginTable("##TitleTable", 3)) {
        ImGui::TableSetupColumn("##Empty1", ImGuiTableColumnFlags_WidthStretch);
        ImGui::TableSetupColumn("##Button", ImGuiTableColumnFlags_WidthFixed);
        ImGui::TableSetupColumn("##Empty2", ImGuiTableColumnFlags_WidthStretch);

        ImGui::TableNextRow();
        ImGui::TableSetColumnIndex(1);
        ImGui::TextUnformatted(SDL_GetWindowTitle(window()));

        ImGui::EndTable();
    }

    ImGui::Dummy({0.0f, ImGui::GetFontSize()});

    ImGui::TextWrapped("This application, made for the M.A.S.S. Builder community by Guillaume Jacquemin (aka William JCM), "
                       "is a rewrite of the wxWidgets-powered M.A.S.S. Builder Save Tool (formerly known as wxMASSManager).");

    auto website = "https://williamjcm.ovh/mbst";
    drawAlignedText(ICON_FA_GLOBE " %s", website);
    ImGui::SameLine();
    if(ImGui::Button("Copy to clipboard")) {
        ImGui::SetClipboardText(website);
    }
    ImGui::SameLine();
    if(ImGui::Button("Open in browser")) {
        openUri(website);
    }
    auto repo = "https://git.williamjcm.ovh/williamjcm/MassBuilderSaveTool";
    drawAlignedText(ICON_FA_GIT_ALT " %s", repo);
    ImGui::SameLine();
    if(ImGui::Button("Copy to clipboard")) {
        ImGui::SetClipboardText(repo);
    }
    ImGui::SameLine();
    if(ImGui::Button("Open in browser")) {
        openUri(repo);
    }

    ImGui::Separator();

    if(ImGui::CollapsingHeader("Licence")) {
        ImGui::TextWrapped("This application is made available under the terms of the GNU General Public License,"
                           "version 3, the full text of which is available below:");

        if(ImGui::BeginChild("##GPL", {0.0f, float(windowSize().y()) * 0.3f}, ImGuiChildFlags_Border)) {
            static auto licence = _rs.getRaw("COPYING");
            ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[1]);
            ImGui::TextUnformatted(licence.cbegin(), licence.cend());
            ImGui::PopFont();
        }
        ImGui::EndChild();
    }

    if(ImGui::CollapsingHeader("Third-party components")) {
        ImGui::TextWrapped("This application uses the following third-party components:");

        ImGui::PushStyleVar(ImGuiStyleVar_IndentSpacing, 0.0f);

        if(ImGui::TreeNodeEx("Corrade", ImGuiTreeNodeFlags_SpanAvailWidth)) {
            ImGui::Text("Version used: %s", CORRADE_VERSION_STRING);
            auto corrade_website = "https://magnum.graphics/corrade";
            drawAlignedText(ICON_FA_GLOBE " %s", corrade_website);
            ImGui::SameLine();
            if(ImGui::Button("Copy to clipboard")) {
                ImGui::SetClipboardText(corrade_website);
            }
            ImGui::SameLine();
            if(ImGui::Button("Open in browser")) {
                openUri(corrade_website);
            }

            ImGui::TextUnformatted("Licence: MIT");

            ImGui::TreePop();
        }

        if(ImGui::TreeNodeEx("Magnum and integration libraries", ImGuiTreeNodeFlags_SpanAvailWidth)) {
            ImGui::TextUnformatted("Versions used:");
            ImGui::BulletText("Magnum: %s", MAGNUM_VERSION_STRING);
            ImGui::BulletText("Integration: %s", MAGNUMINTEGRATION_VERSION_STRING);
            auto magnum_website = "https://magnum.graphics";
            drawAlignedText(ICON_FA_GLOBE " %s", magnum_website);
            ImGui::SameLine();
            if(ImGui::Button("Copy to clipboard")) {
                ImGui::SetClipboardText(magnum_website);
            }
            ImGui::SameLine();
            if(ImGui::Button("Open in browser")) {
                openUri(magnum_website);
            }

            ImGui::TextUnformatted("Licence: MIT");

            ImGui::TreePop();
        }

        if(ImGui::TreeNodeEx("Dear ImGui", ImGuiTreeNodeFlags_SpanAvailWidth)) {
            ImGui::Text("Version used: %s", IMGUI_VERSION);
            auto imgui_repo = "https://github.com/ocornut/imgui";
            drawAlignedText(ICON_FA_GITHUB " %s", imgui_repo);
            ImGui::SameLine();
            if(ImGui::Button("Copy to clipboard")) {
                ImGui::SetClipboardText(imgui_repo);
            }
            ImGui::SameLine();
            if(ImGui::Button("Open in browser")) {
                openUri(imgui_repo);
            }

            ImGui::TextUnformatted("Licence: MIT");

            ImGui::TreePop();
        }

        if(ImGui::TreeNodeEx("Simple DirectMedia Layer (SDL) 2", ImGuiTreeNodeFlags_SpanAvailWidth)) {
            ImGui::Text("Version used: %i.%i.%i", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
            auto sdl_website = "https://www.libsdl.org/";
            drawAlignedText(ICON_FA_GLOBE " %s", sdl_website);
            ImGui::SameLine();
            if(ImGui::Button("Copy to clipboard")) {
                ImGui::SetClipboardText(sdl_website);
            }
            ImGui::SameLine();
            if(ImGui::Button("Open in browser")) {
                openUri(sdl_website);
            }

            ImGui::TextUnformatted("Licence: zlib");

            ImGui::TreePop();
        }

        if(ImGui::TreeNodeEx("libzip", ImGuiTreeNodeFlags_SpanAvailWidth)) {
            ImGui::Text("Version used: %s", LIBZIP_VERSION);
            auto libzip_website = "https://libzip.org/";
            drawAlignedText(ICON_FA_GLOBE " %s", libzip_website);
            ImGui::SameLine();
            if(ImGui::Button("Copy to clipboard")) {
                ImGui::SetClipboardText(libzip_website);
            }
            ImGui::SameLine();
            if(ImGui::Button("Open in browser")) {
                openUri(libzip_website);
            }

            ImGui::TextUnformatted("Licence: 3-clause BSD");

            ImGui::TreePop();
        }

        if(ImGui::TreeNodeEx("Entropia File System Watcher (efsw)", ImGuiTreeNodeFlags_SpanAvailWidth)) {
            auto efsw_repo = "https://github.com/SpartanJ/efsw";
            drawAlignedText(ICON_FA_GITHUB " %s", efsw_repo);
            ImGui::SameLine();
            if(ImGui::Button("Copy to clipboard")) {
                ImGui::SetClipboardText(efsw_repo);
            }
            ImGui::SameLine();
            if(ImGui::Button("Open in browser")) {
                openUri(efsw_repo);
            }

            ImGui::TextUnformatted("Licence: MIT");

            ImGui::TreePop();
        }

        if(ImGui::TreeNodeEx("libcurl", ImGuiTreeNodeFlags_SpanAvailWidth)) {
            ImGui::Text("Version used: %s", LIBCURL_VERSION);
            auto curl_website = "https://curl.se/libcurl";
            drawAlignedText(ICON_FA_GLOBE " %s", curl_website);
            ImGui::SameLine();
            if(ImGui::Button("Copy to clipboard")) {
                ImGui::SetClipboardText(curl_website);
            }
            ImGui::SameLine();
            if(ImGui::Button("Open in browser")) {
                openUri(curl_website);
            }

            ImGui::TextUnformatted("Licence: MIT/X derivative");

            ImGui::TreePop();
        }

        if(ImGui::TreeNodeEx("Font Awesome", ImGuiTreeNodeFlags_SpanAvailWidth)) {
            ImGui::TextUnformatted("Version used: 5.15.3");
            auto fa_website = "https://fontawesome.com/";
            drawAlignedText(ICON_FA_GLOBE " %s", fa_website);
            ImGui::SameLine();
            if(ImGui::Button("Copy to clipboard")) {
                ImGui::SetClipboardText(fa_website);
            }
            ImGui::SameLine();
            if(ImGui::Button("Open in browser")) {
                openUri(fa_website);
            }

            ImGui::TextUnformatted("Licence: SIL Open Font License 1.1");

            ImGui::TreePop();
        }

        if(ImGui::TreeNodeEx("IconFontCppHeaders", ImGuiTreeNodeFlags_SpanAvailWidth)) {
            auto icon_repo = "https://github.com/juliettef/IconFontCppHeaders";
            drawAlignedText(ICON_FA_GITHUB " %s", icon_repo);
            ImGui::SameLine();
            if(ImGui::Button("Copy to clipboard")) {
                ImGui::SetClipboardText(icon_repo);
            }
            ImGui::SameLine();
            if(ImGui::Button("Open in browser")) {
                openUri(icon_repo);
            }

            ImGui::TextUnformatted("Licence: zlib");

            ImGui::TreePop();
        }

        ImGui::PopStyleVar();
    }

    ImGui::EndPopup();
}

}