use iced::{
    Background,
    Element,
    Length,
    widget::{
        button,
        container,
        mouse_area,
        row,
        text,
    },
};

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

pub fn tab_bar(editor: &Editor) -> Element<'_, Message> {
    if editor.tabs.is_empty() {
        return container(text("")).width(Length::Fill).height(0).into();
    }

    let mut tabs_row = row![].spacing(Spacing::MICRO);

    for (idx, tab) in editor.tabs.iter().enumerate() {
        let is_active = editor.active_tab == Some(idx);

        let tab_content = row![
            text(tab.title()).size(13).color(if is_active {
                Gold::_500
            } else {
                Neutral::WARM_GRAY_100
            }),
            button(text("×").size(14).color(Neutral::WARM_GRAY_400))
                .on_press(Message::CloseTab(idx))
                .padding([0, 4])
                .style(|_theme, status| button::Style {
                    background: match status {
                        | button::Status::Hovered => Some(Background::Color(Aubergine::_600)),
                        | _ => None,
                    },
                    text_color: Neutral::WARM_GRAY_400,
                    ..Default::default()
                }),
        ]
        .spacing(Spacing::SMALL);

        let tab_button = button(tab_content)
            .on_press(Message::SwitchTab(idx))
            .padding([Spacing::SMALL, Spacing::MEDIUM])
            .style(move |_theme, status| button::Style {
                background: if is_active {
                    Some(Background::Color(Aubergine::_900))
                } else {
                    match status {
                        | button::Status::Hovered => Some(Background::Color(Aubergine::_700)),
                        | button::Status::Pressed => Some(Background::Color(Aubergine::_700)),
                        | _ => None,
                    }
                },
                text_color: if is_active {
                    Gold::_500
                } else {
                    Neutral::WARM_GRAY_100
                },
                border: iced::Border {
                    color: iced::Color::TRANSPARENT,
                    width: 0.0,
                    radius: 0.0.into(),
                },
                ..Default::default()
            });

        // Wrap in mouse_area to track hover state for middle-click
        let tab_with_hover = mouse_area(tab_button)
            .on_enter(Message::TabHovered(Some(idx)))
            .on_exit(Message::TabHovered(None));

        tabs_row = tabs_row.push(tab_with_hover);
    }

    container(tabs_row)
        .width(Length::Fill)
        .padding([Spacing::MICRO, Spacing::SMALL])
        .style(|_theme| container::Style {
            background: Some(Background::Color(Aubergine::_800)),
            border: iced::Border {
                color: iced::Color::TRANSPARENT,
                width: 0.0,
                radius: 0.0.into(),
            },
            ..Default::default()
        })
        .into()
}
