use data::{Config, Preview, preview};
use iced::widget::{column, container, text};
use iced::{ContentFit, Padding};

use super::Element;
use crate::widget::image;
use crate::{Theme, font, theme};

pub fn preview_content<'a, M: 'a>(
    preview: &'a Preview,
    config: &'a Config,
    theme: &'a Theme,
) -> Element<'a, M> {
    match preview {
        Preview::Card(preview::Card {
            image: card_image,
            title,
            description,
            ..
        }) => container(
            column![
                text(title)
                    .shaping(text::Shaping::Advanced)
                    .style(theme::text::primary)
                    .font_maybe(
                        theme::font_style::primary(theme).map(font::get)
                    ),
                description.as_ref().map(|description| {
                    container(
                        text(description)
                            .shaping(text::Shaping::Advanced)
                            .wrapping(text::Wrapping::WordOrGlyph)
                            .style(theme::text::secondary)
                            .font_maybe(
                                theme::font_style::secondary(theme)
                                    .map(font::get),
                            ),
                    )
                    .clip(false)
                    .max_height(config.preview.card.description_max_height)
                }),
                config.preview.card.show_image.then_some(
                    container(image::from_data(
                        card_image,
                        config.preview.card.round_image_corners,
                        ContentFit::ScaleDown,
                    ))
                    .padding(Padding::default().top(8))
                    .max_height(config.preview.card.image_max_height),
                ),
            ]
            .spacing(8)
            .max_width(config.preview.card.max_width),
        )
        .padding(8)
        .into(),

        Preview::Image(img) => container(image::from_data(
            img,
            config.preview.image.round_corners,
            ContentFit::ScaleDown,
        ))
        .max_width(config.preview.image.max_width)
        .max_height(config.preview.image.max_height)
        .into(),
    }
}
