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

#pragma once

#include "../material/material.h"
#include "../../embedded/IconsMaterialDesign.h"
#include "imgui.h"
#include <string>
#include <vector>
#include <functional>

namespace dragonx {
namespace ui {
namespace screens {

using namespace material;

// ============================================================================
// Balance/Home Screen
// ============================================================================
// Main dashboard showing wallet balances and recent activity

/**
 * @brief Balance information for display
 */
struct BalanceInfo {
    double total = 0.0;
    double shielded = 0.0;
    double transparent = 0.0;
    double pending = 0.0;
    double fiatValue = 0.0;
    std::string fiatCurrency = "USD";
};

/**
 * @brief Recent transaction summary
 */
struct RecentTransaction {
    std::string txid;
    std::string type;        // "sent", "received", "mined"
    std::string address;     // Truncated address
    double amount;
    std::string time;        // Relative time "2 hours ago"
    bool confirmed;
};

/**
 * @brief Render the home/balance screen
 */
class HomeScreen {
public:
    void setBalance(const BalanceInfo& balance) { m_balance = balance; }
    void setRecentTransactions(const std::vector<RecentTransaction>& txns) { m_recentTxns = txns; }
    void setOnSendClick(std::function<void()> callback) { m_onSendClick = callback; }
    void setOnReceiveClick(std::function<void()> callback) { m_onReceiveClick = callback; }
    void setOnTransactionClick(std::function<void(const std::string&)> callback) { m_onTxClick = callback; }
    
    void render();
    
private:
    void renderBalanceCard();
    void renderQuickActions();
    void renderShieldedCard();
    void renderTransparentCard();
    void renderRecentTransactions();
    
    BalanceInfo m_balance;
    std::vector<RecentTransaction> m_recentTxns;
    std::function<void()> m_onSendClick;
    std::function<void()> m_onReceiveClick;
    std::function<void(const std::string&)> m_onTxClick;
};

// ============================================================================
// Implementation
// ============================================================================

inline void HomeScreen::render() {
    ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(spacing::dp(2), spacing::dp(2)));
    
    // Two-column layout
    float availWidth = ImGui::GetContentRegionAvail().x;
    float cardWidth = (availWidth - spacing::dp(2)) * 0.5f;
    
    // Row 1: Total Balance + Recent Transactions
    ImGui::BeginGroup();
    {
        // Total Balance Card
        ImGui::BeginGroup();
        renderBalanceCard();
        ImGui::EndGroup();
        
        ImGui::SameLine();
        
        // Recent Transactions Card
        ImGui::BeginGroup();
        renderRecentTransactions();
        ImGui::EndGroup();
    }
    ImGui::EndGroup();
    
    ImGui::Dummy(ImVec2(0, spacing::dp(2)));
    
    // Row 2: Shielded + Transparent
    ImGui::BeginGroup();
    {
        ImGui::BeginGroup();
        renderShieldedCard();
        ImGui::EndGroup();
        
        ImGui::SameLine();
        
        ImGui::BeginGroup();
        renderTransparentCard();
        ImGui::EndGroup();
    }
    ImGui::EndGroup();
    
    ImGui::Dummy(ImVec2(0, spacing::dp(2)));
    
    // Quick Actions
    renderQuickActions();
    
    ImGui::PopStyleVar();
}

inline void HomeScreen::renderBalanceCard() {
    float cardWidth = (ImGui::GetContentRegionAvail().x - spacing::dp(2)) * 0.5f;
    
    CardSpec spec;
    spec.elevation = 2;
    spec.padding = spacing::dp(3);
    
    ImGui::PushID("balance_card");
    if (BeginCard(spec)) {
        // Overline
        Typography::instance().textColored(TypeStyle::Overline, OnSurfaceMedium(), "TOTAL BALANCE");
        ImGui::Dummy(ImVec2(0, spacing::dp(1)));
        
        // Balance amount
        char balanceStr[64];
        snprintf(balanceStr, sizeof(balanceStr), "%.8f", m_balance.total);
        Typography::instance().text(TypeStyle::H4, balanceStr);
        
        ImGui::SameLine();
        Typography::instance().textColored(TypeStyle::H6, OnSurfaceMedium(), "DRGX");
        
        // Fiat value
        if (m_balance.fiatValue > 0) {
            ImGui::Dummy(ImVec2(0, spacing::dp(0.5f)));
            char fiatStr[64];
            snprintf(fiatStr, sizeof(fiatStr), "≈ $%.2f %s", m_balance.fiatValue, m_balance.fiatCurrency.c_str());
            Typography::instance().textColored(TypeStyle::Body2, OnSurfaceMedium(), fiatStr);
        }
        
        // Pending indicator
        if (m_balance.pending > 0) {
            ImGui::Dummy(ImVec2(0, spacing::dp(1)));
            char pendingStr[64];
            snprintf(pendingStr, sizeof(pendingStr), ICON_MD_HOURGLASS_EMPTY " %.8f pending", m_balance.pending);
            Typography::instance().textColored(TypeStyle::Caption, Secondary(), pendingStr);
        }
        
        EndCard();
    }
    ImGui::PopID();
}

inline void HomeScreen::renderQuickActions() {
    ImGui::Dummy(ImVec2(0, spacing::dp(1)));
    
    float buttonWidth = 120.0f;
    float availWidth = ImGui::GetContentRegionAvail().x;
    float startX = (availWidth - buttonWidth * 2 - spacing::dp(2)) * 0.5f;
    
    ImGui::SetCursorPosX(ImGui::GetCursorPosX() + startX);
    
    if (ContainedButton(ICON_MD_CALL_MADE " SEND")) {
        if (m_onSendClick) m_onSendClick();
    }
    
    ImGui::SameLine(0, spacing::dp(2));
    
    if (OutlinedButton(ICON_MD_CALL_RECEIVED " RECEIVE")) {
        if (m_onReceiveClick) m_onReceiveClick();
    }
}

inline void HomeScreen::renderShieldedCard() {
    CardSpec spec;
    spec.elevation = 1;
    spec.padding = spacing::dp(2);
    
    ImGui::PushID("shielded_card");
    if (BeginCard(spec)) {
        // Icon and label
        ImGui::BeginGroup();
        Typography::instance().textColored(TypeStyle::Body2, Primary(), ICON_MD_SHIELD);
        ImGui::SameLine();
        Typography::instance().textColored(TypeStyle::Overline, OnSurfaceMedium(), "SHIELDED (PRIVATE)");
        ImGui::EndGroup();
        
        ImGui::Dummy(ImVec2(0, spacing::dp(1)));
        
        // Amount
        char amountStr[64];
        snprintf(amountStr, sizeof(amountStr), "%.8f", m_balance.shielded);
        Typography::instance().text(TypeStyle::H5, amountStr);
        
        ImGui::SameLine();
        Typography::instance().textColored(TypeStyle::Body1, OnSurfaceMedium(), "DRGX");
        
        EndCard();
    }
    ImGui::PopID();
}

inline void HomeScreen::renderTransparentCard() {
    CardSpec spec;
    spec.elevation = 1;
    spec.padding = spacing::dp(2);
    
    ImGui::PushID("transparent_card");
    if (BeginCard(spec)) {
        // Icon and label
        ImGui::BeginGroup();
        Typography::instance().textColored(TypeStyle::Body2, Secondary(), ICON_MD_CONTENT_COPY);
        ImGui::SameLine();
        Typography::instance().textColored(TypeStyle::Overline, OnSurfaceMedium(), "TRANSPARENT");
        ImGui::EndGroup();
        
        ImGui::Dummy(ImVec2(0, spacing::dp(1)));
        
        // Amount
        char amountStr[64];
        snprintf(amountStr, sizeof(amountStr), "%.8f", m_balance.transparent);
        Typography::instance().text(TypeStyle::H5, amountStr);
        
        ImGui::SameLine();
        Typography::instance().textColored(TypeStyle::Body1, OnSurfaceMedium(), "DRGX");
        
        EndCard();
    }
    ImGui::PopID();
}

inline void HomeScreen::renderRecentTransactions() {
    CardSpec spec;
    spec.elevation = 1;
    spec.padding = spacing::dp(2);
    
    ImGui::PushID("recent_txns");
    if (BeginCard(spec)) {
        Typography::instance().textColored(TypeStyle::Overline, OnSurfaceMedium(), "RECENT TRANSACTIONS");
        ImGui::Dummy(ImVec2(0, spacing::dp(1)));
        
        if (m_recentTxns.empty()) {
            Typography::instance().textColored(TypeStyle::Body2, OnSurfaceMedium(), "No recent transactions");
        } else {
            BeginList("recent_list", false);
            
            int count = 0;
            for (const auto& tx : m_recentTxns) {
                if (count >= 5) break;  // Show max 5
                
                ListItemSpec itemSpec;
                
                // Icon based on type
                if (tx.type == "received") {
                    itemSpec.leadingIcon = ICON_MD_CALL_RECEIVED;
                } else if (tx.type == "sent") {
                    itemSpec.leadingIcon = ICON_MD_CALL_MADE;
                } else {
                    itemSpec.leadingIcon = ICON_MD_CONSTRUCTION;
                }
                
                // Amount as primary text
                char amountStr[32];
                snprintf(amountStr, sizeof(amountStr), "%+.4f DRGX", 
                        tx.type == "sent" ? -tx.amount : tx.amount);
                itemSpec.primaryText = amountStr;
                itemSpec.secondaryText = tx.time.c_str();
                itemSpec.dividerBelow = (count < 4 && count < (int)m_recentTxns.size() - 1);
                
                if (ListItem(itemSpec)) {
                    if (m_onTxClick) m_onTxClick(tx.txid);
                }
                
                count++;
            }
            
            EndList();
        }
        
        EndCard();
    }
    ImGui::PopID();
}

} // namespace screens
} // namespace ui
} // namespace dragonx
