use crate::DbPool;
use crate::api::ClientVersion;
use crate::providers::Provider;
use crate::status::*;
use crate::util::cache::VideoCache;
use crate::util::discord::{format_error_chain, send_discord_error_report};
use crate::util::requester::Requester;
use crate::util::time::parse_time_to_seconds;
use crate::videos::{ServerOptions, VideoFormat, VideoItem};
use async_trait::async_trait;
use error_chain::error_chain;
use futures::stream::{FuturesUnordered, StreamExt};
use htmlentity::entity::{ICodedDataTrait, decode};
use std::sync::{Arc, RwLock};
use std::{thread, vec};
use titlecase::Titlecase;

pub const CHANNEL_METADATA: crate::providers::ProviderChannelMetadata =
    crate::providers::ProviderChannelMetadata {
        group_id: "studio-network",
        tags: &["studio", "hd", "scenes"],
    };

error_chain! {
    foreign_links {
        Io(std::io::Error);
        HttpRequest(wreq::Error);
        Json(serde_json::Error);
    }
    errors {
        Parse(msg: String) {
            description("parse error")
            display("parse error: {}", msg)
        }
    }
}

#[derive(Debug, Clone)]
pub struct HqpornerProvider {
    url: String,
    stars: Arc<RwLock<Vec<FilterOption>>>,
    categories: Arc<RwLock<Vec<FilterOption>>>,
}

impl HqpornerProvider {
    pub fn new() -> Self {
        let provider = HqpornerProvider {
            url: "https://hqporner.com".to_string(),
            stars: Arc::new(RwLock::new(vec![])),
            categories: Arc::new(RwLock::new(vec![])),
        };
        provider.spawn_initial_load();
        provider
    }

    fn spawn_initial_load(&self) {
        let url = self.url.clone();
        let stars = Arc::clone(&self.stars);
        let categories = Arc::clone(&self.categories);

        thread::spawn(move || {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build();

            if let Ok(runtime) = rt {
                runtime.block_on(async move {
                    if let Err(e) = Self::load_stars(&url, stars).await {
                        eprintln!("load_stars failed: {e}");
                    }
                    if let Err(e) = Self::load_categories(&url, categories).await {
                        eprintln!("load_categories failed: {e}");
                    }
                });
            }
        });
    }

    async fn load_stars(base_url: &str, stars: Arc<RwLock<Vec<FilterOption>>>) -> Result<()> {
        let mut requester = Requester::new();
        let text = requester
            .get(&format!("{}/girls", base_url), None)
            .await
            .map_err(|e| Error::from(format!("Request failed: {}", e)))?;

        let stars_div = text
            .split("<span>Girls</span>")
            .last()
            .and_then(|s| s.split("</ul>").next())
            .ok_or_else(|| Error::from("Could not find stars div"))?;

        for stars_element in stars_div.split("<li ").skip(1) {
            let star_id = stars_element
                .split("href=\"/actress/")
                .nth(1)
                .and_then(|s| s.split('"').next())
                .map(|s| s.to_string());

            let star_name = stars_element
                .split("<a ")
                .nth(1)
                .and_then(|s| s.split('>').nth(1))
                .and_then(|s| s.split('<').next())
                .map(|s| s.to_string());

            if let (Some(id), Some(name)) = (star_id, star_name) {
                Self::push_unique(&stars, FilterOption { id, title: name });
            }
        }
        Ok(())
    }

    async fn load_categories(
        base_url: &str,
        categories: Arc<RwLock<Vec<FilterOption>>>,
    ) -> Result<()> {
        let mut requester = Requester::new();
        let text = requester
            .get(&format!("{}/categories", base_url), None)
            .await
            .map_err(|e| Error::from(format!("Request failed: {}", e)))?;

        let categories_div = text
            .split("<span>Categories</span>")
            .last()
            .and_then(|s| s.split("</ul>").next())
            .ok_or_else(|| Error::from("Could not find categories div"))?;

        for categories_element in categories_div.split("<li ").skip(1) {
            let category_id = categories_element
                .split("href=\"/category/")
                .nth(1)
                .and_then(|s| s.split('"').next())
                .map(|s| s.to_string());

            let category_name = categories_element
                .split("<a ")
                .nth(1)
                .and_then(|s| s.split('>').nth(1))
                .and_then(|s| s.split('<').next())
                .map(|s| s.titlecase());

            if let (Some(id), Some(name)) = (category_id, category_name) {
                Self::push_unique(&categories, FilterOption { id, title: name });
            }
        }
        Ok(())
    }

    fn build_channel(&self, _clientversion: ClientVersion) -> Channel {
        Channel {
            id: "hqporner".to_string(),
            name: "HQPorner".to_string(),
            description: "HD Porn Videos Tube".to_string(),
            premium: false,
            favicon: "https://www.google.com/s2/favicons?sz=64&domain=hqporner.com".to_string(),
            status: "active".to_string(),
            categories: self
                .categories
                .read()
                .map(|c| c.iter().map(|o| o.title.clone()).collect())
                .unwrap_or_default(),
            options: vec![],
            nsfw: true,
            cacheDuration: None,
        }
    }

    fn push_unique(target: &Arc<RwLock<Vec<FilterOption>>>, item: FilterOption) {
        if let Ok(mut vec) = target.write() {
            if !vec.iter().any(|x| x.id == item.id) {
                vec.push(item);
            }
        }
    }

    async fn get(
        &self,
        cache: VideoCache,
        page: u8,
        _sort: &str,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let video_url = format!("{}/hdporn/{}", self.url, page);
        if let Some((time, items)) = cache.get(&video_url) {
            if time.elapsed().unwrap_or_default().as_secs() < 300 {
                return Ok(items.clone());
            }
        }

        let mut requester = options.requester.clone().ok_or("No requester")?;
        let text = requester
            .get(&video_url, None)
            .await
            .map_err(|e| Error::from(format!("Request failed: {}", e)))?;

        let video_items = self
            .get_video_items_from_html(text, &mut requester, &options)
            .await;
        if !video_items.is_empty() {
            cache.insert(video_url, video_items.clone());
        }
        Ok(video_items)
    }

    async fn query(
        &self,
        cache: VideoCache,
        page: u8,
        query: &str,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let search_string = query.trim().to_lowercase();
        let mut video_url = format!("{}/?q={}&p={}", self.url, search_string, page);

        if let Ok(stars) = self.stars.read() {
            if let Some(star) = stars
                .iter()
                .find(|s| s.title.to_lowercase() == search_string)
            {
                video_url = format!("{}/actress/{}/{}", self.url, star.id, page);
            }
        }
        if let Ok(cats) = self.categories.read() {
            if let Some(cat) = cats
                .iter()
                .find(|c| c.title.to_lowercase() == search_string)
            {
                video_url = format!("{}/category/{}/{}", self.url, cat.id, page);
            }
        }

        if let Some((time, items)) = cache.get(&video_url) {
            if time.elapsed().unwrap_or_default().as_secs() < 300 {
                return Ok(items.clone());
            }
        }

        let mut requester = options.requester.clone().ok_or("No requester")?;
        let text = requester
            .get(&video_url, None)
            .await
            .map_err(|e| Error::from(format!("Request failed: {}", e)))?;

        let video_items = self
            .get_video_items_from_html(text, &mut requester, &options)
            .await;
        if !video_items.is_empty() {
            cache.insert(video_url, video_items.clone());
        }
        Ok(video_items)
    }

    async fn get_video_items_from_html(
        &self,
        html: String,
        requester: &mut Requester,
        options: &ServerOptions,
    ) -> Vec<VideoItem> {
        if html.is_empty() || html.contains("404 Not Found") {
            return vec![];
        }

        let raw_videos: Vec<String> = html
            .split("id=\"footer\"")
            .next()
            .and_then(|s| s.split("<section class=\"box features\">").nth(2))
            .map(|s| {
                s.split("<section class=\"box feature\">")
                    .skip(1)
                    .map(|v| v.to_string())
                    .collect()
            })
            .unwrap_or_default();

        // Limit concurrent detail-page requests to reduce transient connect errors.
        let mut in_flight = FuturesUnordered::new();
        let mut iter = raw_videos.into_iter();
        let mut items = Vec::new();
        const MAX_IN_FLIGHT: usize = 6;

        loop {
            while in_flight.len() < MAX_IN_FLIGHT {
                let Some(seg) = iter.next() else {
                    break;
                };
                in_flight.push(self.get_video_item(seg, requester.clone(), options));
            }

            let Some(result) = in_flight.next().await else {
                break;
            };
            match result {
                Ok(item)
                    if item
                        .formats
                        .as_ref()
                        .map(|formats| !formats.is_empty())
                        .unwrap_or(false) =>
                {
                    items.push(item);
                }
                Ok(_) => {}
                Err(e) => {
                    let msg = e.to_string();
                    let chain = format_error_chain(&e);
                    tokio::spawn(async move {
                        let _ = send_discord_error_report(
                            msg,
                            Some(chain),
                            Some("Hqporner Provider"),
                            None,
                            file!(),
                            line!(),
                            module_path!(),
                        )
                        .await;
                    });
                }
            }
        }

        items
    }

    async fn get_video_item(
        &self,
        seg: String,
        mut requester: Requester,
        options: &ServerOptions,
    ) -> Result<VideoItem> {
        let video_url = format!(
            "{}{}",
            self.url,
            seg.split("<a href=\"")
                .nth(1)
                .and_then(|s| s.split('"').next())
                .ok_or_else(|| ErrorKind::Parse(format!("url \n{seg}").into()))?
        );
        let title_raw = seg
            .split("<h3 class=\"meta-data-title\">")
            .nth(1)
            .and_then(|s| s.split('>').nth(1))
            .and_then(|s| s.split('<').next())
            .ok_or_else(|| ErrorKind::Parse(format!("title \n{seg}").into()))?;
        let title = decode(title_raw.as_bytes())
            .to_string()
            .unwrap_or_else(|_| title_raw.to_string())
            .titlecase();

        let id = video_url
            .split('/')
            .nth(4)
            .and_then(|s| s.split('.').next())
            .ok_or_else(|| ErrorKind::Parse(format!("id \n{seg}").into()))?
            .to_string();
        let thumb_raw = seg
            .split("onmouseleave='defaultImage(\"")
            .nth(1)
            .and_then(|s| s.split('"').next())
            .ok_or_else(|| ErrorKind::Parse(format!("thumb \n{seg}").into()))?;
        let thumb_abs = if thumb_raw.starts_with("//") {
            format!("https:{}", thumb_raw)
        } else if thumb_raw.starts_with("http://") || thumb_raw.starts_with("https://") {
            thumb_raw.to_string()
        } else {
            format!("https://{}", thumb_raw.trim_start_matches('/'))
        };
        let thumb = match thumb_abs.strip_prefix("https://") {
            Some(path) => crate::providers::build_proxy_url(options, "hqporner-thumb", path),
            None => thumb_abs,
        };
        let raw_duration = seg
            .split("<span class=\"icon fa-clock-o meta-data\">")
            .nth(1)
            .and_then(|s| s.split("s<").next())
            .map(|s| s.replace("m ", ":"))
            .unwrap_or_default();
        let duration = parse_time_to_seconds(&raw_duration).unwrap_or(0) as u32;

        let (tags, formats) = self.extract_media(&video_url, &mut requester).await?;

        Ok(
            VideoItem::new(id, title, video_url, "hqporner".into(), thumb, duration)
                .formats(formats)
                .tags(tags),
        )
    }

    async fn extract_media(
        &self,
        url: &str,
        requester: &mut Requester,
    ) -> Result<(Vec<String>, Vec<VideoFormat>)> {
        let mut formats = vec![];
        let mut tags = vec![];
        let headers = vec![("Referer".to_string(), "https://hqporner.com/".into())];
        let mut text = match self
            .fetch_text_with_retries(requester, url, &headers, 3)
            .await
        {
            Ok(text) => text,
            Err(primary_err) => {
                if url.contains("://hqporner.com/") {
                    let fallback_url = url.replace("://hqporner.com/", "://www.hqporner.com/");
                    self.fetch_text_with_retries(requester, &fallback_url, &headers, 3)
                        .await
                        .map_err(|fallback_err| {
                            Error::from(format!(
                                "Request failed: primary={primary_err}; fallback={fallback_err}"
                            ))
                        })?
                } else {
                    return Err(Error::from(format!("Request failed: {}", primary_err)));
                }
            }
        };

        if text.is_empty() && url.contains("://hqporner.com/") {
            let fallback_url = url.replace("://hqporner.com/", "://www.hqporner.com/");
            text = self
                .fetch_text_with_retries(requester, &fallback_url, &headers, 3)
                .await
                .unwrap_or_default();
        }

        if text.contains("Why do I see it?") {
            return Ok((tags, formats));
        }

        // Extract Stars & Tags
        if let Some(stars_block) = text
            .split("icon fa-star-o")
            .nth(1)
            .and_then(|s| s.split("</li>").next())
        {
            for star_el in stars_block.split("href=\"/actress/").skip(1) {
                let id = star_el.split('"').next().unwrap_or("").to_string();
                let name = star_el
                    .split("\">")
                    .nth(1)
                    .and_then(|s| s.split('<').next())
                    .unwrap_or("")
                    .to_string();
                if !name.is_empty() {
                    tags.push(name.clone());
                    Self::push_unique(&self.stars, FilterOption { id, title: name });
                }
            }
        }

        // Player / Video Extraction
        let player_url = format!(
            "https:{}",
            text.split("url: '/blocks/altplayer.php?i=")
                .nth(1)
                .and_then(|s| s.split('\'').next())
                .ok_or("No player link")?
        );
        let response_text = match self
            .fetch_text_with_retries(requester, &player_url, &headers, 2)
            .await
        {
            Ok(text) => text,
            Err(e) => {
                let err = format!("altplayer request failed: {e}");
                send_discord_error_report(
                    err.clone(),
                    None,
                    Some("Hqporner Provider"),
                    Some(&player_url),
                    file!(),
                    line!(),
                    module_path!(),
                )
                .await;
                return Ok((tags, formats));
            }
        };
        let text2 = response_text;

        // Check for error response
        if text2.starts_with("ERR:") {
            return Ok((tags, formats));
        }

        let video_element = text2
            .split("<video ")
            .nth(2)
            .and_then(|s| s.split("</video>").next())
            .ok_or(format!("No video element\n{player_url}\n{text2}"))?;
        for source in video_element.split("<source ").skip(1) {
            let title = source
                .split("title=\\\"")
                .nth(1)
                .and_then(|s| s.split("\\\"").next())
                .unwrap_or("")
                .to_string();
            let quality = title.split(' ').next().unwrap_or("HD").to_string();
            let media_url = format!(
                "https:{}",
                source
                    .split("src=\\\"")
                    .nth(1)
                    .and_then(|s| s.split("\\\"").next())
                    .unwrap_or("")
            );

            formats.push(
                VideoFormat::new(media_url, quality, "mp4".into())
                    .format_id(title.clone())
                    .format_note(title),
            );
        }

        Ok((tags, formats))
    }

    async fn fetch_text_with_retries(
        &self,
        requester: &mut Requester,
        url: &str,
        headers: &[(String, String)],
        max_attempts: u8,
    ) -> std::result::Result<String, String> {
        let mut last_err = String::new();

        for attempt in 1..=max_attempts {
            match requester.get_raw_with_headers(url, headers.to_vec()).await {
                Ok(resp) => match resp.text().await {
                    Ok(text) => return Ok(text),
                    Err(e) => {
                        last_err =
                            format!("text read failed (attempt {attempt}/{max_attempts}): {e}");
                    }
                },
                Err(e) => {
                    last_err = format!("request failed (attempt {attempt}/{max_attempts}): {e}");
                }
            }

            if attempt < max_attempts {
                tokio::time::sleep(std::time::Duration::from_millis(250 * attempt as u64)).await;
            }
        }

        Err(last_err)
    }
}

#[async_trait]
impl Provider for HqpornerProvider {
    async fn get_videos(
        &self,
        cache: VideoCache,
        _pool: DbPool,
        sort: String,
        query: Option<String>,
        page: String,
        _per_page: String,
        options: ServerOptions,
    ) -> Vec<VideoItem> {
        let page_num = page.parse::<u8>().unwrap_or(1);
        let res = match query {
            Some(q) => self.query(cache, page_num, &q, options).await,
            None => self.get(cache, page_num, &sort, options).await,
        };
        res.unwrap_or_else(|e| {
            eprintln!("Hqporner error: {e}");
            let _ = send_discord_error_report(
                e.to_string(),
                Some(format_error_chain(&e)),
                None,
                None,
                file!(),
                line!(),
                module_path!(),
            );
            vec![]
        })
    }

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