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


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

#[derive(Debug, Clone)]
pub struct MissavProvider {
    url: String,
}
impl MissavProvider {
    pub fn new() -> Self {
        MissavProvider {
            url: "https://missav.ws".to_string()
        }
    }
    async fn get(&self, cache:VideoCache, pool:DbPool, page: u8, sort: String, options: ServerOptions) -> Result<Vec<VideoItem>> {

        // Extract needed fields from options at the start
        let language = options.language.clone().unwrap();
        let filter = options.filter.clone().unwrap();
        let mut requester = options.requester.clone().unwrap();

        let url_str = format!("{}/{}/{}?page={}&sort={}", self.url, language, filter, page, sort);
        
        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(), 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>> {
        // Extract needed fields from options at the start
        let language = options.language.clone().unwrap();
        let filter = options.filter.clone().unwrap();
        let mut requester = options.requester.clone().unwrap();
        let search_string = query.replace(" ", "%20");
        let url_str = format!(
            "{}/{}/search/{}?page={}&sort={}",
            self.url, language, search_string, page, sort
        );
        // 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(), 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> {
        if html.is_empty() {
            println!("HTML is empty");
            return vec![];
        }
        let raw_videos = html
            .split("@mouseenter=\"setPreview(\'")
            .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 = 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(), pool.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, pool: DbPool, mut requester: Requester) -> Result<VideoItem> {
        let mut conn = pool.get().expect("couldn't get db connection from pool");
        let db_result = db::get_video(&mut conn,url_str.clone());
        match db_result {
            Ok(Some(entry)) => {
                let video_item: VideoItem = serde_json::from_str(entry.as_str()).unwrap();
                return Ok(video_item)
            }
            Ok(None) => {
            }
            Err(e) => {
                println!("Error fetching video from database: {}", e);
            }
        }
        drop(conn);
        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);
        if url_str.contains("uncensored") {
            title = format!("[Uncensored] {}", title);
        }
        let thumb = vid.split("<meta property=\"og:image\" content=\"").collect::<Vec<&str>>()[1]
            .split("\"")
            .collect::<Vec<&str>>()[0]
            .to_string();

        let raw_duration = vid.split("<meta property=\"og:video:duration\" content=\"").collect::<Vec<&str>>()[1]
            .split("\"")
            .collect::<Vec<&str>>()[0]
            .to_string();
        let duration = raw_duration.parse::<u32>().unwrap_or(0);

        let id = url_str.split("/").collect::<Vec<&str>>().last().unwrap()
            .to_string();
        let mut tags = vec![];
        if vid.contains("<span>Actress:</span>"){
            for actress_snippet in vid.split("<span>Actress:</span>").collect::<Vec<&str>>()[1]
            .split("</div>").collect::<Vec<&str>>()[0].split("class=\"text-nord13 font-medium\">"){
                let tag = actress_snippet.split("<").collect::<Vec<&str>>()[0].trim()
                    .to_string();
                if !tag.is_empty(){
                    tags.push(format!("@actress:{}", tag));
                }
            }
        }
        if vid.contains("<span>Actor:</span>"){
            for actor_snippet in vid.split("<span>Actor:</span>").collect::<Vec<&str>>()[1]
            .split("</div>").collect::<Vec<&str>>()[0].split("class=\"text-nord13 font-medium\">"){
                let tag = actor_snippet.split("<").collect::<Vec<&str>>()[0].trim()
                    .to_string();
                if !tag.is_empty(){
                    tags.push(format!("@actor:{}", tag));
                }
            }
        }
        
        if vid.contains("<span>Maker:</span>"){
            for maker_snippet in vid.split("<span>Maker:</span>").collect::<Vec<&str>>()[1]
            .split("</div>").collect::<Vec<&str>>()[0]
            .split("class=\"text-nord13 font-medium\">"){
                let tag = maker_snippet.split("<").collect::<Vec<&str>>()[0].trim()
                    .to_string();
                if !tag.is_empty(){
                    tags.push(format!("@maker:{}", tag));
                }
            }
        }   

        if vid.contains("<span>Genre:</span>"){
            for tag_snippet in vid.split("<span>Genre:</span>").collect::<Vec<&str>>()[1]
            .split("</div>").collect::<Vec<&str>>()[0].split("class=\"text-nord13 font-medium\">"){
                let tag = tag_snippet.split("<").collect::<Vec<&str>>()[0].trim()
                    .to_string();
                if !tag.is_empty(){
                    tags.push(format!("@genre:{}", tag));
                }
            }
        }
        
        let preview = format!("https://fourhoi.com/{}/preview.mp4",id.clone());

        let mut video_url_parts = vid.split("m3u8").collect::<Vec<&str>>()[1]
            .split("https").collect::<Vec<&str>>()[0]
            .split("|").collect::<Vec<&str>>();
        video_url_parts.reverse();  
        let video_url = format!("https://{}.{}/{}-{}-{}-{}-{}/playlist.m3u8", 
                                        video_url_parts[1],
                                        video_url_parts[2],
                                        video_url_parts[3],
                                        video_url_parts[4],
                                        video_url_parts[5],
                                        video_url_parts[6],
                                        video_url_parts[7]
                                    );
        let video_item = VideoItem::new(
            id,
            title,
            video_url.clone(),
            "missav".to_string(),
            thumb,
            duration,
        )
        .tags(tags)
        .preview(preview)
        ;

        let mut conn = pool.get().expect("couldn't get db connection from pool");
        let insert_result = db::insert_video(&mut conn, &url_str, &serde_json::to_string(&video_item)?);
        match insert_result{
            Ok(_) => (),
            Err(e) => {println!("{:?}", e); }
        }
        drop(conn);

        return Ok(video_item);
    }
}

impl Provider for MissavProvider {
    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![]
            }
        }
    }
}
