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

#pragma once

#include "../colors.h"
#include "../typography.h"
#include "../layout.h"
#include "../draw_helpers.h"
#include "imgui.h"

namespace dragonx {
namespace ui {
namespace material {

// ============================================================================
// Material Design Card Component
// ============================================================================
// Based on https://m2.material.io/components/cards
//
// Cards contain content and actions about a single subject.
// They can be elevated (with shadow) or outlined (with border).

/**
 * @brief Card configuration
 */
struct CardSpec {
    int elevation = 1;           // Elevation in dp (0-24)
    bool outlined = false;       // Use outline instead of elevation
    float cornerRadius = 4.0f;   // Corner radius in dp
    bool clickable = false;      // Make entire card clickable
    float padding = 16.0f;       // Content padding
    float minHeight = 0.0f;      // Minimum height (0 = auto)
};

/**
 * @brief Begin a Material Design card
 * 
 * @param id Unique identifier for the card
 * @param spec Card configuration
 * @return true if card is visible and content should be rendered
 */
bool BeginCard(const char* id, const CardSpec& spec = CardSpec());

/**
 * @brief End the card
 */
void EndCard();

/**
 * @brief Begin a clickable card that returns click state
 * 
 * @param id Unique identifier
 * @param spec Card configuration
 * @param clicked Output: true if card was clicked
 * @return true if card is visible
 */
bool BeginClickableCard(const char* id, const CardSpec& spec, bool* clicked);

/**
 * @brief Card header with title and optional subtitle
 * 
 * @param title Primary title text
 * @param subtitle Optional secondary text
 * @param avatar Optional avatar texture (rendered as circle)
 */
void CardHeader(const char* title, const char* subtitle = nullptr);

/**
 * @brief Card supporting text/content
 * 
 * @param text Body text content
 */
void CardContent(const char* text);

/**
 * @brief Begin card action area (for buttons)
 * 
 * Actions are typically placed at the bottom of the card.
 */
void CardActions();

/**
 * @brief End card action area
 */
void CardActionsEnd();

/**
 * @brief Add divider within card
 */
void CardDivider();

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

inline bool BeginCard(const char* id, const CardSpec& spec) {
    ImGui::PushID(id);
    
    // Calculate surface color based on elevation
    ImU32 bgColor = spec.outlined ? Surface() : GetElevatedSurface(GetCurrentColorTheme(), spec.elevation);
    
    // When acrylic backdrop is active, scale card bg alpha by UI opacity
    // so cards smoothly transition from opaque (1.0) to see-through.
    bool opaqueCards = dragonx::ui::effects::isLowSpecMode();
    if (IsBackdropActive() && !opaqueCards) {
        ImVec4 c = ImGui::ColorConvertU32ToFloat4(bgColor);
        float uiOp = dragonx::ui::effects::ImGuiAcrylic::GetUIOpacity();
        c.w *= uiOp;
        ImGui::PushStyleColor(ImGuiCol_ChildBg, c);
    } else {
        ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::ColorConvertU32ToFloat4(bgColor));
    }
    ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, spec.cornerRadius);
    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(spec.padding, spec.padding));
    
    // Border for outlined variant
    if (spec.outlined) {
        ImGui::PushStyleColor(ImGuiCol_Border, ImGui::ColorConvertU32ToFloat4(Outline()));
        ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 1.0f);
    } else {
        ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0.0f);
    }
    
    ImVec2 size(0, spec.minHeight);  // 0 width = use available width
    ImGuiChildFlags flags = ImGuiChildFlags_AutoResizeY;
    if (spec.outlined) {
        flags |= ImGuiChildFlags_Borders;
    }
    
    bool visible = ImGui::BeginChild(id, size, flags);
    
    return visible;
}

inline void EndCard() {
    ImGui::EndChild();
    
    ImGui::PopStyleVar(3);  // ChildRounding, WindowPadding, ChildBorderSize
    ImGui::PopStyleColor(1);  // ChildBg
    
    // Check if we used outline style (need to pop extra color)
    // Note: We always push the border size var, handle outline color in BeginCard
    
    ImGui::PopID();
    
    // Add spacing after card
    VSpace(2);
}

inline bool BeginClickableCard(const char* id, const CardSpec& spec, bool* clicked) {
    *clicked = false;
    
    ImGui::PushID(id);
    
    ImVec2 startPos = ImGui::GetCursorScreenPos();
    
    // Render card background
    ImU32 bgColor = spec.outlined ? Surface() : GetElevatedSurface(GetCurrentColorTheme(), spec.elevation);
    
    ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::ColorConvertU32ToFloat4(bgColor));
    ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, spec.cornerRadius);
    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(spec.padding, spec.padding));
    ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, spec.outlined ? 1.0f : 0.0f);
    
    if (spec.outlined) {
        ImGui::PushStyleColor(ImGuiCol_Border, ImGui::ColorConvertU32ToFloat4(Outline()));
    }
    
    ImVec2 size(0, spec.minHeight);
    ImGuiChildFlags flags = ImGuiChildFlags_AutoResizeY;
    if (spec.outlined) {
        flags |= ImGuiChildFlags_Borders;
    }
    
    bool visible = ImGui::BeginChild(id, size, flags);
    
    return visible;
}

inline void CardHeader(const char* title, const char* subtitle) {
    Typography::instance().text(TypeStyle::H6, title);
    
    if (subtitle) {
        Typography::instance().textColored(TypeStyle::Body2, OnSurfaceMedium(), subtitle);
    }
    
    VSpace(1);
}

inline void CardContent(const char* text) {
    Typography::instance().textWrapped(TypeStyle::Body2, text);
    VSpace(1);
}

inline void CardActions() {
    ImGui::Separator();
    VSpace(1);
    ImGui::BeginGroup();
}

inline void CardActionsEnd() {
    ImGui::EndGroup();
}

inline void CardDivider() {
    ImGui::Separator();
    VSpace(1);
}

} // namespace material
} // namespace ui
} // namespace dragonx
