use crate::DbPool;
use crate::providers::Provider;
use crate::util::cache::VideoCache;
use crate::util::requester::Requester;
use crate::videos::VideoItem;
use crate::videos::{self, ServerOptions};
use error_chain::error_chain;
use futures::future::join_all;
use htmlentity::entity::{ICodedDataTrait, decode};
use std::vec;
use async_trait::async_trait;

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

#[derive(Debug, Clone)]
pub struct ParadisehillProvider {
    url: String,
}
impl ParadisehillProvider {
    pub fn new() -> Self {
        ParadisehillProvider {
            url: "https://en.paradisehill.cc".to_string(),
        }
    }
    async fn get(
        &self,
        cache: VideoCache,
        page: u8,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let mut requester = options.requester.clone().unwrap();

        let url_str = format!("{}/all/?sort=created_at&page={}", self.url, page);

        let old_items = match cache.get(&url_str) {
            Some((time, items)) => {
                if time.elapsed().unwrap_or_default().as_secs() < 60 * 60 {
                    return Ok(items.clone());
                } else {
                    items.clone()
                }
            }
            None => {
                vec![]
            }
        };

        let text = requester.get(&url_str).await.unwrap();
        // Pass a reference to options if needed, or reconstruct as needed
        let video_items: Vec<VideoItem> = self
            .get_video_items_from_html(text.clone(), requester)
            .await;
        if !video_items.is_empty() {
            cache.remove(&url_str);
            cache.insert(url_str.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>> {
        // Extract needed fields from options at the start
        let mut requester = options.requester.clone().unwrap();
        let search_string = query.replace(" ", "+");
        let url_str = format!(
            "{}/search/?pattern={}&page={}",
            self.url, search_string, page
        );
        // Check our Video Cache. If the result is younger than 1 hour, we return it.
        let old_items = match cache.get(&url_str) {
            Some((time, items)) => {
                if time.elapsed().unwrap_or_default().as_secs() < 60 * 60 {
                    return Ok(items.clone());
                } else {
                    let _ = cache.check().await;
                    return Ok(items.clone());
                }
            }
            None => {
                vec![]
            }
        };
        let text = requester.get(&url_str).await.unwrap();
        let video_items: Vec<VideoItem> = self
            .get_video_items_from_html(text.clone(), requester)
            .await;
        if !video_items.is_empty() {
            cache.remove(&url_str);
            cache.insert(url_str.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() {
            println!("HTML is empty");
            return vec![];
        }
        let raw_videos = html.split("item list-film-item").collect::<Vec<&str>>()[1..].to_vec();
        let mut urls: Vec<String> = vec![];
        for video_segment in &raw_videos {
            // let vid = video_segment.split("\n").collect::<Vec<&str>>();
            // for (index, line) in vid.iter().enumerate() {
            //     println!("Line {}: {}", index, line.to_string().trim());
            // }

            let url_str = format!(
                "{}{}",
                self.url,
                video_segment.split("<a href=\"").collect::<Vec<&str>>()[1]
                    .split("\"")
                    .collect::<Vec<&str>>()[0]
                    .to_string()
            );
            urls.push(url_str.clone());
        }
        let futures = urls
            .into_iter()
            .map(|el| self.get_video_item(el.clone(), 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, url_str: String, mut requester: Requester) -> Result<VideoItem> {
        let vid = requester.get(&url_str).await.unwrap();
        let mut title = vid
            .split("<meta property=\"og:title\" content=\"")
            .collect::<Vec<&str>>()[1]
            .split("\"")
            .collect::<Vec<&str>>()[0]
            .trim()
            .to_string();
        title = decode(title.as_bytes()).to_string().unwrap_or(title);
        let thumb = format!(
            "{}{}",
            self.url,
            vid.split("<meta property=\"og:image\" content=\"")
                .collect::<Vec<&str>>()[1]
                .split("\"")
                .collect::<Vec<&str>>()[0]
                .to_string()
        );

        let video_urls = vid.split("var videoList = ").collect::<Vec<&str>>()[1]
            .split("\"src\":\"")
            .collect::<Vec<&str>>()[1..].to_vec();
        let mut formats = vec![];
        for url in video_urls {
            let video_url = url
                .split("\"")
                .collect::<Vec<&str>>()[0]
                .replace("\\", "")
                .to_string();
            let format =
                videos::VideoFormat::new(video_url.clone(), "1080".to_string(), "mp4".to_string())
                    // .protocol("https".to_string())
                    .format_id(video_url.split("/").last().unwrap().to_string())
                    .format_note(format!("{}", video_url.split("_").last().unwrap().replace(".mp4", "").to_string()))
                    ;
                formats.push(format);
        }

        formats.reverse();
        let id = url_str
            .split("/")
            .collect::<Vec<&str>>()[3]
            .split("_")
            .collect::<Vec<&str>>()[0]
            .to_string();

        let video_item = VideoItem::new(
            id,
            title,
            url_str.clone(),
            "paradisehill".to_string(),
            thumb,
            0,
        )
        .aspect_ratio(0.697674419 as f32)
        .formats(formats);

        return Ok(video_item);
    }
}

#[async_trait]
impl Provider for ParadisehillProvider {
    async fn get_videos(
        &self,
        cache: VideoCache,
        pool: DbPool,
        sort: String,
        query: Option<String>,
        page: String,
        per_page: String,
        options: ServerOptions,
    ) -> Vec<VideoItem> {
        let _ = pool;
        let _ = sort;
        let _ = per_page;
        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), options)
                    .await
            }
        };
        match videos {
            Ok(v) => v,
            Err(e) => {
                println!("Error fetching videos: {}", e);
                vec![]
            }
        }
    }
}
