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

// Material Design 2 Color Theme System
// Based on https://m2.material.io/design/color/the-color-system.html

#pragma once

#include "imgui.h"
#include <cstdint>

// Windows defines RGB as a macro - undefine it to avoid conflicts
#ifdef RGB
#undef RGB
#endif

namespace dragonx {
namespace ui {
namespace material {

// ============================================================================
// Color Utilities
// ============================================================================

/**
 * @brief Convert ImU32 to ImVec4
 */
inline ImVec4 ColorU32ToVec4(ImU32 color) {
    return ImGui::ColorConvertU32ToFloat4(color);
}

/**
 * @brief Convert ImVec4 to ImU32
 */
inline ImU32 ColorVec4ToU32(const ImVec4& color) {
    return ImGui::ColorConvertFloat4ToU32(color);
}

/**
 * @brief Create color from RGB values (0-255)
 */
constexpr ImU32 MakeRGB(uint8_t r, uint8_t g, uint8_t b) {
    return IM_COL32(r, g, b, 255);
}

/**
 * @brief Create color from RGBA values (0-255)
 */
constexpr ImU32 MakeRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
    return IM_COL32(r, g, b, a);
}

/**
 * @brief Create color from hex value (0xRRGGBB)
 */
constexpr ImU32 Hex(uint32_t hex) {
    return IM_COL32(
        (hex >> 16) & 0xFF,
        (hex >> 8) & 0xFF,
        hex & 0xFF,
        255
    );
}

/**
 * @brief Create color from hex value with alpha (0xRRGGBB, alpha 0-255)
 */
constexpr ImU32 HexA(uint32_t hex, uint8_t alpha) {
    return IM_COL32(
        (hex >> 16) & 0xFF,
        (hex >> 8) & 0xFF,
        hex & 0xFF,
        alpha
    );
}

/**
 * @brief Apply alpha to existing color
 */
inline ImU32 WithAlpha(ImU32 color, uint8_t alpha) {
    return (color & 0x00FFFFFF) | (static_cast<ImU32>(alpha) << 24);
}

/**
 * @brief Apply alpha percentage (0.0 - 1.0) to existing color
 */
inline ImU32 WithAlphaF(ImU32 color, float alpha) {
    return WithAlpha(color, static_cast<uint8_t>(alpha * 255.0f));
}

/**
 * @brief Blend two colors with overlay opacity
 */
ImU32 BlendOverlay(ImU32 base, ImU32 overlay, float overlayOpacity);

/**
 * @brief Lighten a color by percentage (0.0 - 1.0)
 */
ImU32 Lighten(ImU32 color, float amount);

/**
 * @brief Darken a color by percentage (0.0 - 1.0)
 */
ImU32 Darken(ImU32 color, float amount);

// ============================================================================
// Material Design Color Theme
// ============================================================================

/**
 * @brief Complete Material Design 2 color theme
 * 
 * Based on the Material Design color system with:
 * - Primary and secondary brand colors
 * - Surface and background colors
 * - "On" colors for content on surfaces
 * - State overlay colors
 * - Error colors
 */
struct ColorTheme {
    // ========================================================================
    // Brand Colors - Primary
    // ========================================================================
    ImU32 primary;              // Main brand color (displayed most frequently)
    ImU32 primaryVariant;       // Darker variant for contrast
    ImU32 primaryLight;         // Lighter variant for highlights
    
    // ========================================================================
    // Brand Colors - Secondary (Accent)
    // ========================================================================
    ImU32 secondary;            // Accent color for emphasis
    ImU32 secondaryVariant;     // Darker variant
    ImU32 secondaryLight;       // Lighter variant
    
    // ========================================================================
    // Surface Colors
    // ========================================================================
    ImU32 background;           // App background
    ImU32 surface;              // Card/dialog surfaces (elevation 0dp)
    ImU32 surfaceVariant;       // Alternative surface
    
    // ========================================================================
    // "On" Colors - Content colors for surfaces
    // ========================================================================
    ImU32 onPrimary;            // Text/icons on primary color
    ImU32 onSecondary;          // Text/icons on secondary color
    ImU32 onBackground;         // Text/icons on background
    ImU32 onSurface;            // Text/icons on surface (high emphasis)
    ImU32 onSurfaceMedium;      // Medium emphasis text (70% opacity)
    ImU32 onSurfaceDisabled;    // Disabled text (38% opacity)
    
    // ========================================================================
    // Error Colors
    // ========================================================================
    ImU32 error;                // Error indication
    ImU32 onError;              // Text on error color
    
    // ========================================================================
    // Success/Warning Colors (Material extension)
    // ========================================================================
    ImU32 success;              // Success indication
    ImU32 onSuccess;            // Text on success
    ImU32 warning;              // Warning indication
    ImU32 onWarning;            // Text on warning
    
    // ========================================================================
    // State Overlay Colors (applied to surfaces)
    // ========================================================================
    ImU32 stateHover;           // Hover overlay (4% white/black)
    ImU32 stateFocus;           // Focus overlay (12%)
    ImU32 statePressed;         // Pressed overlay (10%)
    ImU32 stateSelected;        // Selected overlay (8%)
    ImU32 stateDragged;         // Dragged overlay (8%)
    
    // ========================================================================
    // Additional UI Colors
    // ========================================================================
    ImU32 divider;              // Divider lines
    ImU32 outline;              // Outline/border color
    ImU32 scrim;                // Modal overlay background
};

// ============================================================================
// Elevation System (Dark Theme)
// ============================================================================

/**
 * @brief Material Design elevation levels
 * 
 * In dark theme, elevation is shown through surface color lightening
 * rather than shadows.
 */
enum class Elevation {
    Dp0  = 0,     // 0dp  - Background
    Dp1  = 1,     // 1dp  - Cards at rest, Search bar
    Dp2  = 2,     // 2dp  - Buttons at rest
    Dp3  = 3,     // 3dp  - Refresh indicator
    Dp4  = 4,     // 4dp  - App bar
    Dp6  = 6,     // 6dp  - FAB, Snackbar
    Dp8  = 8,     // 8dp  - Bottom sheet, Menu, Cards picked up
    Dp12 = 12,    // 12dp - FAB pressed
    Dp16 = 16,    // 16dp - Navigation drawer, Modal sheets
    Dp24 = 24     // 24dp - Dialog
};

/**
 * @brief Get overlay opacity for elevation level (dark theme)
 * 
 * Material Design uses white overlay on dark surfaces to indicate elevation.
 * Higher elevation = more white overlay = lighter surface.
 */
float GetElevationOverlay(Elevation level);
float GetElevationOverlay(int dp);

/**
 * @brief Calculate surface color for given elevation
 * 
 * Applies white overlay to base surface color based on elevation.
 */
ImU32 GetElevatedSurface(const ColorTheme& theme, Elevation level);
ImU32 GetElevatedSurface(const ColorTheme& theme, int dp);

// ============================================================================
// Predefined Themes
// ============================================================================

/**
 * @brief DragonX branded dark theme with red primary color
 */
ColorTheme GetDragonXColorTheme();

/**
 * @brief Standard Material dark theme
 */
ColorTheme GetMaterialDarkTheme();

/**
 * @brief Standard Material light theme
 */
ColorTheme GetMaterialLightTheme();

// ============================================================================
// Theme Management
// ============================================================================

/**
 * @brief Get current active color theme
 */
const ColorTheme& GetCurrentColorTheme();

/**
 * @brief Set current color theme
 */
void SetCurrentColorTheme(const ColorTheme& theme);

/**
 * @brief Check if current theme is dark mode
 */
bool IsDarkTheme();

/**
 * @brief Set whether OS-level backdrop (DWM Mica/Acrylic) is active
 * 
 * When active, background and surface colors become semi-transparent
 * so the compositor's blur effect shows through.
 */
void SetBackdropActive(bool active);

/**
 * @brief Check if OS-level backdrop is active
 */
bool IsBackdropActive();

/**
 * @brief Apply Material Design color theme to ImGui
 * 
 * This updates ImGui's color palette to use the Material theme colors.
 */
void ApplyColorThemeToImGui(const ColorTheme& theme);

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