// 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 <functional>

namespace dragonx {
namespace ui {
namespace screens {

using namespace material;

// ============================================================================
// Confirmation Dialog
// ============================================================================
// Material Design confirmation dialog for critical actions

/**
 * @brief Confirmation dialog type
 */
enum class ConfirmationType {
    Info,
    Warning,
    Danger,
    Transaction
};

/**
 * @brief Transaction confirmation details
 */
struct TransactionConfirmation {
    std::string toAddress;
    double amount;
    double fee;
    std::string memo;
    bool isShielded;
};

/**
 * @brief Confirmation dialog manager
 */
class ConfirmationDialog {
public:
    static ConfirmationDialog& instance() {
        static ConfirmationDialog s_instance;
        return s_instance;
    }
    
    /**
     * @brief Show a simple confirmation dialog
     */
    void show(const std::string& title,
              const std::string& message,
              ConfirmationType type,
              std::function<void(bool)> callback) {
        m_title = title;
        m_message = message;
        m_type = type;
        m_callback = callback;
        m_isOpen = true;
        m_isTransaction = false;
    }
    
    /**
     * @brief Show transaction confirmation dialog
     */
    void showTransaction(const TransactionConfirmation& tx,
                        std::function<void(bool)> callback) {
        m_title = "Confirm Transaction";
        m_type = ConfirmationType::Transaction;
        m_transaction = tx;
        m_callback = callback;
        m_isOpen = true;
        m_isTransaction = true;
    }
    
    /**
     * @brief Render the dialog (call every frame)
     */
    void render();
    
    /**
     * @brief Check if dialog is currently open
     */
    bool isOpen() const { return m_isOpen; }
    
private:
    ConfirmationDialog() = default;
    
    void renderSimpleDialog();
    void renderTransactionDialog();
    
    std::string m_title;
    std::string m_message;
    ConfirmationType m_type = ConfirmationType::Info;
    TransactionConfirmation m_transaction;
    std::function<void(bool)> m_callback;
    bool m_isOpen = false;
    bool m_isTransaction = false;
};

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

inline void ConfirmationDialog::render() {
    if (!m_isOpen) return;
    
    if (m_isTransaction) {
        renderTransactionDialog();
    } else {
        renderSimpleDialog();
    }
}

inline void ConfirmationDialog::renderSimpleDialog() {
    DialogSpec dialogSpec;
    dialogSpec.title = m_title.c_str();
    dialogSpec.maxWidth = 400.0f;
    
    DialogResult result = BeginDialog("confirm_dialog", dialogSpec);
    
    if (result.isOpen) {
        // Icon based on type
        ImGui::BeginGroup();
        {
            ImVec4 iconColor;
            const char* icon;
            
            switch (m_type) {
                case ConfirmationType::Info:
                    iconColor = colors::Blue500;
                    icon = ICON_MD_INFO;
                    break;
                case ConfirmationType::Warning:
                    iconColor = Secondary();
                    icon = ICON_MD_WARNING;
                    break;
                case ConfirmationType::Danger:
                    iconColor = Error();
                    icon = ICON_MD_DANGEROUS;
                    break;
                default:
                    iconColor = Primary();
                    icon = ICON_MD_HELP;
                    break;
            }
            
            Typography::instance().textColored(TypeStyle::H4, iconColor, icon);
            ImGui::SameLine();
            
            ImGui::BeginGroup();
            ImGui::TextWrapped("%s", m_message.c_str());
            ImGui::EndGroup();
        }
        ImGui::EndGroup();
        
        ImGui::Dummy(ImVec2(0, spacing::dp(3)));
        
        // Buttons
        float availWidth = ImGui::GetContentRegionAvail().x;
        ImGui::SetCursorPosX(ImGui::GetCursorPosX() + availWidth - 200);
        
        if (TextButton("CANCEL")) {
            m_isOpen = false;
            if (m_callback) m_callback(false);
        }
        
        ImGui::SameLine(0, spacing::dp(2));
        
        // Confirm button color based on type
        if (m_type == ConfirmationType::Danger) {
            ImGui::PushStyleColor(ImGuiCol_Button, Error());
            ImGui::PushStyleColor(ImGuiCol_ButtonHovered, colors::Red400);
        }
        
        if (ContainedButton("CONFIRM")) {
            m_isOpen = false;
            if (m_callback) m_callback(true);
        }
        
        if (m_type == ConfirmationType::Danger) {
            ImGui::PopStyleColor(2);
        }
        
        EndDialog();
    }
    
    if (result.dismissed) {
        m_isOpen = false;
        if (m_callback) m_callback(false);
    }
}

inline void ConfirmationDialog::renderTransactionDialog() {
    DialogSpec dialogSpec;
    dialogSpec.title = "Confirm Transaction";
    dialogSpec.maxWidth = 500.0f;
    
    DialogResult result = BeginDialog("tx_confirm_dialog", dialogSpec);
    
    if (result.isOpen) {
        // Transaction type badge
        ChipSpec chipSpec;
        chipSpec.variant = ChipVariant::Filled;
        
        if (m_transaction.isShielded) {
            chipSpec.color = Primary();
            Chip(ICON_MD_SHIELD " Shielded Transaction", chipSpec);
        } else {
            chipSpec.color = Secondary();
            Chip(ICON_MD_DESCRIPTION " Transparent Transaction", chipSpec);
        }
        
        ImGui::Dummy(ImVec2(0, spacing::dp(3)));
        
        // Amount (large, prominent)
        Typography::instance().textColored(TypeStyle::Overline, OnSurfaceMedium(), "AMOUNT");
        char amountStr[64];
        snprintf(amountStr, sizeof(amountStr), "%.8f DRGX", m_transaction.amount);
        Typography::instance().textColored(TypeStyle::H4, Primary(), amountStr);
        
        ImGui::Dummy(ImVec2(0, spacing::dp(2)));
        
        // Recipient
        Typography::instance().textColored(TypeStyle::Overline, OnSurfaceMedium(), "TO ADDRESS");
        ImGui::TextWrapped("%s", m_transaction.toAddress.c_str());
        
        ImGui::Dummy(ImVec2(0, spacing::dp(2)));
        
        // Fee
        Typography::instance().textColored(TypeStyle::Overline, OnSurfaceMedium(), "NETWORK FEE");
        char feeStr[64];
        snprintf(feeStr, sizeof(feeStr), "%.8f DRGX", m_transaction.fee);
        Typography::instance().text(TypeStyle::Body1, feeStr);
        
        // Memo if present
        if (!m_transaction.memo.empty()) {
            ImGui::Dummy(ImVec2(0, spacing::dp(2)));
            Typography::instance().textColored(TypeStyle::Overline, OnSurfaceMedium(), "MEMO (ENCRYPTED)");
            ImGui::TextWrapped("%s", m_transaction.memo.c_str());
        }
        
        ImGui::Dummy(ImVec2(0, spacing::dp(2)));
        
        // Total
        ImVec2 divPos = ImGui::GetCursorScreenPos();
        float availWidth = ImGui::GetContentRegionAvail().x;
        ImGui::GetWindowDrawList()->AddLine(
            divPos,
            ImVec2(divPos.x + availWidth, divPos.y),
            ImGui::GetColorU32(Divider())
        );
        ImGui::Dummy(ImVec2(0, spacing::dp(2)));
        
        Typography::instance().textColored(TypeStyle::Overline, OnSurfaceMedium(), "TOTAL");
        char totalStr[64];
        snprintf(totalStr, sizeof(totalStr), "%.8f DRGX", m_transaction.amount + m_transaction.fee);
        Typography::instance().text(TypeStyle::H5, totalStr);
        
        ImGui::Dummy(ImVec2(0, spacing::dp(3)));
        
        // Warning for shielded transactions
        if (m_transaction.isShielded) {
            CardSpec warningCard;
            warningCard.elevation = 0;
            warningCard.padding = spacing::dp(2);
            warningCard.outlined = true;
            
            ImGui::PushStyleColor(ImGuiCol_Border, colors::withAlpha(Secondary(), 0.5f));
            if (BeginCard(warningCard)) {
                Typography::instance().textColored(TypeStyle::Body2, Secondary(), 
                    ICON_MD_WARNING " Shielded transactions are private but take longer to process.");
                EndCard();
            }
            ImGui::PopStyleColor();
            
            ImGui::Dummy(ImVec2(0, spacing::dp(2)));
        }
        
        // Buttons
        ImGui::SetCursorPosX(ImGui::GetCursorPosX() + availWidth - 220);
        
        if (OutlinedButton("CANCEL")) {
            m_isOpen = false;
            if (m_callback) m_callback(false);
        }
        
        ImGui::SameLine(0, spacing::dp(2));
        
        if (ContainedButton("SEND NOW")) {
            m_isOpen = false;
            if (m_callback) m_callback(true);
        }
        
        EndDialog();
    }
    
    if (result.dismissed) {
        m_isOpen = false;
        if (m_callback) m_callback(false);
    }
}

// ============================================================================
// Quick Confirmation Helpers
// ============================================================================

/**
 * @brief Show a simple confirmation dialog
 */
inline void ShowConfirmation(const std::string& title,
                             const std::string& message,
                             std::function<void(bool)> callback) {
    ConfirmationDialog::instance().show(title, message, ConfirmationType::Info, callback);
}

/**
 * @brief Show a warning confirmation dialog
 */
inline void ShowWarningConfirmation(const std::string& title,
                                    const std::string& message,
                                    std::function<void(bool)> callback) {
    ConfirmationDialog::instance().show(title, message, ConfirmationType::Warning, callback);
}

/**
 * @brief Show a danger confirmation dialog
 */
inline void ShowDangerConfirmation(const std::string& title,
                                   const std::string& message,
                                   std::function<void(bool)> callback) {
    ConfirmationDialog::instance().show(title, message, ConfirmationType::Danger, callback);
}

/**
 * @brief Show a transaction confirmation dialog
 */
inline void ShowTransactionConfirmation(const TransactionConfirmation& tx,
                                        std::function<void(bool)> callback) {
    ConfirmationDialog::instance().showTransaction(tx, callback);
}

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