/// Storybook Editor Theme
/// Based on the aubergine + gold design system
///
/// Synthesis of Teenage Engineering's bold minimalism and Dieter Rams'
/// functionalist precision.
use iced::color;
use iced::{
    Background,
    Border,
    Color,
    Degrees,
    Gradient,
    Shadow,
    Theme,
    Vector,
    gradient::Linear,
};

/// Aubergine color palette
#[allow(dead_code)]
pub struct Aubergine;

#[allow(dead_code)]
impl Aubergine {
    // Active elements
    pub const _300: Color = color!(0x9f, 0x76, 0xa7);
    // Subtle highlights
    pub const _400: Color = color!(0x80, 0x57, 0x93);
    // Borders, dividers
    pub const _500: Color = color!(0x61, 0x38, 0x6f);
    // Surface hover
    pub const _600: Color = color!(0x4f, 0x2e, 0x5b);
    // Alternate row color
    pub const _700: Color = color!(0x3d, 0x24, 0x47);
    // Surface (panels, cards)
    pub const _750: Color = color!(0x38, 0x24, 0x44);
    // Background (darkest)
    pub const _800: Color = color!(0x2b, 0x1a, 0x33);
    pub const _900: Color = color!(0x1a, 0x0f, 0x1e); // Muted text, info
}

/// Gold/Orange accent palette
#[allow(dead_code)]
pub struct Gold;

#[allow(dead_code)]
impl Gold {
    // Pressed/active state
    pub const _200: Color = color!(0xfa, 0xd6, 0xad);
    // Hover state
    pub const _300: Color = color!(0xf8, 0xc5, 0x94);
    // PRIMARY ACCENT ⭐
    pub const _400: Color = color!(0xf6, 0xb4, 0x7a);
    // Deeper accent (optional)
    pub const _500: Color = color!(0xf4, 0xa2, 0x61);
    pub const _600: Color = color!(0xe8, 0x93, 0x50); // Very subtle highlight
}

/// Neutral colors
#[allow(dead_code)]
pub struct Neutral;

#[allow(dead_code)]
impl Neutral {
    // Disabled elements
    pub const CHARCOAL: Color = color!(0x1f, 0x1f, 0x1f);
    pub const CREAM: Color = color!(0xfd, 0xf8, 0xf3);
    // Primary text
    pub const WARM_GRAY_100: Color = color!(0xe8, 0xe3, 0xdd);
    // Secondary text
    pub const WARM_GRAY_200: Color = color!(0xd1, 0xcb, 0xc3);
    // Tertiary text
    pub const WARM_GRAY_400: Color = color!(0x8b, 0x86, 0x80);
    // Muted text, placeholders
    pub const WARM_GRAY_700: Color = color!(0x4a, 0x48, 0x45); // Pure dark (code bg)
}

/// Semantic colors
#[allow(dead_code)]
pub struct Semantic;

#[allow(dead_code)]
impl Semantic {
    // Warning pressed
    pub const ERROR: Color = color!(0xd6, 0x28, 0x28);
    // Error hover
    pub const ERROR_DARK: Color = color!(0xb8, 0x1e, 0x1e);
    // Parse errors ✗
    pub const ERROR_LIGHT: Color = color!(0xe5, 0x45, 0x45);
    // Error pressed
    pub const INFO: Color = Aubergine::_300;
    pub const SUCCESS: Color = color!(0x6a, 0x99, 0x4e);
    // Success hover
    pub const SUCCESS_DARK: Color = color!(0x55, 0x80, 0x39);
    // Validation passed ✓
    pub const SUCCESS_LIGHT: Color = color!(0x8a, 0xb8, 0x64);
    // Success pressed
    pub const WARNING: Color = Gold::_500;
    // Warning hover
    pub const WARNING_DARK: Color = Gold::_600;
    // Warning, attention ⚠
    pub const WARNING_LIGHT: Color = Gold::_400; // Info, hints ℹ
}

/// Spacing constants following 8px base grid
#[allow(dead_code)]
pub struct Spacing;

#[allow(dead_code)]
impl Spacing {
    // Section spacing
    pub const LARGE: f32 = 24.0;
    // Default gap between related elements
    pub const MEDIUM: f32 = 16.0;
    pub const MICRO: f32 = 4.0;
    // Icon padding, tight spacing
    pub const SMALL: f32 = 8.0;
    // Panel padding
    pub const XLARGE: f32 = 32.0;
    // Major section separation
    pub const XXLARGE: f32 = 48.0; // Page margins, hero spacing
}

/// Create the custom Storybook theme
pub fn storybook_theme() -> Theme {
    Theme::custom(
        "Storybook".to_string(),
        iced::theme::Palette {
            background: Aubergine::_900,
            text: Neutral::CREAM,
            primary: Gold::_500,
            success: Semantic::SUCCESS,
            danger: Semantic::ERROR,
            warning: Semantic::WARNING,
        },
    )
}

/// Helper function for creating gold focus borders with glow
#[allow(dead_code)]
pub fn gold_focus_border() -> Border {
    Border {
        color: Gold::_500,
        width: 2.0,
        radius: 4.0.into(),
    }
}

/// Helper function for creating subtle aubergine borders
pub fn subtle_border() -> Border {
    Border {
        color: Aubergine::_600,
        width: 1.0,
        radius: 4.0.into(),
    }
}

/// Helper function for panel background with shadow
pub fn panel_background() -> Background {
    Background::Color(Aubergine::_800)
}

/// Helper function for gold glow shadow (focus states)
#[allow(dead_code)]
pub fn gold_glow() -> Shadow {
    Shadow {
        color: Color::from_rgba(Gold::_500.r, Gold::_500.g, Gold::_500.b, 0.2),
        offset: Vector::new(0.0, 0.0),
        blur_radius: 8.0,
    }
}

/// Helper function for subtle panel shadow
#[allow(dead_code)]
pub fn panel_shadow() -> Shadow {
    Shadow {
        color: Color::from_rgba(0.0, 0.0, 0.0, 0.3),
        offset: Vector::new(0.0, 2.0),
        blur_radius: 8.0,
    }
}

/// Helper function for left panel gradient background
/// Aubergine (left edge) → Cream (right edge)
pub fn left_panel_gradient() -> Background {
    Background::Gradient(Gradient::Linear(
        Linear::new(Degrees(90.0))
            .add_stop(0.0, Aubergine::_900) // Left edge: dark aubergine
            .add_stop(1.0, Neutral::CREAM),
    )) // Right edge: cream
}

/// Helper function for right panel gradient background
/// Cream (left edge) → Aubergine (right edge)
pub fn right_panel_gradient() -> Background {
    Background::Gradient(Gradient::Linear(
        Linear::new(Degrees(90.0))
            .add_stop(0.0, Neutral::CREAM) // Left edge: cream
            .add_stop(1.0, Aubergine::_900),
    )) // Right edge: dark aubergine
}
