use crate::DbPool;
use crate::api::ClientVersion;
use crate::providers::{Provider, report_provider_error};
use crate::status::*;
use crate::util::cache::VideoCache;
use crate::util::flaresolverr::{FlareSolverrRequest, Flaresolverr};
use crate::util::parse_abbreviated_number;
use crate::util::time::parse_time_to_seconds;
use crate::videos::{ServerOptions, VideoItem};
use async_trait::async_trait;
use error_chain::error_chain;
use htmlentity::entity::{ICodedDataTrait, decode};
use std::collections::HashMap;
use std::env;
use std::sync::{Arc, RwLock};
use std::vec;
use wreq::Client;
use wreq_util::Emulation;

pub const CHANNEL_METADATA: crate::providers::ProviderChannelMetadata =
    crate::providers::ProviderChannelMetadata {
        group_id: "mainstream-tube",
        tags: &["tube", "mixed", "search"],
    };

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

#[derive(Debug, Clone)]
pub struct OkxxxProvider {
    url: String,
    tag_map: Arc<RwLock<HashMap<String, String>>>,
}
impl OkxxxProvider {
    pub fn new() -> Self {
        OkxxxProvider {
            url: "https://ok.xxx".to_string(),
            tag_map: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    fn normalize_key(value: &str) -> String {
        value
            .trim()
            .to_ascii_lowercase()
            .replace(['_', '-'], " ")
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn humanize_slug(value: &str) -> String {
        value
            .trim_matches('/')
            .replace('-', " ")
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn insert_tag_mapping(&self, kind: &str, slug: &str, title: Option<&str>) {
        let slug = slug.trim().trim_matches('/');
        if slug.is_empty() {
            return;
        }
        let path = format!("{kind}/{slug}");
        if let Ok(mut map) = self.tag_map.write() {
            map.insert(Self::normalize_key(slug), path.clone());
            let normalized_title = Self::normalize_key(title.unwrap_or(slug));
            if !normalized_title.is_empty() {
                map.insert(normalized_title, path);
            }
        }
    }

    fn resolve_query_path(&self, query: &str) -> Option<String> {
        let trimmed = query.trim().trim_start_matches('@');
        if let Some((kind, raw_value)) = trimmed.split_once(':') {
            let kind = kind.trim().to_ascii_lowercase();
            let value = raw_value.trim().trim_matches('/').replace(' ', "-");
            if !value.is_empty() && matches!(kind.as_str(), "sites" | "models") {
                return Some(format!("{kind}/{value}"));
            }
        }
        let normalized = Self::normalize_key(trimmed);
        if normalized.is_empty() {
            return None;
        }
        self.tag_map.read().ok()?.get(&normalized).cloned()
    }

    fn build_channel(&self, _clientversion: ClientVersion) -> Channel {
        Channel {
            id: "okxxx".to_string(),
            name: "Ok.xxx".to_string(),
            description: "free porn tube!".to_string(),
            premium: false,
            favicon: "https://www.google.com/s2/favicons?sz=64&domain=ok.xxx".to_string(),
            status: "active".to_string(),
            categories: vec![],
            options: vec![ChannelOption {
                id: "sort".to_string(),
                title: "Sort".to_string(),
                description: "Sort the Videos".to_string(),
                systemImage: "list.number".to_string(),
                colorName: "blue".to_string(),
                options: vec![
                    FilterOption {
                        id: "new".to_string(),
                        title: "New".to_string(),
                    },
                    FilterOption {
                        id: "popular".to_string(),
                        title: "Popular".to_string(),
                    },
                    FilterOption {
                        id: "trending".to_string(),
                        title: "Trending".to_string(),
                    },
                ],
                multiSelect: false,
            }],
            nsfw: true,
            cacheDuration: Some(1800),
            ytdlpCommand: Some("yt-dlp --impersonate chrome-120".to_string())
        }
    }
    async fn get(&self, cache: VideoCache, page: u8, sort: &str) -> Result<Vec<VideoItem>> {
        let sort_string = match sort {
            "trending" => "/trending",
            "popular" => "/popular",
            _ => "",
        };
        let video_url = format!("{}{}/{}/", self.url, sort_string, page);
        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 proxy = Proxy::all("http://192.168.0.103:8081").unwrap();
        let client = Client::builder()
            .cert_verification(false)
            .emulation(Emulation::Firefox136)
            .build()?;

        let mut response = client
            .get(video_url.clone())
            // .proxy(proxy.clone())
            .send()
            .await?;
        if response.status().is_redirection() {
            let location = match response
                .headers()
                .get("Location")
                .and_then(|h| h.to_str().ok())
            {
                Some(location) => location,
                None => {
                    report_provider_error(
                        "okxxx",
                        "get.redirect_location",
                        &format!("url={video_url}; missing/invalid Location header"),
                    )
                    .await;
                    return Ok(old_items);
                }
            };
            println!("Redirection detected, following to: {}", location);
            response = client
                .get(location)
                // .proxy(proxy.clone())
                .send()
                .await?;
        }
        if response.status().is_success() {
            let text = response.text().await?;
            let video_items: Vec<VideoItem> = self.get_video_items_from_html(text.clone());
            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)
        } else {
            let flare_url = match env::var("FLARE_URL") {
                Ok(url) => url,
                Err(e) => {
                    report_provider_error("okxxx", "get.flare_url", &e.to_string()).await;
                    return Ok(old_items);
                }
            };
            let flare = Flaresolverr::new(flare_url);
            let result = flare
                .solve(FlareSolverrRequest {
                    cmd: "request.get".to_string(),
                    url: video_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)
                }
                Err(e) => {
                    println!("Error solving FlareSolverr: {}", e);
                    return Err("Failed to solve FlareSolverr".into());
                }
            };
            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) -> Result<Vec<VideoItem>> {
        let search_string = query.to_lowercase().trim().replace(" ", "-");
        let mut video_url = format!("{}/search/{}/{}/", self.url, search_string, page);
        if let Some(path) = self.resolve_query_path(query) {
            video_url = format!("{}/{}/{}/", self.url, path, page);
        }
        // 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 proxy = Proxy::all("http://192.168.0.103:8081").unwrap();
        let client = Client::builder()
            .cert_verification(false)
            .emulation(Emulation::Firefox136)
            .build()?;

        let mut response = client
            .get(video_url.clone())
            // .proxy(proxy.clone())
            .send()
            .await?;

        if response.status().is_redirection() {
            let location = match response
                .headers()
                .get("Location")
                .and_then(|h| h.to_str().ok())
            {
                Some(location) => location,
                None => {
                    report_provider_error(
                        "okxxx",
                        "query.redirect_location",
                        &format!("url={video_url}; missing/invalid Location header"),
                    )
                    .await;
                    return Ok(old_items);
                }
            };
            response = client
                .get(self.url.clone() + location)
                // .proxy(proxy.clone())
                .send()
                .await?;
        }

        if response.status().is_success() {
            let text = response.text().await?;
            let video_items: Vec<VideoItem> = self.get_video_items_from_html(text.clone());
            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)
        } else {
            let flare_url = match env::var("FLARE_URL") {
                Ok(url) => url,
                Err(e) => {
                    report_provider_error("okxxx", "query.flare_url", &e.to_string()).await;
                    return Ok(old_items);
                }
            };
            let flare = Flaresolverr::new(flare_url);
            let result = flare
                .solve(FlareSolverrRequest {
                    cmd: "request.get".to_string(),
                    url: video_url.clone(),
                    maxTimeout: 60000,
                })
                .await;
            let video_items = match result {
                Ok(res) => self.get_video_items_from_html(res.solution.response),
                Err(e) => {
                    println!("Error solving FlareSolverr: {}", e);
                    return Err("Failed to solve FlareSolverr".into());
                }
            };
            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)
        }
    }

    fn get_video_items_from_html(&self, html: String) -> Vec<VideoItem> {
        if html.is_empty() {
            println!("HTML is empty");
            return vec![];
        }
        let mut items: Vec<VideoItem> = Vec::new();
        let raw_videos = html
            .split("<div class=\"pagination\"")
            .collect::<Vec<&str>>()
            .get(0)
            .copied()
            .unwrap_or_default()
            .split("item thumb-bl thumb-bl-video video_")
            .collect::<Vec<&str>>()[1..]
            .to_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);
            // }
            let video_url: String = format!(
                "{}{}",
                self.url,
                video_segment
                    .split("<a href=\"")
                    .collect::<Vec<&str>>()
                    .get(1)
                    .copied()
                    .unwrap_or_default()
                    .split("\"")
                    .collect::<Vec<&str>>()
                    .get(0)
                    .copied()
                    .unwrap_or_default()
            );
            let preview_url = video_segment
                .split("data-preview-custom=\"")
                .collect::<Vec<&str>>()
                .get(1)
                .copied()
                .unwrap_or_default()
                .split("\"")
                .collect::<Vec<&str>>()
                .get(0)
                .copied()
                .unwrap_or_default()
                .to_string();
            let mut title = video_segment
                .split("\" title=\"")
                .collect::<Vec<&str>>()
                .get(1)
                .copied()
                .unwrap_or_default()
                .split("\"")
                .collect::<Vec<&str>>()
                .get(0)
                .copied()
                .unwrap_or_default()
                .to_string();
            // html decode
            title = decode(title.as_bytes()).to_string().unwrap_or(title);
            let id = video_url
                .split("/")
                .collect::<Vec<&str>>()
                .get(4)
                .copied()
                .unwrap_or_default()
                .to_string();
            let raw_duration = video_segment
                .split("fa fa-clock-o")
                .collect::<Vec<&str>>()
                .get(1)
                .copied()
                .unwrap_or_default()
                .split("<span>")
                .collect::<Vec<&str>>()
                .get(1)
                .copied()
                .unwrap_or_default()
                .split("<")
                .collect::<Vec<&str>>()
                .get(0)
                .copied()
                .unwrap_or_default()
                .to_string();
            let duration = parse_time_to_seconds(&raw_duration).unwrap_or(0) as u32;

            let thumb = format!(
                "https:{}",
                video_segment
                    .split(" class=\"thumb lazy-load\"")
                    .collect::<Vec<&str>>()
                    .get(1)
                    .copied()
                    .unwrap_or_default()
                    .split("data-original=\"")
                    .collect::<Vec<&str>>()
                    .get(1)
                    .copied()
                    .unwrap_or_default()
                    .split("\"")
                    .collect::<Vec<&str>>()
                    .get(0)
                    .copied()
                    .unwrap_or_default()
                    .to_string()
            );

            let mut tags = vec![];
            if video_segment.contains("href=\"/sites/") {
                let raw_tags = video_segment.split("href=\"/sites/").collect::<Vec<&str>>()[1..]
                    .iter()
                    .map(|s| {
                        s.split("/\"")
                            .collect::<Vec<&str>>()
                            .get(0)
                            .copied()
                            .unwrap_or_default()
                            .to_string()
                    })
                    .collect::<Vec<String>>();
                for tag in raw_tags {
                    if !tag.is_empty() {
                        self.insert_tag_mapping("sites", &tag, None);
                        tags.push(Self::humanize_slug(&tag));
                    }
                }
            }
            if video_segment.contains("href=\"/models/") {
                let raw_tags = video_segment
                    .split("href=\"/models/")
                    .collect::<Vec<&str>>()[1..]
                    .iter()
                    .map(|s| {
                        s.split("/\"")
                            .collect::<Vec<&str>>()
                            .get(0)
                            .copied()
                            .unwrap_or_default()
                            .to_string()
                    })
                    .collect::<Vec<String>>();
                for tag in raw_tags {
                    if !tag.is_empty() {
                        self.insert_tag_mapping("models", &tag, None);
                        tags.push(Self::humanize_slug(&tag));
                    }
                }
            }

            let views_part = video_segment
                .split("fa fa-eye")
                .collect::<Vec<&str>>()
                .get(1)
                .copied()
                .unwrap_or_default()
                .split("<span>")
                .collect::<Vec<&str>>()
                .get(1)
                .copied()
                .unwrap_or_default()
                .split("<")
                .collect::<Vec<&str>>()
                .get(0)
                .copied()
                .unwrap_or_default()
                .to_string();
            let views = parse_abbreviated_number(&views_part).unwrap_or(0) as u32;

            let video_item = VideoItem::new(
                id,
                title,
                video_url.to_string(),
                "okxxx".to_string(),
                thumb,
                duration,
            )
            .preview(preview_url)
            .views(views)
            .tags(tags);
            items.push(video_item);
        }
        return items;
    }
}

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

    fn get_channel(&self, clientversion: ClientVersion) -> Option<Channel> {
        Some(self.build_channel(clientversion))
    }
}
