use crate::net;

use super::db;
use super::ui;
use iced::widget::markdown;
use iced::widget::row;
use iced::widget::scrollable;
use iced::widget::Column;
use iced::Theme;
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| {
                let n = net::url_network(&f.url);
                let prefix = match n {
                    net::Network::Clearnet => {""}
                    net::Network::Tor => {"Tor: "}
                    net::Network::I2P => {"I2P: "}
                    _ => {""}
                };
                row!(
                    button(text(prefix.to_owned() + &f.title)).on_press(Message::LoadFeed(f.feed_id)),
                    button(text("Remove feed")).on_press(Message::RemoveFeed(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(content: &'_ Vec<Content>) -> iced::widget::Container<'_, Message> {
    let mut children: Vec<Element<Message>> = Vec::new();
    for c in content {
                match c {
                    Content::MarkdownParsed(p) => {
                        children.push(
                            markdown(p, Theme::Dark).map(Message::LinkClicked)
                        );
                    }
                    Content::Image(_) => {children.push(text("<Image goes here>")
                        .size(32)
                        .into());},
                    Content::Audio(_) => {children.push(text("<Audio widget here>")
                        .size(32)
                        .into());},
                    Content::Video(_) => {children.push(text("<Video goes here>")
                        .size(32)
                        .into());},
                    _ => {}
                }
            }
    container(
        Column::with_children(children).spacing(5)
    )

}

pub fn media_view(state: &'_ ui::State) -> Element<'_, Message> {
    match state.current_item.clone().unwrap().media {
        Some(m) => {
            if m.len() > 0{
                text(format!("<Media is available:{}>",m)).into()
            }
            else {
                text("".to_owned()).into()

            }
        }
        None => {
            text("".to_owned()).into()
        }
    }
}

pub fn content_view(state: &'_ ui::State) -> iced::widget::Scrollable<'_, Message> {
    let item = state.current_item.clone().unwrap();

    scrollable(
        column!(
            match item.description {
                Some(_d) => {
                    content_area(&state.item_description)
                },
                None => {container(text("No description found"))}
            },
            match item.content {
                Some(_c) => {
                    content_area(&state.item_content)
                }
                None => {container(text("No content found"))}
            }  
            
        )
    ).width(iced::Fill)
}