use std::vec;
use std::env;
use error_chain::error_chain;
use futures::future::join_all;
use htmlentity::entity::{decode, ICodedDataTrait};
use ntex::channel::pool;
use reqwest::Client;
use reqwest::{Proxy};
use crate::db;
use crate::providers::Provider;
use crate::util::cache::VideoCache;
use crate::util::flaresolverr::{FlareSolverrRequest, Flaresolverr};
use crate::videos::{self, VideoItem};
use crate::DbPool;
use crate::USER_AGENT; // Make sure Provider trait is imported
use std::collections::HashMap;
use std::time::Duration;
use tokio::time::sleep;

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


#[derive(Debug, Clone)]
pub struct SpankbangProvider {
    url: String,
}
impl SpankbangProvider {
    pub fn new() -> Self {
        SpankbangProvider {
            url: "https://spankbang.com/".to_string(),
        }
    }
    async fn get(&self, cache:VideoCache, pool: DbPool, page: u8, sort: String) -> Result<Vec<VideoItem>> {
        
        let mut url = format!("{}{}/{}/", self.url, sort, page);
        
        let old_items = match cache.get(&url) {
            Some((time, items)) => {
                if time.elapsed().unwrap_or_default().as_secs() < 60 * 60 {
                    println!("Cache hit for URL: {}", url);
                    return Ok(items.clone());
                }
                else{
                    items.clone()
                }
            }
            None => {
                vec![]
            }
        };

        
        let client = match env::var("BURP_URL").as_deref() {
            Ok(burp_url) => 
                reqwest::Client::builder()
                .user_agent(USER_AGENT)
                .proxy(Proxy::https(burp_url).unwrap())
                .danger_accept_invalid_certs(true)
                .build()?,
            Err(_) => reqwest::Client::builder()
                .user_agent(USER_AGENT)
                .danger_accept_invalid_certs(true)
                .build()?,
        };
        
        let response = client.get(url.clone()).send().await?;
        let mut cookies_string = String::new();
        if let Some(cookies) = response.headers().get_all("set-cookie").iter().next() {
            for value in response.headers().get_all("set-cookie").iter() {
                let mut cookies_map = HashMap::new();
                for value in response.headers().get_all("set-cookie").iter() {
                    if let Ok(cookie_str) = value.to_str() {
                        if let Some((k, v)) = cookie_str.split_once('=') {
                            let key = k.trim();
                            let val = v.split(';').next().unwrap_or("").trim();
                            cookies_map.insert(key.to_string(), val.to_string());
                        }
                    }
                }
                cookies_string = cookies_map
                    .iter()
                    .map(|(k, v)| format!("{}={}", k, v))
                    .collect::<Vec<String>>()
                    .join("; ");
            }
        }
        if response.status().is_success() {
            let text = response.text().await?;
            println!("Response text: {}", text);
            let video_items: Vec<VideoItem> = self.get_video_items_from_html(text.clone(), &client, cookies_string, pool.clone()).await;


            if !video_items.is_empty() {
                cache.remove(&url);
                cache.insert(url.clone(), video_items.clone());
            } else{
                return Ok(old_items);
            }
            Ok(video_items)
        } else {
            let flare_url = env::var("FLARE_URL").expect("FLARE_URL not set");
            let flare = Flaresolverr::new(flare_url);
            let result = flare
                .solve(FlareSolverrRequest {
                    cmd: "request.get".to_string(),
                    url: url.clone(),
                    maxTimeout: 60000,
                })
                .await;
            let video_items = match result {
                Ok(res) => {
                    // println!("FlareSolverr response: {}", res);
                    self.get_video_items_from_html(res.solution.response,  &client,String::new(), pool.clone()).await
                }
                Err(e) => {
                    println!("Error solving FlareSolverr: {}", e);
                    return Err("Failed to solve FlareSolverr".into());
                }
            };
            if !video_items.is_empty() {
                cache.remove(&url);
                cache.insert(url.clone(), video_items.clone());
            } else {
                return Ok(old_items);
            }
            Ok(video_items)
        }
    }

    async fn query(&self, cache: VideoCache, pool: DbPool, page: u8, query: &str) -> Result<Vec<VideoItem>> {
        let url = format!("{}s/{}/{}/", self.url, query.replace(" ", "+"), page);
        
        let old_items = match cache.get(&url) {
            Some((time, items)) => {
                if time.elapsed().unwrap_or_default().as_secs() < 60 * 60 {
                    println!("Cache hit for URL: {}", url);
                    return Ok(items.clone());
                }
                else{
                    items.clone()
                }
            }
            None => {
                vec![]
            }
        };

        
        let client = match env::var("BURP_URL").as_deref() {
            Ok(burp_url) => 
                reqwest::Client::builder()
                .user_agent(USER_AGENT)
                .proxy(Proxy::https(burp_url).unwrap())
                .danger_accept_invalid_certs(true)
                .build()?,
            Err(_) => reqwest::Client::builder()
                .user_agent(USER_AGENT)
                .danger_accept_invalid_certs(true)
                .build()?,
        };
        
        let response = client.get(url.clone()).send().await?;
        let mut cookies_string = String::new();
        if let Some(cookies) = response.headers().get_all("set-cookie").iter().next() {
            for value in response.headers().get_all("set-cookie").iter() {
                let mut cookies_map = HashMap::new();
                for value in response.headers().get_all("set-cookie").iter() {
                    if let Ok(cookie_str) = value.to_str() {
                        if let Some((k, v)) = cookie_str.split_once('=') {
                            let key = k.trim();
                            let val = v.split(';').next().unwrap_or("").trim();
                            cookies_map.insert(key.to_string(), val.to_string());
                        }
                    }
                }
                cookies_string = cookies_map
                    .iter()
                    .map(|(k, v)| format!("{}={}", k, v))
                    .collect::<Vec<String>>()
                    .join("; ");
            }
        }
        if response.status().is_success() {
            let text = response.text().await?;
            let video_items: Vec<VideoItem> = self.get_video_items_from_html(text.clone(), &client, cookies_string, pool.clone()).await;
            if !video_items.is_empty() {
                cache.remove(&url);
                cache.insert(url.clone(), video_items.clone());
            } else{
                return Ok(old_items);
            }
            Ok(video_items)
        } else {
            let flare_url = env::var("FLARE_URL").expect("FLARE_URL not set");
            let flare = Flaresolverr::new(flare_url);
            let result = flare
                .solve(FlareSolverrRequest {
                    cmd: "request.get".to_string(),
                    url: url.clone(),
                    maxTimeout: 60000,
                })
                .await;
            let video_items = match result {
                Ok(res) => {
                    // println!("FlareSolverr response: {}", res);
                    self.get_video_items_from_html(res.solution.response, &client, String::new(), pool.clone()).await
                }
                Err(e) => {
                    println!("Error solving FlareSolverr: {}", e);
                    return Err("Failed to solve FlareSolverr".into());
                }
            };
            if !video_items.is_empty() {
                cache.remove(&url);
                cache.insert(url.clone(), video_items.clone());
            } else {
                return Ok(old_items);
            }
            Ok(video_items)
        }
    }

    async fn get_video_url(&self, url:String, client:&Client, cookies: String, pool: DbPool) -> Result<String> {
        
        let mut conn = pool.get().expect("couldn't get db connection from pool");
        let db_result = db::get_video(&mut conn,url.clone());
        drop(conn);
        match db_result {
            Ok(Some(video_url)) => {
                return Ok(video_url);
            }
            Ok(None) => (),
            Err(e) => {
                println!("Error fetching video from database: {}", e);
                // return Err(format!("Error fetching video from database: {}", e).into());
            }
        }
        let response = client.get(url.clone()).header("Cookie", cookies.clone()).send().await?;

        let mut response = response;
        while response.status().as_u16() == 429 {
            println!("Received 429 Too Many Requests. Waiting 10 seconds before retrying...");
            sleep(Duration::from_secs(60)).await;
            response = client.get(url.clone()).header("Cookie", cookies.clone()).send().await?;
        }

        if response.status().is_success() {
            let text = response.text().await?;
            let lines = text.split("\n").collect::<Vec<&str>>();
            let url_line = lines.iter()
            .find(|s| s.trim_start().starts_with("<source src=") && s.contains("type=\"video/mp4\""))
            .unwrap_or(&"");
            let new_url = url_line.split("src=\"").collect::<Vec<&str>>()[1].split("\"").collect::<Vec<&str>>()[0].to_string();
            let mut conn = pool.get().expect("couldn't get db connection from pool");
            let _ = db::insert_video(&mut conn, &url, &new_url);
            drop(conn);
            return Ok(new_url)
        }
        Err(Error::from("Failed to get video URL"))
    }

    async fn parse_video_item(
        &self,
        html: String,
        client: &Client,
        cookies: String,
        pool: DbPool
    ) -> Result<VideoItem> {
        let vid = html.split("\n").collect::<Vec<&str>>();
        if vid.len() > 200 {
            return Err("Video item has too many lines".into());
        }
        let title_line = vid.iter()
            .find(|s| s.trim_start().starts_with("<a href=") && s.contains("title="))
            .unwrap_or(&"");
        let mut title = title_line.split("title=\"").collect::<Vec<&str>>()[1].split("\"").collect::<Vec<&str>>()[0].to_string();
        title = decode(title.as_bytes()).to_string().unwrap_or(title);
        let thumb = vid[13].split("data-src=\"").collect::<Vec<&str>>()[1].split("\"").collect::<Vec<&str>>()[0].to_string();
        let mut preview = "".to_string();
        if vid[15].contains("data-preview=\""){
            preview = vid[15].split("data-preview=\"").collect::<Vec<&str>>()[1].split("\"").collect::<Vec<&str>>()[0].to_string();
        }
        let duration_str = vid.iter().find(|s| s.contains("<span class=\"video-badge l\">")).unwrap().split("<span class=\"video-badge l\">").collect::<Vec<&str>>()[1].split("m<").collect::<Vec<&str>>()[0];
        let duration: u32 = duration_str.parse::<u32>().unwrap_or(0) * 60;
        let view_and_rating_str: Vec<&str> = vid.iter().copied().filter(|s| s.contains("<span class=\"md:text-body-md\">")).collect();
        let views_str = view_and_rating_str[0].split(">").collect::<Vec<&str>>()[1].split("K<").collect::<Vec<&str>>()[0];
        let views = (views_str.parse::<f32>().unwrap_or(0.0) * 1000.0) as u32;
        let rate_str = view_and_rating_str[1].split(">").collect::<Vec<&str>>()[1].split("%<").collect::<Vec<&str>>()[0];
        let rating = rate_str.parse::<f32>().unwrap_or(0.0);
        let url_part = vid.iter().find(|s| s.contains("<a href=\"/")).unwrap().split("<a href=\"/").collect::<Vec<&str>>()[1].split("\"").collect::<Vec<&str>>()[0];
        let url = match self.get_video_url(self.url.clone() + url_part, client, cookies, pool).await {
            Ok(video_url) => video_url,
            Err(e) => {
                return Err("Failed to get video URL".into());
            }
        };
        let id = url_part.split("/").collect::<Vec<&str>>()[0].to_string();

        let quality_str = match vid[25].contains("<"){
            true => vid[25].split(">").collect::<Vec<&str>>()[1].split("<").collect::<Vec<&str>>()[0],
            false => "SD",
        };
        let quality = match quality_str{
            "HD" => "1080",
            "4k" => "2160",
            "SD" => "720",
            _ => "1080",
        };

        let video_item = VideoItem::new(id, title, url.clone().to_string(), "spankbang".to_string(), thumb, duration)
        .views(views)
        .rating(rating)
        // .formats(vec![format])
        .preview(preview);
        Ok(video_item)
    }

    async fn get_video_items_from_html(&self, html: String, client: &Client, cookies:String, pool: DbPool) -> Vec<VideoItem> {
        if html.is_empty() {
            println!("HTML is empty");
            return vec![];
        }
        let mut items: Vec<VideoItem> = Vec::new();
        let mut split_html = html.split("\"video-list").collect::<Vec<&str>>();
        if split_html.len() < 2 {
            println!("Could not find video-list in HTML");
            return items;
        }
        let video_listing_content = split_html[1];
        let raw_videos_vec = video_listing_content
            .split("data-testid=\"video-item\"")
            .collect::<Vec<&str>>();
        if raw_videos_vec.len() < 2 {
            println!("Could not find video-item in HTML");
            return items;
        }
        let raw_videos = raw_videos_vec[1..].to_vec();
        let futures = raw_videos.into_iter().map(|el| self.parse_video_item(el.to_string(), client, cookies.clone(), pool.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;
    }

}

impl Provider for SpankbangProvider {
    async fn get_videos(
        &self,
        cache: VideoCache,
        pool: DbPool,
        _channel: String,
        mut sort: String,
        query: Option<String>,
        page: String,
        per_page: String,
        featured: String,
    ) -> Vec<VideoItem> {
        let _ = per_page;
        let _ = featured;
        let _ = pool;

        if sort == "date"{
            sort = "trending_videos".to_string();
        }
        let videos: std::result::Result<Vec<VideoItem>, Error> = match query {
            Some(q) => self.query(cache, pool, page.parse::<u8>().unwrap_or(1), &q).await,
            None => self.get(cache, pool, page.parse::<u8>().unwrap_or(1), sort).await,
        };
        match videos {
            Ok(v) => v,
            Err(e) => {
                println!("Error fetching videos: {}", e);
                vec![]
            }
        }
    }
}
