use super::styles::{
    self, GRAY_30, GRAY_40, GRAY_60, WATCHED_GREEN, WATCHED_GREEN_HOVER, card_button_style,
};
use iced::Element;

pub(crate) const COVER_WIDTH: f32 = 180.0;
pub(crate) const COVER_HEIGHT: f32 = 270.0;

pub(crate) fn media_card<'a, Msg: Clone + 'a>(
    cover: Element<'a, Msg>,
    label: Element<'a, Msg>,
    width: iced::Length,
    on_press: Msg,
) -> Element<'a, Msg> {
    iced::widget::button(
        iced::widget::column![cover, label]
            .spacing(styles::SPACING_TIGHT)
            .width(width),
    )
    .on_press(on_press)
    .padding(iced::Padding::new(styles::PADDING_CARD))
    .style(card_button_style)
    .into()
}

pub(crate) fn cover_with_watched_badge<'a, Msg: 'a>(
    cover: Element<'a, Msg>,
    watched: bool,
    width: f32,
    height: f32,
    ui_scale: f32,
) -> Element<'a, Msg> {
    if watched {
        iced::widget::stack![cover, watched_badge(width, height, ui_scale)].into()
    } else {
        cover
    }
}

pub(crate) fn watched_badge<'a, Msg: 'a>(
    width: f32,
    height: f32,
    ui_scale: f32,
) -> Element<'a, Msg> {
    let badge_size = 24.0 * ui_scale;
    let badge = iced::widget::container(
        iced::widget::text("✓")
            .size(14.0 * ui_scale)
            .color(iced::Color::WHITE),
    )
    .width(badge_size)
    .height(badge_size)
    .center_x(badge_size)
    .center_y(badge_size)
    .style(move |_theme| iced::widget::container::Style {
        background: Some(iced::Background::Color(WATCHED_GREEN)),
        border: iced::border::rounded(badge_size / 2.0),
        ..Default::default()
    });

    iced::widget::container(badge)
        .width(width)
        .height(height)
        .align_right(width)
        .padding(iced::Padding::from([4.0, 4.0]))
        .into()
}

pub(crate) fn cover_placeholder<'a, Msg: 'a>(
    title: &str,
    year: &str,
    width: f32,
    height: f32,
    ui_scale: f32,
) -> Element<'a, Msg> {
    iced::widget::container(
        iced::widget::column![
            iced::widget::text(title.to_string())
                .size(styles::TEXT_SIZE_SMALL * ui_scale)
                .color(GRAY_60)
                .wrapping(iced::widget::text::Wrapping::WordOrGlyph),
            iced::widget::text(year.to_string())
                .size(10.0 * ui_scale)
                .color(GRAY_40),
        ]
        .spacing(styles::SPACING_TIGHT)
        .padding(iced::Padding::new(styles::PADDING_SMALL)),
    )
    .width(width)
    .height(height)
    .style(styles::placeholder_style)
    .into()
}

pub(crate) fn watched_toggle_button<Msg: Clone + 'static>(
    watched: bool,
    on_press: Msg,
    ui_scale: f32,
) -> Element<'static, Msg> {
    let (label, bg, hover_bg) = if watched {
        ("✓ Watched", WATCHED_GREEN, WATCHED_GREEN_HOVER)
    } else {
        ("○ Unwatched", GRAY_30, GRAY_40)
    };

    iced::widget::button(
        iced::widget::text(label)
            .size(styles::TEXT_SIZE_BODY * ui_scale)
            .color(iced::Color::WHITE),
    )
    .on_press(on_press)
    .padding(iced::Padding::from([8.0, 24.0]))
    .style(move |_theme, status| {
        let bg = match status {
            iced::widget::button::Status::Hovered => hover_bg,
            _ => bg,
        };
        iced::widget::button::Style {
            background: Some(iced::Background::Color(bg)),
            text_color: iced::Color::WHITE,
            border: iced::border::rounded(6),
            ..Default::default()
        }
    })
    .into()
}

pub(crate) fn watched_toggle_compact<Msg: Clone + 'static>(
    watched: bool,
    on_press: Msg,
    ui_scale: f32,
) -> Element<'static, Msg> {
    let (label, bg, hover_bg) = if watched {
        ("✓", WATCHED_GREEN, WATCHED_GREEN_HOVER)
    } else {
        ("○", GRAY_30, GRAY_40)
    };

    iced::widget::button(
        iced::widget::text(label)
            .size(styles::TEXT_SIZE_SMALL * ui_scale)
            .color(iced::Color::WHITE),
    )
    .on_press(on_press)
    .padding(iced::Padding::from([4.0, 12.0]))
    .style(move |_theme, status| {
        let bg = match status {
            iced::widget::button::Status::Hovered => hover_bg,
            _ => bg,
        };
        iced::widget::button::Style {
            background: Some(iced::Background::Color(bg)),
            text_color: iced::Color::WHITE,
            border: iced::border::rounded(styles::RADIUS_STANDARD),
            ..Default::default()
        }
    })
    .into()
}
