//! About dialog — matches the ferret app's About style.
//!
//! Layout (top to bottom, left-aligned):
//!   marten   <version>                         [×]
//!   ─────────────────────────────────────────
//!   A modern, accuracy-first image viewer for Linux.
//!
//!   Author:    Jeremy Anderson
//!   Website:   http://git.dcos.net/dcosnet/marten
//!   License:   GPL-2.0-or-later
//!
//!   Built with Rust, iced, wgpu, and winit.
//!   Copyright © 2026 Jeremy Anderson.

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

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

const ACCENT_ORANGE: iced::Color = iced::Color::from_rgb(0.824, 0.412, 0.118); // #d2691e

#[derive(Debug, Clone, Default)]
pub struct AboutDialog {
    pub visible: bool,
}

#[derive(Debug, Clone, Copy)]
pub enum AboutMessage {
    Dismiss,
    OpenWebsite,
}

impl AboutDialog {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn show(&mut self) {
        self.visible = true;
    }

    pub fn dismiss(&mut self) {
        self.visible = false;
    }

    pub fn view(&self) -> Option<Element<'_, AboutMessage>> {
        if !self.visible {
            return None;
        }

        let version = env!("CARGO_PKG_VERSION");

        // Header: app name (orange, large) + version (grey, small) + close button
        let title = text("marten")
            .color(ACCENT_ORANGE)
            .size(28);

        let version_text = text(version)
            .color(theme::TEXT_SECONDARY)
            .size(14);

        let close_btn = button(Icon::Close.widget(16.0, &theme::text_secondary_hex()))
            .on_press(AboutMessage::Dismiss)
            .padding(iced::Padding {
                top: 4.0,
                right: 6.0,
                bottom: 4.0,
                left: 6.0,
            })
            .style(|_theme, status| {
                let bg = match status {
                    button::Status::Hovered => Some(theme::BG_CHROME_HOVER),
                    _ => None,
                };
                button::Style {
                    background: bg.map(iced::Background::Color),
                    border: iced::Border {
                        radius: 4.0.into(),
                        ..Default::default()
                    },
                    ..Default::default()
                }
            });

        let header = row![
            title,
            version_text,
            row![].width(Length::Fill), // spacer
            close_btn,
        ]
        .align_y(iced::Alignment::Center)
        .spacing(8.0);

        // Separator
        let separator = container(text("").height(Length::Fixed(1.0)))
            .width(Length::Fill)
            .height(Length::Fixed(1.0))
            .style(|_| container::Style {
                background: Some(theme::BG_CHROME_BORDER.into()),
                ..Default::default()
            });

        // Tagline
        let tagline = text("A modern, accuracy-first image viewer for Linux.")
            .color(theme::TEXT_PRIMARY)
            .size(13);

        // Info rows: Author / Website / License
        let label = |s: &'static str| {
            text(s)
                .color(theme::TEXT_SECONDARY)
                .size(12)
                .width(Length::Fixed(70.0))
        };
        let value = |s: String| {
            text(s)
                .color(theme::TEXT_PRIMARY)
                .size(12)
        };

        let author_row = row![
            label("Author:"),
            value("Jeremy Anderson".to_string()),
        ]
        .spacing(4.0);

        let website_value = text("http://git.dcos.net/dcosnet/marten")
            .color(ACCENT_ORANGE)
            .size(12);
        let website_btn = button(website_value)
            .on_press(AboutMessage::OpenWebsite)
            .padding(iced::Padding {
                top: 0.0,
                right: 0.0,
                bottom: 0.0,
                left: 0.0,
            })
            .style(|_theme, _status| button::Style {
                background: Some(iced::Color::TRANSPARENT.into()),
                border: iced::Border::default(),
                ..Default::default()
            });
        let website_row = row![label("Website:"), website_btn].spacing(4.0);

        let license_row = row![
            label("License:"),
            value("GPL-2.0-or-later".to_string()),
        ]
        .spacing(4.0);

        // Footer separator
        let footer_sep = container(text("").height(Length::Fixed(1.0)))
            .width(Length::Fill)
            .height(Length::Fixed(1.0))
            .style(|_| container::Style {
                background: Some(theme::BG_CHROME_BORDER.into()),
                ..Default::default()
            });

        // Footer: build info + copyright
        let build_info = text("Built with Rust, iced, wgpu, and winit.")
            .color(theme::TEXT_DIM)
            .size(11);
        let copyright = text("Copyright © 2026 Jeremy Anderson.")
            .color(theme::TEXT_DIM)
            .size(11);

        let card_content = column![
            header,
            text("").height(Length::Fixed(12.0)),
            separator,
            text("").height(Length::Fixed(12.0)),
            tagline,
            text("").height(Length::Fixed(16.0)),
            author_row,
            website_row,
            license_row,
            text("").height(Length::Fixed(16.0)),
            footer_sep,
            text("").height(Length::Fixed(8.0)),
            build_info,
            copyright,
        ]
        .padding(iced::Padding {
            top: 20.0,
            right: 24.0,
            bottom: 20.0,
            left: 24.0,
        });

        // Card with orange border (matching ferret style)
        let card = container(card_content)
            .max_width(440.0)
            .style(|_| container::Style {
                background: Some(theme::BG_CHROME.into()),
                border: iced::Border {
                    color: ACCENT_ORANGE,
                    width: 1.0,
                    radius: 6.0.into(),
                },
                shadow: iced::Shadow {
                    color: iced::Color::from_rgba(0.0, 0.0, 0.0, 0.7),
                    offset: iced::Vector::new(0.0, 8.0),
                    blur_radius: 32.0,
                },
                ..Default::default()
            });

        // Center the card in a full-screen overlay backdrop.
        let centered = container(card)
            .width(Length::Fill)
            .height(Length::Fill)
            .align_x(iced::Alignment::Center)
            .align_y(iced::Alignment::Center)
            .style(|_| container::Style {
                background: Some(
                    iced::Color::from_rgba(0.0, 0.0, 0.0, 0.75).into(),
                ),
                ..Default::default()
            });

        Some(centered.into())
    }
}
