use crate::DbPool;
use crate::api::ClientVersion;
use crate::providers::Provider;
use crate::status::*;
use crate::util::cache::VideoCache;
use crate::util::requester::Requester;
use crate::util::parse_abbreviated_number;
use crate::util::time::parse_time_to_seconds;
use crate::videos::{ServerOptions, VideoFormat, VideoItem};
use async_trait::async_trait;
use error_chain::error_chain;
use futures::future::join_all;
use htmlentity::entity::{ICodedDataTrait, decode};
use wreq::Version;
use std::{vec};
use titlecase::Titlecase;

error_chain! {
    foreign_links {
        Io(std::io::Error);
        HttpRequest(wreq::Error);
    }
}

#[derive(Debug, Clone)]
pub struct NoodlemagazineProvider {
    url: String,
}
impl NoodlemagazineProvider {
    pub fn new() -> Self {
        let provider = NoodlemagazineProvider {
            url: "https://noodlemagazine.com".to_string(),
        };
        provider
    }

    fn build_channel(&self, clientversion: ClientVersion) -> Channel {
        let _ = clientversion;
        Channel {
            id: "noodlemagazine".to_string(),
            name: "Noodlemagazine".to_string(),
            description: "The Best Search Engine of HD Videos".to_string(),
            premium: false,
            favicon: "https://www.google.com/s2/favicons?sz=64&domain=noodlemagazine.com"
                .to_string(),
            status: "active".to_string(),
            categories: vec![],
            options: vec![],
            nsfw: true,
            cacheDuration: Some(1800),
        }
    }

    async fn get(
        &self,
        cache: VideoCache,
        page: u8,
        sort: &str,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let _ = sort;
        let video_url = format!(
            "{}/popular/recent?sort_by=views&sort_order=desc&p={}",
            self.url,
            page - 1
        );
        let old_items = match cache.get(&video_url) {
            Some((time, items)) => {
                if time.elapsed().unwrap_or_default().as_secs() < 60 * 5 {
                    return Ok(items.clone());
                } else {
                    items.clone()
                }
            }
            None => {
                vec![]
            }
        };

        let mut requester = options.requester.clone().unwrap();
        let text = requester.get(&video_url, Some(Version::HTTP_2)).await.unwrap();
        let video_items: Vec<VideoItem> = self
            .get_video_items_from_html(text.clone(), requester.clone())
            .await;
        if !video_items.is_empty() {
            cache.remove(&video_url);
            cache.insert(video_url.clone(), video_items.clone());
        } else {
            return Ok(old_items);
        }
        Ok(video_items)
    }

    async fn query(
        &self,
        cache: VideoCache,
        page: u8,
        query: &str,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let search_string = query.trim().replace(" ", "%20").to_string();

        let video_url = format!("{}/video/{}?p={}", self.url, search_string, page - 1);
        // Check our Video Cache. If the result is younger than 1 hour, we return it.
        let old_items = match cache.get(&video_url) {
            Some((time, items)) => {
                if time.elapsed().unwrap_or_default().as_secs() < 60 * 5 {
                    return Ok(items.clone());
                } else {
                    let _ = cache.check().await;
                    return Ok(items.clone());
                }
            }
            None => {
                vec![]
            }
        };

        let mut requester = options.requester.clone().unwrap();

        let text = requester.get(&video_url, Some(Version::HTTP_2)).await.unwrap();
        let video_items: Vec<VideoItem> = self
            .get_video_items_from_html(text.clone(), requester.clone())
            .await;
        if !video_items.is_empty() {
            cache.remove(&video_url);
            cache.insert(video_url.clone(), video_items.clone());
        } else {
            return Ok(old_items);
        }
        Ok(video_items)
    }

    async fn get_video_items_from_html(
        &self,
        html: String,
        requester: Requester,
    ) -> Vec<VideoItem> {
        if html.is_empty() || html.contains("404 Not Found") {
            return vec![];
        }
        let raw_videos = html.split(">Show more</div>").collect::<Vec<&str>>()[0]
            .split("<div class=\"list_videos\" id=\"list_videos\">")
            .collect::<Vec<&str>>()[1]
            .split("<div class=\"item\">")
            .collect::<Vec<&str>>()[1..]
            .to_vec();

        let futures = raw_videos
            .into_iter()
            .map(|el| self.get_video_item(el.to_string(), requester.clone()));
        let results: Vec<Result<VideoItem>> = join_all(futures).await;
        let video_items: Vec<VideoItem> = results.into_iter().filter_map(Result::ok).collect();
        return video_items;
    }

    async fn get_video_item(
        &self,
        video_segment: String,
        requester: Requester,
    ) -> Result<VideoItem> {
        let video_url: String = format!(
            "{}{}",
            self.url,
            video_segment.split("<a href=\"").collect::<Vec<&str>>()[1]
                .split("\"")
                .collect::<Vec<&str>>()[0]
                .to_string()
        );
        let mut title = video_segment
            .split("<div class=\"title\">")
            .collect::<Vec<&str>>()[1]
            .split("<")
            .collect::<Vec<&str>>()[0]
            .trim()
            .to_string();
        // html decode
        title = decode(title.as_bytes())
            .to_string()
            .unwrap_or(title)
            .titlecase();
        let id = video_url.split("/").collect::<Vec<&str>>()[4]
            .split(".")
            .collect::<Vec<&str>>()[0]
            .to_string();

        let thumb = video_segment.split("<img ").collect::<Vec<&str>>()[1]
            .split("data-src=\"")
            .collect::<Vec<&str>>()[1]
            .split("\"")
            .collect::<Vec<&str>>()[0]
            .to_string();
        let raw_duration = video_segment
            .split("#clock-o\"></use></svg>")
            .collect::<Vec<&str>>()[1]
            .split("<")
            .collect::<Vec<&str>>()[0]
            .trim()
            .to_string();
        let duration = parse_time_to_seconds(raw_duration.as_str()).unwrap_or(0) as u32;
        let views = parse_abbreviated_number( video_segment
            .split("<use xlink:href=\"#eye\"></use></svg>")
            .collect::<Vec<&str>>()[1]
            .split("<")
            .collect::<Vec<&str>>()[0]
            .trim()).unwrap_or(0);

        let formats = match self.extract_media(&video_url, requester.clone()).await{
            Some(f) => f,
            None => return Err(Error::from("Failed to extract media formats")),
        };
        let video_item = VideoItem::new(
            id,
            title,
            video_url,
            "noodlemagazine".to_string(),
            thumb,
            duration,
        )
        .views(views)
        .formats(formats)
        ;
        return Ok(video_item);
    }

    async fn extract_media(&self, video_url: &String, mut requester: Requester) -> Option<Vec<VideoFormat>> {
        let text = requester.get(video_url, Some(Version::HTTP_2)).await.unwrap_or("".to_string());
        if text.is_empty() {
            return None;
        }
        let mut formats = vec![];
        let json_str = text.split("window.playlist = ")
                .collect::<Vec<&str>>()[1]
                .split(";")
                .collect::<Vec<&str>>()[0];
        let json: serde_json::Value = serde_json::from_str(json_str).unwrap();
        let sources = json["sources"].as_array().unwrap();
        for source in sources {
            let file = source["file"].as_str().unwrap().to_string();
            let label = source["label"].as_str().unwrap_or("unknown").to_string();
            let format = VideoFormat::new(
                file,
                label.clone(),
                "video/mp4".to_string(),
            )
            .format_id(label.clone())
            .format_note(label.clone())
            .http_header("Referer".to_string(), video_url.clone())
            ;
            formats.push(format);
        }
        return Some(formats.into_iter().rev().collect());
    }
}

#[async_trait]
impl Provider for NoodlemagazineProvider {
    async fn get_videos(
        &self,
        cache: VideoCache,
        pool: DbPool,
        sort: String,
        query: Option<String>,
        page: String,
        per_page: String,
        options: ServerOptions,
    ) -> Vec<VideoItem> {
        let _ = per_page;
        let _ = pool;
        let videos: std::result::Result<Vec<VideoItem>, Error> = match query {
            Some(q) => {
                self.query(cache, page.parse::<u8>().unwrap_or(1), &q, options)
                    .await
            }
            None => {
                self.get(cache, page.parse::<u8>().unwrap_or(1), &sort, options)
                    .await
            }
        };
        match videos {
            Ok(v) => v,
            Err(e) => {
                println!("Error fetching videos: {}", e);
                vec![]
            }
        }
    }
    fn get_channel(&self, clientversion: ClientVersion) -> crate::status::Channel {
        self.build_channel(clientversion)
    }
}
