/// Reusable UI components for consistent styling
use iced::widget::{
    Column,
    button,
    column,
    container,
    text,
    text_input,
};
use iced::{
    Background,
    Element,
    Length,
};

use crate::{
    app::Message,
    theme::*,
};

/// Menu bar button with consistent styling
pub fn menu_button<'a>(
    label: &'a str,
    enabled: bool,
    on_press: Option<Message>,
) -> Element<'a, Message> {
    let mut btn = button(text(label).size(14).color(if enabled {
        Neutral::CREAM
    } else {
        Neutral::WARM_GRAY_700
    }))
    .padding([Spacing::MICRO, Spacing::SMALL])
    .style(move |_theme, status| {
        let background = if enabled {
            match status {
                | button::Status::Hovered => Background::Color(Aubergine::_700),
                | button::Status::Pressed => Background::Color(Aubergine::_600),
                | _ => Background::Color(Aubergine::_800),
            }
        } else {
            Background::Color(Aubergine::_800)
        };
        button::Style {
            background: Some(background),
            text_color: if enabled {
                Neutral::CREAM
            } else {
                Neutral::WARM_GRAY_700
            },
            border: subtle_border(),
            ..Default::default()
        }
    });

    if let Some(msg) = on_press {
        btn = btn.on_press(msg);
    }

    btn.into()
}

/// Entity browser item button
pub fn entity_button<'a>(
    label: &'a str,
    is_selected: bool,
    on_press: Message,
) -> Element<'a, Message> {
    button(text(label).size(13).color(if is_selected {
        Gold::_500
    } else {
        Neutral::WARM_GRAY_100
    }))
    .on_press(on_press)
    .padding([Spacing::MICRO, Spacing::SMALL])
    .style(move |_theme, status| button::Style {
        background: if is_selected {
            Some(Background::Color(Aubergine::_600))
        } else {
            match status {
                | button::Status::Hovered => Some(Background::Color(Aubergine::_700)),
                | _ => None,
            }
        },
        text_color: if is_selected {
            Gold::_500
        } else {
            Neutral::WARM_GRAY_100
        },
        ..Default::default()
    })
    .into()
}

/// Quick action button (prominent actions)
pub fn quick_action_button<'a>(
    label: &'a str,
    enabled: bool,
    on_press: Option<Message>,
) -> Element<'a, Message> {
    let mut btn = button(text(label).size(14).color(if enabled {
        Aubergine::_900
    } else {
        Neutral::WARM_GRAY_700
    }))
    .padding([Spacing::SMALL, Spacing::MEDIUM])
    .style(move |_theme, status| {
        let background = if enabled {
            match status {
                | button::Status::Hovered => Background::Color(Gold::_400),
                | button::Status::Pressed => Background::Color(Gold::_600),
                | _ => Background::Color(Gold::_500),
            }
        } else {
            Background::Color(Aubergine::_800)
        };
        button::Style {
            background: Some(background),
            text_color: if enabled {
                Aubergine::_900
            } else {
                Neutral::WARM_GRAY_700
            },
            border: if enabled {
                subtle_border()
            } else {
                iced::Border {
                    color: Aubergine::_700,
                    width: 1.0,
                    radius: 0.0.into(),
                }
            },
            ..Default::default()
        }
    });

    if let Some(msg) = on_press {
        btn = btn.on_press(msg);
    }

    btn.into()
}

/// Section header for entity browser sections
pub fn section_header<'a>(label: &'a str) -> Element<'a, Message> {
    text(label).size(14).color(Gold::_500).into()
}

/// Entity item text (non-clickable)
pub fn entity_item<'a>(label: &'a str) -> Element<'a, Message> {
    text(label).size(13).color(Neutral::WARM_GRAY_100).into()
}

/// Metadata field with soft styling (label + input with subtle underline)
pub fn metadata_field<'a>(label: &'a str, value: &str) -> Column<'a, Message> {
    column![
        // Soft, subtle label
        text(label).size(11).color(Neutral::WARM_GRAY_400),
        // Input with soft underline only
        text_input("", value)
            .padding([Spacing::MICRO, 0.0])
            .size(15)
            .style(|_theme, status| text_input::Style {
                background: Background::Color(iced::Color::TRANSPARENT),
                border: iced::Border {
                    color: match status {
                        | text_input::Status::Focused { .. } => Neutral::WARM_GRAY_200,
                        | text_input::Status::Hovered => Neutral::WARM_GRAY_400,
                        | _ => Neutral::WARM_GRAY_700,
                    },
                    width: 1.0,
                    radius: 0.0.into(),
                },
                icon: iced::Color::TRANSPARENT,
                placeholder: Neutral::WARM_GRAY_700,
                value: Neutral::CREAM,
                selection: Neutral::WARM_GRAY_400,
            })
    ]
    .spacing(Spacing::MICRO)
}

/// Side panel container (entity browser, quick actions)
pub fn side_panel<'a>(
    content: impl Into<Element<'a, Message>>,
    width: u16,
) -> Element<'a, Message> {
    container(content)
        .width(Length::Fixed(width as f32))
        .height(Length::Fill)
        .style(|_theme| container::Style {
            background: Some(panel_background()),
            border: subtle_border(),
            ..Default::default()
        })
        .into()
}

/// Main content panel (editor area)
pub fn main_panel<'a>(content: impl Into<Element<'a, Message>>) -> Element<'a, Message> {
    container(content)
        .width(Length::Fill)
        .height(Length::Fill)
        .style(|_theme| container::Style {
            background: Some(Background::Color(Aubergine::_900)),
            ..Default::default()
        })
        .into()
}
