use crate::db::FeedItem;
use crate::widgets::content_view;
use crate::widgets::media_view;

use super::db;
use super::widgets;
use iced::widget::markdown::Url;
use iced::widget::row;
use iced::widget::scrollable;
use iced::widget::text_input;
use iced::Task;
use iced::{
    widget::{button, column, container, text},
    Element,
    Length::Fill,
};
use rss_content::parse_content;
use rss_content::Content;

pub fn user_interface() -> iced::Result {
    iced::run(update, view)
}

#[derive(Clone, Debug)]
pub enum Page {
    Home,
    FeedView,
    AllItems,
    ItemView,
    Testing,
}

pub struct State {
    page: Page,
    current_feed: usize,
    pub current_item: Option<FeedItem>,
    pub item_description: Vec<Content>,
    pub item_content: Vec<Content>,
    feed_input: String,
}
impl Default for State {
    fn default() -> Self {
        State {
            page: Page::Home,
            current_feed: 0,
            current_item: None,
            item_description: Vec::new(),
            item_content: Vec::new(),
            feed_input: String::from(""),
        }
    }
}

#[derive(Debug, Clone)]
pub enum Message {
    ChangePage(Page),
    LoadFeed(usize),
    AddFeed(String),
    RemoveFeed(usize),
    LoadItem(usize),
    FieldUpdated(AppField, String),
    LinkClicked(Url),
    Done(String),
    ResetDB,
}

#[derive(Debug, Clone)]
pub enum AppField {
    FeedInput,
}

async fn add_feed_background(url: String) -> String {
    println!("Adding feed!");
    db::add_feed(&url);
    "Done adding feed".to_string()
}
async fn remove_feed_background(id:usize) -> String {
    println!("Removing feed");
    db::remove_feed(id);
    "Done removing feed".to_owned()
}

fn update(state: &mut State, mes: Message) -> Task<Message> {
    match mes {
        Message::ChangePage(p) => {
            state.page = p;
            Task::none()
        }
        Message::LoadFeed(feed_id) => {
            state.current_feed = feed_id;
            state.page = Page::FeedView;
            Task::none()
        }
        
        Message::AddFeed(f) => {
            state.feed_input = "".to_string();
            Task::perform(add_feed_background(f.to_string()), Message::Done)
        }
        Message::RemoveFeed(id) => {
            Task::perform(remove_feed_background(id), Message::Done)
        }
        Message::LinkClicked(l) => {
            println!("Link clicked: {}", l);
            
            Task::none()
        }
        Message::LoadItem(id) => {
            let item = db::get_item(id);
            state.item_description = match &item.description {
                Some(d) => {
                    parse_content(&d)
                }
                None => Vec::new()
            };
            state.item_content = match &item.content{
                Some(c) => {
                    parse_content(&c)
                }
                None => Vec::new()
            };
            state.current_item = Some(item);
            state.page = Page::ItemView;
            Task::none()
        }
        Message::Done(_) => Task::none(),
        Message::FieldUpdated(field, value) => {
            match field {
                AppField::FeedInput => {
                    state.feed_input = value;
                }
            }
            Task::none()
        }
        Message::ResetDB => {
            db::reset();
            Task::none()
        }
    }
}

fn view(state: &State) -> Element<'_, Message> {
    match state.page {
        Page::Home => home(&state),
        Page::FeedView => feed_layout(&state),
        Page::AllItems => item_list(&state),
        Page::ItemView => item_view(&state),
        Page::Testing => testing(&state),
    }
}

fn home(_state: &State) -> Element<'_, Message> {
    container(column!(
        scrollable(widgets::list_feeds())
            .width(iced::Fill)
            .height(iced::Fill),
        button("Go to test!").on_press(Message::ChangePage(Page::Testing))
    ))
    .padding(15)
    .height(Fill)
    .width(Fill)
    .into()
}

fn feed_layout(state: &State) -> Element<'_, Message> {
    container(column!(
        button(text("Home")).on_press(Message::ChangePage(Page::Home)),
        scrollable(widgets::list_items(state.current_feed))
            .width(iced::Fill)
            .height(iced::Fill),
    ))
    .height(Fill)
    .width(Fill)
    .into()
}

fn item_view(state: &State) -> Element<'_, Message> {
    let title = match state.current_item.clone() {
        Some(i) => {i.title}
        None => {"".to_owned()}
    };
    container(column!(
        row!(
            button(text("Home")).on_press(Message::ChangePage(Page::Home)),
            button(text("Feed")).on_press(Message::ChangePage(Page::FeedView))
        )
        .spacing(10),
        text(title).size(34),
        media_view(state),
        content_view(state),
    ))
    .height(Fill)
    .width(Fill)
    .into()
}

fn item_list(_: &State) -> Element<'_, Message> {
    todo!()
}
fn testing(state: &State) -> Element<'_, Message> {
    column!(
        text("Dev Panel"),
        button("Add gabe.rocks").on_press(Message::AddFeed(String::from("https://gabe.rocks/rss"))),
        button("Add LSN").on_press(Message::AddFeed(String::from(
            "https://libresolutions.network/rss"
        ))),
        row!(
            text_input("Add a feed", &state.feed_input)
                .on_input(|val| Message::FieldUpdated(AppField::FeedInput, val))
                .width(300),
            button("Add feed!").on_press(Message::AddFeed(state.feed_input.clone()))
        )
        .spacing(5)
        .padding(10),
        button("Wipe DB").on_press(Message::ResetDB),
        button("go back!").on_press(Message::ChangePage(Page::Home))
    )
    .spacing(5)
    .into()
}
