use crate::DbPool;
use crate::providers::Provider;
use crate::util::cache::VideoCache;
use crate::util::requester::Requester;
use crate::util::time::parse_time_to_seconds;
use crate::videos::VideoItem;
use crate::videos::ServerOptions;
use error_chain::error_chain;
use htmlentity::entity::{ICodedDataTrait, decode};
use scraper::{Html, Selector};
use std::vec;
use async_trait::async_trait;

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

// fn has_blacklisted_class(element: &ElementRef, blacklist: &[&str]) -> bool {
//     element
//         .value()
//         .attr("class")
//         .map(|classes| classes.split_whitespace().any(|c| blacklist.contains(&c)))
//         .unwrap_or(false)
// }

#[derive(Debug, Clone)]
pub struct SxyprnProvider {
    url: String,
}
impl SxyprnProvider {
    pub fn new() -> Self {
        SxyprnProvider {
            url: "https://sxyprn.com".to_string(),
        }
    }
    async fn get(
        &self,
        cache: VideoCache,
        pool: DbPool,
        page: u8,
        sort: String,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let sort_string = match sort.as_str() {
            "views" => "views",
            "rating" => "rating",
            "orgasmic" => "orgasmic",
            _ => "latest",
        };
        // Extract needed fields from options at the start
        let filter = options.filter.clone().unwrap();
        let filter_string = match filter.as_str() {
            "other" => "other",
            "all" => "all",
            _ => "top",
        };
        let mut requester = options.requester.clone().unwrap();

        let url_str = format!(
            "{}/blog/all/{}.html?fl={}&sm={}",
            self.url,
            ((page as u32) - 1) * 20,
            filter_string,
            sort_string
        );

        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, None).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(), pool, 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,
        pool: DbPool,
        page: u8,
        query: &str,
        sort: String,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let sort_string = match sort.as_str() {
            "views" => "views",
            "rating" => "trending",
            "orgasmic" => "orgasmic",
            _ => "latest",
        };
        // Extract needed fields from options at the start
        let mut requester = options.requester.clone().unwrap();
        let search_string = query.replace(" ", "-");
        let url_str = format!(
            "{}/{}.html?page={}&sm={}",
            self.url, search_string, ((page as u32) - 1) * 20, sort_string
        );
        // 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, None).await.unwrap();
        let video_items: Vec<VideoItem> = self
            .get_video_items_from_html(text.clone(), pool, 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,
        pool: DbPool,
        requester: Requester,
    ) -> Vec<VideoItem> {
        let _ = requester;
        let _ = pool;
        if html.is_empty() {
            println!("HTML is empty");
            return vec![];
        }
        let raw_videos = html.split("<script async").collect::<Vec<&str>>()[0]
            .split("post_el_small'")
            .collect::<Vec<&str>>()[1..]
            .to_vec();
        let mut items: Vec<VideoItem> = Vec::new();
        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());
            // }
            // println!("\n\n\n");

            let url = video_segment.split("/post/").collect::<Vec<&str>>()[1]
                .split("'")
                .collect::<Vec<&str>>()[0]
                .to_string();

            let video_url = format!("https://hottub.spacemoehre.de/proxy/sxyprn/post/{}", url);

            let title_parts = video_segment.split("post_text").collect::<Vec<&str>>()[1]
                .split("style=''>")
                .collect::<Vec<&str>>()[1]
                .split("</div>")
                .collect::<Vec<&str>>()[0];
            let document = Html::parse_document(title_parts);
            let selector = Selector::parse("*").unwrap();

            let mut texts = Vec::new();
            for element in document.select(&selector) {
                let text = element.text().collect::<Vec<_>>().join(" ");
                if !text.trim().is_empty() {
                    texts.push(text.trim().to_string());
                }
            }
            let mut title = texts[0].clone();
            // html decode
            title = decode(title.as_bytes())
                .to_string()
                .unwrap_or(title)
                .replace("  ", " ");
            title = title.replace(" + ", " ").replace("  ", " ");
            if title.to_ascii_lowercase().starts_with("new "){
                title = title[4..].to_string();
            }
            // println!("Title: {}", title);
            let id = video_url.split("/").collect::<Vec<&str>>()[6].to_string();

            let thumb = format!(
                "https:{}",
                video_segment
                    .split("<img class='mini_post_vid_thumb lazyload'")
                    .collect::<Vec<&str>>()[1]
                    .split("data-src='")
                    .collect::<Vec<&str>>()[1]
                    .split("'")
                    .collect::<Vec<&str>>()[0]
                    .to_string()
            );

            let preview = match video_segment.contains("class='hvp_player'") {
                true => Some(format!(
                    "https:{}",
                    video_segment
                        .split("class='hvp_player'")
                        .collect::<Vec<&str>>()[1]
                        .split(" src='")
                        .collect::<Vec<&str>>()[1]
                        .split("'")
                        .collect::<Vec<&str>>()[0]
                        .to_string()
                )),
                false => None,
            };

            let views = video_segment
                .split("<strong>·</strong> ")
                .collect::<Vec<&str>>()[1]
                .split(" ")
                .collect::<Vec<&str>>()[0]
                .to_string();

            let raw_duration = video_segment.split("duration_small").collect::<Vec<&str>>()[1]
                .split("title='")
                .collect::<Vec<&str>>()[1]
                .split("'")
                .collect::<Vec<&str>>()[1]
                .split(">")
                .collect::<Vec<&str>>()[1]
                .split("<")
                .collect::<Vec<&str>>()[0]
                .to_string();
            let duration = parse_time_to_seconds(&raw_duration).unwrap_or(0) as u32;

            let stream_urls = video_segment
                .split("extlink_icon extlink")
                .collect::<Vec<&str>>()
                .iter()
                .map(|part| {
                    let url = part
                        .split("href='")
                        .collect::<Vec<&str>>()
                        .last()
                        .unwrap_or(&"")
                        .split("'")
                        .collect::<Vec<&str>>()[0]
                        .to_string();
                    url
                })
                .filter(|url| url.starts_with("http") && !url.starts_with("https://bigwarp.io/") && !url.starts_with("https://doodstream.com/"))
                .collect::<Vec<String>>();
            let video_item_url = match stream_urls.first() {
                Some(u) => u.clone(),
                None => video_url.clone(),
            };
            let mut video_item = VideoItem::new(
                id,
                title,
                video_item_url,
                "sxyprn".to_string(),
                thumb,
                duration,
            )
            .views(views.parse::<u32>().unwrap_or(0));
            if let Some(p) = preview {
                video_item = video_item.preview(p);
            }
            items.push(video_item);
        }
        return items;
    }
}

#[async_trait]
impl Provider for SxyprnProvider {
    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 videos: std::result::Result<Vec<VideoItem>, Error> = match query {
            Some(q) => {
                self.query(
                    cache,
                    pool,
                    page.parse::<u8>().unwrap_or(1),
                    &q,
                    sort,
                    options,
                )
                .await
            }
            None => {
                self.get(cache, pool, page.parse::<u8>().unwrap_or(1), sort, options)
                    .await
            }
        };
        match videos {
            Ok(v) => v,
            Err(e) => {
                println!("Error fetching videos: {}", e);
                vec![]
            }
        }
    }
}
