//! Bottom toolbar — gpicview-style, with SVG icons.
//!
//! In marten v0.2 the layout was flipped: thumbnail bar is now at the TOP
//! and the toolbar (this module) is at the BOTTOM. This gives the image
//! area more vertical room and matches the gpicview mental model where
//! the toolbar sits below the content.

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

use super::icons::Icon;
use super::theme;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ToolbarMessage {
    Prev,
    Next,
    FitToWindow,
    ActualSize,
    ZoomIn,
    ZoomOut,
    RotateCw,
    RotateCcw,
    ToggleFullscreen,
    OpenFolder,
    About,
    Shuffle,
}

const ICON_SIZE: f32 = 16.0;
const BTN_HEIGHT: f32 = 28.0;

fn icon_btn(icon: Icon, msg: ToolbarMessage, disabled: bool) -> button::Button<'static, ToolbarMessage> {
    let color = if disabled {
        theme::text_dim_hex()
    } else {
        theme::text_primary_hex()
    };

    let content: Element<'static, ToolbarMessage> = icon
        .widget(ICON_SIZE, &color)
        .into();

    button(content)
        .on_press_maybe(if disabled { None } else { Some(msg) })
        .height(Length::Fixed(BTN_HEIGHT))
        .padding(iced::Padding {
            top: 4.0,
            right: 8.0,
            bottom: 4.0,
            left: 8.0,
        })
        .style(move |_theme, status| {
            let bg = match status {
                button::Status::Hovered => Some(theme::BG_CHROME_HOVER),
                button::Status::Pressed => Some(theme::ACCENT_DIM),
                _ => None,
            };
            button::Style {
                background: bg.map(iced::Background::Color),
                text_color: theme::TEXT_PRIMARY,
                border: iced::Border {
                    radius: 4.0.into(),
                    ..Default::default()
                },
                ..Default::default()
            }
        })
}

fn text_btn(label: &'static str, msg: ToolbarMessage, disabled: bool) -> button::Button<'static, ToolbarMessage> {
    let txt = text(label)
        .color(if disabled {
            theme::TEXT_DIM
        } else {
            theme::TEXT_PRIMARY
        })
        .size(12);

    button(txt)
        .on_press_maybe(if disabled { None } else { Some(msg) })
        .height(Length::Fixed(BTN_HEIGHT))
        .padding(iced::Padding {
            top: 4.0,
            right: 8.0,
            bottom: 4.0,
            left: 8.0,
        })
        .style(move |_theme, status| {
            let bg = match status {
                button::Status::Hovered => Some(theme::BG_CHROME_HOVER),
                button::Status::Pressed => Some(theme::ACCENT_DIM),
                _ => None,
            };
            button::Style {
                background: bg.map(iced::Background::Color),
                text_color: theme::TEXT_PRIMARY,
                border: iced::Border {
                    radius: 4.0.into(),
                    ..Default::default()
                },
                ..Default::default()
            }
        })
}

pub fn view(disabled: bool) -> Element<'static, ToolbarMessage> {
    let spacer = || text("").width(Length::Fixed(8.0));
    let fill_spacer = || text("").width(Length::Fill);

    // Left group: navigation + zoom
    let left_group = row![
        icon_btn(Icon::ChevronLeft, ToolbarMessage::Prev, disabled),
        icon_btn(Icon::ChevronRight, ToolbarMessage::Next, disabled),
        icon_btn(Icon::Shuffle, ToolbarMessage::Shuffle, disabled),
        spacer(),
        icon_btn(Icon::Fit, ToolbarMessage::FitToWindow, disabled),
        icon_btn(Icon::ActualSize, ToolbarMessage::ActualSize, disabled),
        icon_btn(Icon::ZoomIn, ToolbarMessage::ZoomIn, disabled),
        icon_btn(Icon::ZoomOut, ToolbarMessage::ZoomOut, disabled),
        spacer(),
        icon_btn(Icon::RotateCw, ToolbarMessage::RotateCw, disabled),
        icon_btn(Icon::RotateCcw, ToolbarMessage::RotateCcw, disabled),
        spacer(),
        icon_btn(Icon::Fullscreen, ToolbarMessage::ToggleFullscreen, disabled),
    ]
    .spacing(2.0)
    .align_y(iced::Alignment::Center);

    // Right group: Open + About
    let right_group = row![
        text_btn("Open", ToolbarMessage::OpenFolder, false),
        spacer(),
        icon_btn(Icon::Info, ToolbarMessage::About, false),
    ]
    .spacing(2.0)
    .align_y(iced::Alignment::Center);

    let content = row![
        left_group,
        fill_spacer(),
        right_group,
    ]
    .padding(iced::Padding {
        top: 0.0,
        right: 8.0,
        bottom: 0.0,
        left: 8.0,
    })
    .spacing(2.0)
    .align_y(iced::Alignment::Center);

    container(content)
        .width(Length::Fill)
        .height(Length::Fixed(34.0))
        .align_y(iced::Alignment::Center)
        .style(|_| container::Style {
            background: Some(theme::BG_CHROME.into()),
            border: iced::Border {
                color: theme::BG_CHROME_BORDER,
                width: 0.0,
                radius: 0.0.into(),
            },
            ..Default::default()
        })
        .into()
}
