use crate::db::FeedItem;

use super::db;
use super::ui;
use iced::widget::scrollable;
use rss_content::Content;
use ui::Message;
use rss_content;
use iced::{
    widget::{button, column, container, text},
    Element,
};

pub fn list_feeds() -> iced::widget::Column<'static, Message> {
    let feeds = db::get_feeds();
    column(
        feeds
            .iter()
            .map(|f| {
                button(text(f.title.clone())).on_press(Message::LoadFeed(f.feed_id))
            })
            .map(Element::from),
    )
    .align_x(iced::Alignment::Start)
    .spacing(5)
    .padding(15)
}

pub fn list_items(feed_id: usize) -> iced::widget::Column<'static,Message> {
    let items: Vec<db::FeedItem> = db::get_feed_items(feed_id);
    column(
        items.iter()
        .map(|i| {
            button(text(i.title.clone())).on_press(Message::LoadItem(i.item_id))
        })
        .map(Element::from),
    )
    .align_x(iced::Alignment::Start)
    .spacing(5)
    .padding(15)
}

pub fn content_area(cnt: String) -> iced::widget::Container<'static,Message>{
    let content = rss_content::parse_content(&cnt);
    container(
        column(
            content.into_iter().map(|c: Content|{
            match c {
                Content::Markdown(md) => {
                    text(md)
                },
                Content::Image(_) => {text("Image goes here")},
                Content::Audio(_) => {text("Audio widget here")},
                Content::Video(_) => {text("video player here")},
                _ => {text("")}
                }
        }).map(Element::from)
        )
    )

}

pub fn content_view(item: FeedItem) -> iced::widget::Scrollable<'static, Message> {

    scrollable(
        column!(
            text(item.title).size(34),
            match item.description {
                Some(d) => {
                    content_area(d)
                },
                None => {container(text("No description found"))}
            },
            match item.content {
                Some(c) => {
                    content_area(c)
                }
                None => {container(text("No content found"))}
            }  
            
        )
    )
}