mod button;
mod checkbox;
mod input;
mod radio;
mod scrollbar;
mod select;
mod switch;
mod text;

use crate::foundation::color::Color;
use crate::ui::theme::color::ColorScheme;
use crate::ui::theme::motion::MotionScale;
use crate::ui::theme::shape::{BorderScale, ElevationScale, RadiusScale};
use crate::ui::theme::spacing::SpaceScale;
use crate::ui::theme::state::Stateful;
use crate::ui::theme::typography::TypeScale;
use crate::ui::unit::Dp;

pub use button::{ButtonStyle, ButtonTheme, ButtonVariant};
pub use checkbox::{CheckboxStyle, CheckboxTheme};
pub use input::{InputStyle, InputTheme};
pub use radio::{RadioStyle, RadioTheme};
pub use scrollbar::ScrollbarTheme;
pub use select::{SelectStyle, SelectTheme};
pub use switch::{SwitchStyle, SwitchTheme};
pub use text::TextTheme;

#[derive(Clone, Debug, PartialEq)]
pub struct ComponentTheme {
    pub button: ButtonTheme,
    pub checkbox: CheckboxTheme,
    pub radio: RadioTheme,
    pub input: InputTheme,
    pub select: SelectTheme,
    pub text: TextTheme,
    pub switch: SwitchTheme,
    pub scrollbar: ScrollbarTheme,
}

impl ComponentTheme {
    pub fn from_tokens(
        colors: &ColorScheme,
        typography: &TypeScale,
        spacing: &SpaceScale,
        radius: &RadiusScale,
        border: &BorderScale,
        _elevation: &ElevationScale,
        _motion: &MotionScale,
    ) -> Self {
        const HOVER_LIGHTEN: f32 = 0.1;
        const SURFACE_HOVER_LIGHTEN: f32 = 0.06;
        const BORDER_HOVER_LIGHTEN: f32 = 0.12;
        const SCROLLBAR_HOVER_LIGHTEN: f32 = 0.18;

        let disabled_surface = colors.disabled;
        let disabled_content = colors.on_disabled;
        Self {
            button: ButtonTheme {
                primary: ButtonVariant {
                    container: Stateful {
                        normal: colors.primary,
                        hovered: colors.primary.lighten(HOVER_LIGHTEN),
                        pressed: colors.primary.darken(HOVER_LIGHTEN),
                        focused: colors.primary,
                        disabled: disabled_surface,
                    },
                    content: Stateful {
                        normal: colors.on_primary,
                        hovered: colors.on_primary,
                        pressed: colors.on_primary,
                        focused: colors.on_primary,
                        disabled: disabled_content,
                    },
                    border: Stateful {
                        normal: colors.primary,
                        hovered: colors.primary.lighten(HOVER_LIGHTEN),
                        pressed: colors.primary.darken(HOVER_LIGHTEN),
                        focused: colors.focus_ring,
                        disabled: disabled_surface,
                    },
                    border_width: Dp::ZERO,
                    radius: radius.md,
                    padding_x: spacing.sm,
                    padding_y: spacing.xs,
                    min_height: spacing.xl,
                    text_style: typography.label.clone(),
                },
                secondary: ButtonVariant {
                    container: Stateful {
                        normal: colors.surface_high,
                        hovered: colors.surface_high.lighten(SURFACE_HOVER_LIGHTEN),
                        pressed: colors.surface_high.darken(SURFACE_HOVER_LIGHTEN),
                        focused: colors.surface_high,
                        disabled: disabled_surface,
                    },
                    content: Stateful {
                        normal: colors.on_surface,
                        hovered: colors.on_surface,
                        pressed: colors.on_surface,
                        focused: colors.on_surface,
                        disabled: disabled_content,
                    },
                    border: Stateful {
                        normal: colors.outline,
                        hovered: colors.outline.lighten(BORDER_HOVER_LIGHTEN),
                        pressed: colors.outline.darken(BORDER_HOVER_LIGHTEN),
                        focused: colors.outline,
                        disabled: disabled_surface,
                    },
                    border_width: border.thin,
                    radius: radius.md,
                    padding_x: spacing.sm,
                    padding_y: spacing.xs,
                    min_height: spacing.xl,
                    text_style: typography.label.clone(),
                },
                ghost: ButtonVariant {
                    container: Stateful {
                        normal: Color::TRANSPARENT,
                        hovered: colors.surface_low.lighten(SURFACE_HOVER_LIGHTEN),
                        pressed: colors.surface_low.darken(SURFACE_HOVER_LIGHTEN),
                        focused: colors.surface_low,
                        disabled: Color::TRANSPARENT,
                    },
                    content: Stateful {
                        normal: colors.on_surface,
                        hovered: colors.on_surface,
                        pressed: colors.on_surface,
                        focused: colors.on_surface,
                        disabled: disabled_content,
                    },
                    border: Stateful {
                        normal: Color::TRANSPARENT,
                        hovered: Color::TRANSPARENT,
                        pressed: Color::TRANSPARENT,
                        focused: colors.focus_ring,
                        disabled: Color::TRANSPARENT,
                    },
                    border_width: Dp::ZERO,
                    radius: radius.md,
                    padding_x: spacing.sm,
                    padding_y: spacing.xs,
                    min_height: spacing.xl,
                    text_style: typography.label.clone(),
                },
                danger: ButtonVariant {
                    container: Stateful {
                        normal: colors.error,
                        hovered: colors.error.lighten(HOVER_LIGHTEN),
                        pressed: colors.error.darken(HOVER_LIGHTEN),
                        focused: colors.error,
                        disabled: disabled_surface,
                    },
                    content: Stateful {
                        normal: colors.on_error,
                        hovered: colors.on_error,
                        pressed: colors.on_error,
                        focused: colors.on_error,
                        disabled: disabled_content,
                    },
                    border: Stateful {
                        normal: colors.error,
                        hovered: colors.error.lighten(HOVER_LIGHTEN),
                        pressed: colors.error.darken(HOVER_LIGHTEN),
                        focused: colors.focus_ring,
                        disabled: disabled_surface,
                    },
                    border_width: Dp::ZERO,
                    radius: radius.md,
                    padding_x: spacing.sm,
                    padding_y: spacing.xs,
                    min_height: spacing.xl,
                    text_style: typography.label.clone(),
                },
            },
            checkbox: CheckboxTheme {
                background: Stateful {
                    normal: colors.surface_low,
                    hovered: colors.surface_low,
                    pressed: colors.surface_low,
                    focused: colors.surface_low,
                    disabled: disabled_surface,
                },
                background_checked: Stateful {
                    normal: colors.primary,
                    hovered: colors.primary,
                    pressed: colors.primary,
                    focused: colors.primary,
                    disabled: disabled_surface,
                },
                border: Stateful {
                    normal: colors.outline,
                    hovered: colors.primary,
                    pressed: colors.primary,
                    focused: colors.outline,
                    disabled: colors.disabled,
                },
                border_checked: Stateful {
                    normal: colors.primary,
                    hovered: colors.primary,
                    pressed: colors.primary,
                    focused: colors.primary,
                    disabled: colors.disabled,
                },
                checkmark: Stateful {
                    normal: colors.on_primary,
                    hovered: colors.on_primary,
                    pressed: colors.on_primary,
                    focused: colors.on_primary,
                    disabled: disabled_content,
                },
                label: Stateful {
                    normal: colors.on_surface,
                    hovered: colors.on_surface,
                    pressed: colors.on_surface,
                    focused: colors.on_surface,
                    disabled: disabled_content,
                },
                border_width: border.thin,
                radius: radius.sm,
                size: spacing.md,
                label_gap: spacing.xs,
                text_style: typography.label.clone(),
            },
            radio: RadioTheme {
                background: Stateful {
                    normal: colors.surface_low,
                    hovered: colors.surface_low.lighten(SURFACE_HOVER_LIGHTEN),
                    pressed: colors.surface_low.darken(SURFACE_HOVER_LIGHTEN),
                    focused: colors.surface_low,
                    disabled: disabled_surface,
                },
                background_checked: Stateful {
                    normal: colors.surface_low,
                    hovered: colors.surface_low.lighten(SURFACE_HOVER_LIGHTEN),
                    pressed: colors.surface_low.darken(SURFACE_HOVER_LIGHTEN),
                    focused: colors.surface_low,
                    disabled: disabled_surface,
                },
                border: Stateful {
                    normal: colors.outline,
                    hovered: colors.outline.lighten(BORDER_HOVER_LIGHTEN),
                    pressed: colors.outline.darken(BORDER_HOVER_LIGHTEN),
                    focused: colors.outline,
                    disabled: colors.disabled,
                },
                border_checked: Stateful {
                    normal: colors.primary,
                    hovered: colors.primary.lighten(HOVER_LIGHTEN),
                    pressed: colors.primary.darken(HOVER_LIGHTEN),
                    focused: colors.focus_ring,
                    disabled: colors.disabled,
                },
                indicator: Stateful {
                    normal: colors.primary,
                    hovered: colors.primary.lighten(HOVER_LIGHTEN),
                    pressed: colors.primary.darken(HOVER_LIGHTEN),
                    focused: colors.primary,
                    disabled: disabled_content,
                },
                label: Stateful {
                    normal: colors.on_surface,
                    hovered: colors.on_surface,
                    pressed: colors.on_surface,
                    focused: colors.on_surface,
                    disabled: disabled_content,
                },
                border_width: border.thin,
                radius: radius.full,
                size: spacing.md,
                label_gap: spacing.xs,
                text_style: typography.label.clone(),
            },
            input: InputTheme {
                background: Stateful {
                    normal: colors.surface_low,
                    hovered: colors.surface_low.lighten(SURFACE_HOVER_LIGHTEN),
                    pressed: colors.surface_low.darken(SURFACE_HOVER_LIGHTEN),
                    focused: colors.surface,
                    disabled: colors.disabled,
                },
                text: Stateful {
                    normal: colors.on_surface,
                    hovered: colors.on_surface,
                    pressed: colors.on_surface,
                    focused: colors.on_surface,
                    disabled: colors.on_disabled,
                },
                placeholder: Stateful {
                    normal: colors.on_surface_muted,
                    hovered: colors.on_surface_muted,
                    pressed: colors.on_surface_muted,
                    focused: colors.on_surface_muted,
                    disabled: colors.on_disabled,
                },
                border: Stateful {
                    normal: colors.outline,
                    hovered: colors.outline.lighten(BORDER_HOVER_LIGHTEN),
                    pressed: colors.outline.darken(BORDER_HOVER_LIGHTEN),
                    focused: colors.focus_ring,
                    disabled: colors.disabled,
                },
                cursor: colors.on_surface,
                selection: colors.selection,
                radius: radius.md,
                padding_x: spacing.sm,
                padding_y: spacing.sm,
                min_height: spacing.xl,
                text_style: typography.body.clone(),
            },
            select: SelectTheme {
                background: Stateful {
                    normal: colors.surface_low,
                    hovered: colors.surface_low.lighten(SURFACE_HOVER_LIGHTEN),
                    pressed: colors.surface_low.darken(SURFACE_HOVER_LIGHTEN),
                    focused: colors.surface,
                    disabled: colors.disabled,
                },
                text: Stateful {
                    normal: colors.on_surface,
                    hovered: colors.on_surface,
                    pressed: colors.on_surface,
                    focused: colors.on_surface,
                    disabled: colors.on_disabled,
                },
                placeholder: Stateful {
                    normal: colors.on_surface_muted,
                    hovered: colors.on_surface_muted,
                    pressed: colors.on_surface_muted,
                    focused: colors.on_surface_muted,
                    disabled: colors.on_disabled,
                },
                border: Stateful {
                    normal: colors.outline,
                    hovered: colors.outline.lighten(BORDER_HOVER_LIGHTEN),
                    pressed: colors.outline.darken(BORDER_HOVER_LIGHTEN),
                    focused: colors.focus_ring,
                    disabled: colors.disabled,
                },
                arrow: Stateful {
                    normal: colors.on_surface_muted,
                    hovered: colors.on_surface,
                    pressed: colors.on_surface,
                    focused: colors.on_surface,
                    disabled: colors.on_disabled,
                },
                menu_background: colors.surface,
                option_background: Stateful {
                    normal: Color::TRANSPARENT,
                    hovered: colors.surface_high.lighten(SURFACE_HOVER_LIGHTEN),
                    pressed: colors.surface_high.darken(SURFACE_HOVER_LIGHTEN),
                    focused: colors.surface_high,
                    disabled: Color::TRANSPARENT,
                },
                selected_option_background: colors.surface_high,
                border_width: border.thin,
                radius: radius.md,
                padding_x: spacing.sm,
                padding_y: spacing.xs,
                min_height: spacing.xl,
                option_height: spacing.xl,
                menu_gap: spacing.xxs,
                text_style: typography.body.clone(),
            },
            text: TextTheme {
                default: typography.body.clone(),
                muted_color: colors.on_surface_muted,
                primary_color: colors.primary,
            },
            switch: SwitchTheme {
                track: Stateful {
                    normal: colors.surface_high,
                    hovered: colors.surface_high.lighten(SURFACE_HOVER_LIGHTEN),
                    pressed: colors.surface_high.darken(SURFACE_HOVER_LIGHTEN),
                    focused: colors.surface_overlay,
                    disabled: colors.disabled,
                },
                track_checked: Stateful {
                    normal: colors.primary,
                    hovered: colors.primary.lighten(HOVER_LIGHTEN),
                    pressed: colors.primary.darken(HOVER_LIGHTEN),
                    focused: colors.primary,
                    disabled: colors.disabled,
                },
                thumb: Stateful {
                    normal: colors.on_surface,
                    hovered: colors.on_surface,
                    pressed: colors.on_surface,
                    focused: colors.on_surface,
                    disabled: colors.on_disabled,
                },
                thumb_checked: Stateful {
                    normal: colors.on_primary,
                    hovered: colors.on_primary,
                    pressed: colors.on_primary,
                    focused: colors.on_primary,
                    disabled: colors.on_disabled,
                },
                border: Stateful {
                    normal: colors.outline_muted,
                    hovered: colors.outline_muted.lighten(BORDER_HOVER_LIGHTEN),
                    pressed: colors.outline_muted.darken(BORDER_HOVER_LIGHTEN),
                    focused: colors.focus_ring,
                    disabled: colors.disabled,
                },
                border_checked: Stateful {
                    normal: colors.primary,
                    hovered: colors.primary.lighten(HOVER_LIGHTEN),
                    pressed: colors.primary.darken(HOVER_LIGHTEN),
                    focused: colors.focus_ring,
                    disabled: colors.disabled,
                },
                border_width: border.none,
                radius: radius.full,
                padding: crate::ui::layout::Insets::all(spacing.xxs),
                width: spacing.xl + spacing.sm + spacing.xxs,
                height: spacing.lg,
            },
            scrollbar: ScrollbarTheme {
                track: Stateful {
                    normal: colors.surface_low,
                    hovered: colors.surface_low.lighten(SURFACE_HOVER_LIGHTEN),
                    pressed: colors.surface_low.darken(SURFACE_HOVER_LIGHTEN),
                    focused: colors.surface_low,
                    disabled: colors.surface_low,
                },
                thumb: Stateful {
                    normal: colors.outline.with_alpha_factor(0.72),
                    hovered: colors
                        .outline
                        .with_alpha_factor(0.72)
                        .lighten(SCROLLBAR_HOVER_LIGHTEN),
                    pressed: colors
                        .outline
                        .with_alpha_factor(0.72)
                        .darken(SCROLLBAR_HOVER_LIGHTEN),
                    focused: colors.on_surface_muted,
                    disabled: colors.disabled,
                },
                width: spacing.sm,
                radius: radius.full,
            },
        }
    }
}
