// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3

#include "about_dialog.h"
#include "../../app.h"
#include "../../config/version.h"
#include "../../util/i18n.h"
#include "../../util/platform.h"
#include "../schema/ui_schema.h"
#include "../material/type.h"
#include "../material/draw_helpers.h"
#include "imgui.h"

namespace dragonx {
namespace ui {

void RenderAboutDialog(App* app, bool* p_open)
{
    (void)app;
    auto& S = schema::UI();
    auto win = S.window("dialogs.about");
    auto linkBtn = S.button("dialogs.about", "link-button");
    auto closeBtn = S.button("dialogs.about", "close-button");
    auto versionLbl = S.label("dialogs.about", "version-label");
    auto editionLbl = S.label("dialogs.about", "edition-label");
    
    if (!material::BeginOverlayDialog(TR("about_title"), p_open, win.width, 0.94f)) {
        return;
    }

    // Use Body2 font for all dialog text
    ImGui::PushFont(Type().body2());

    // Logo/Title area
    ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.3f, 0.69f, 0.31f, 1.0f)); // Green
    ImGui::PushFont(Type().h4());
    ImGui::Text("ObsidianDragon");
    ImGui::PopFont();
    ImGui::PopStyleColor();
    
    ImGui::SameLine(ImGui::GetWindowWidth() - editionLbl.position);
    ImGui::TextDisabled("%s", TR("about_edition"));
    
    ImGui::Spacing();
    ImGui::Separator();
    ImGui::Spacing();
    
    // Version info
    ImGui::Text("%s", TR("about_version"));
    ImGui::SameLine(versionLbl.position);
    ImGui::Text("%s", DRAGONX_VERSION);
    
    ImGui::Text("%s", TR("about_imgui"));
    ImGui::SameLine(versionLbl.position);
    ImGui::Text("%s", IMGUI_VERSION);
    
    ImGui::Text("%s", TR("about_build_date"));
    ImGui::SameLine(versionLbl.position);
    ImGui::Text("%s %s", __DATE__, __TIME__);
    
#ifdef DRAGONX_DEBUG
    ImGui::Text("%s", TR("about_build_type"));
    ImGui::SameLine(versionLbl.position);
    ImGui::TextColored(ImVec4(1.0f, 0.6f, 0.0f, 1.0f), "%s", TR("about_debug"));
#else
    ImGui::Text("%s", TR("about_build_type"));
    ImGui::SameLine(versionLbl.position);
    ImGui::Text("%s", TR("about_release"));
#endif

    // Daemon info
    if (app && app->isConnected()) {
        ImGui::Spacing();
        ImGui::Separator();
        ImGui::Spacing();
        
        ImGui::Text("%s", TR("about_daemon"));
        ImGui::SameLine(versionLbl.position);
        ImGui::TextColored(ImVec4(0.3f, 0.8f, 0.3f, 1.0f), "%s", TR("connected"));
        
        const auto& state = app->getWalletState();
        ImGui::Text("%s", TR("about_chain"));
        ImGui::SameLine(versionLbl.position);
        ImGui::Text("ObsidianDragon");
        
        ImGui::Text("%s", TR("about_block_height"));
        ImGui::SameLine(versionLbl.position);
        ImGui::Text("%d", state.sync.blocks);
        
        ImGui::Text("%s", TR("about_connections"));
        ImGui::SameLine(versionLbl.position);
        ImGui::Text(TR("about_peers_count"), state.peers.size());
    }
    
    ImGui::Spacing();
    ImGui::Separator();
    ImGui::Spacing();
    
    // Credits
    ImGui::TextColored(ImVec4(0.7f, 0.7f, 0.7f, 1.0f), "%s", TR("about_credits"));
    ImGui::Spacing();
    
    ImGui::BulletText("The Hush Developers");
    ImGui::BulletText("ObsidianDragon Community");
    ImGui::BulletText("Dear ImGui by Omar Cornut");
    ImGui::BulletText("ImPlot by Evan Pezent");
    ImGui::BulletText("SDL3 by Sam Lantinga");
    
    ImGui::Spacing();
    ImGui::Separator();
    ImGui::Spacing();
    
    // License
    ImGui::TextColored(ImVec4(0.7f, 0.7f, 0.7f, 1.0f), "%s", TR("about_license"));
    ImGui::Spacing();
    ImGui::TextWrapped("%s", TR("about_license_text"));
    
    ImGui::Spacing();
    ImGui::Separator();
    ImGui::Spacing();
    
    // Links
    if (material::StyledButton(TR("about_website"), ImVec2(linkBtn.width, 0), S.resolveFont(linkBtn.font))) {
        util::Platform::openUrl("https://dragonx.is");
    }
    ImGui::SameLine();
    if (material::StyledButton(TR("about_github"), ImVec2(linkBtn.width, 0), S.resolveFont(linkBtn.font))) {
        util::Platform::openUrl("https://git.dragonx.is/dragonx/ObsidianDragon");
    }
    ImGui::SameLine();
    if (material::StyledButton(TR("about_block_explorer"), ImVec2(linkBtn.width, 0), S.resolveFont(linkBtn.font))) {
        util::Platform::openUrl("https://explorer.dragonx.is");
    }
    
    ImGui::Spacing();
    
    // Close button
    float button_width = closeBtn.width;
    ImGui::SetCursorPosX((ImGui::GetWindowWidth() - button_width) * 0.5f);
    if (material::StyledButton(TR("close"), ImVec2(button_width, 0), S.resolveFont(closeBtn.font))) {
        *p_open = false;
    }

    ImGui::PopFont(); // Body2
    material::EndOverlayDialog();
}

} // namespace ui
} // namespace dragonx
