use crate::DbPool;
use crate::api::ClientVersion;
use crate::providers::{
    Provider, report_provider_error, report_provider_error_background, requester_or_default,
};
use crate::status::*;
use crate::util::cache::VideoCache;
use crate::videos::{ServerOptions, VideoFormat, VideoItem};
use async_trait::async_trait;
use error_chain::error_chain;
use futures::stream::{self, StreamExt};
use htmlentity::entity::{ICodedDataTrait, decode};

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

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

const ENRICH_CONCURRENCY: usize = 4;

#[derive(Debug, Clone)]
pub struct ParadisehillProvider {
    url: String,
}

impl ParadisehillProvider {
    pub fn new() -> Self {
        ParadisehillProvider {
            url: "https://en.paradisehill.cc".to_string(),
        }
    }

    fn build_channel(&self, _clientversion: ClientVersion) -> Channel {
        Channel {
            id: "paradisehill".to_string(),
            name: "Paradisehill".to_string(),
            description: "Porn Movies on Paradise Hill".to_string(),
            premium: false,
            favicon: "https://www.google.com/s2/favicons?sz=64&domain=en.paradisehill.cc"
                .to_string(),
            status: "active".to_string(),
            categories: vec![],
            options: vec![],
            nsfw: true,
            cacheDuration: None,
            ytdlpCommand: Some("yt-dlp --impersonate chrome-120".to_string())
        }
    }

    // Extracts all part MP4 URLs from the `var videoList = [...]` JS block.
    // Each videoList entry is one sequential part ("Part 1", "Part 2", …).
    fn extract_all_parts(html: &str) -> Vec<String> {
        let start = match html.find("var videoList = ") {
            Some(i) => i,
            None => return vec![],
        };
        let slice = &html[start..];
        let end = slice.find("];").map(|i| i + 2).unwrap_or(slice.len());
        let slice = &slice[..end];

        let mut parts = Vec::new();
        let mut pos = 0;
        while let Some(rel) = slice[pos..].find("\"src\":\"") {
            let src_start = pos + rel + "\"src\":\"".len();
            if let Some(src_end) = slice[src_start..].find('"') {
                let url = slice[src_start..src_start + src_end].replace("\\/", "/");
                if url.starts_with("http") {
                    parts.push(url);
                }
            }
            pos += rel + 1;
        }
        parts
    }

    async fn enrich_item(mut item: VideoItem, options: ServerOptions) -> VideoItem {
        let mut requester = requester_or_default(&options, module_path!(), "enrich_item");
        match requester.get(&item.url, None).await {
            Ok(html) => {
                let parts = Self::extract_all_parts(&html);
                if !parts.is_empty() {
                    item.formats = Some(
                        parts
                            .into_iter()
                            .enumerate()
                            .map(|(i, url)| {
                                VideoFormat::new(
                                    url,
                                    format!("Part {}", i + 1),
                                    "mp4".to_string(),
                                )
                            })
                            .collect(),
                    );
                }
            }
            Err(e) => {
                report_provider_error_background(
                    "paradisehill",
                    "enrich_item",
                    &e.to_string(),
                );
            }
        }
        item
    }

    async fn enrich_all(stubs: Vec<VideoItem>, options: ServerOptions) -> Vec<VideoItem> {
        stream::iter(stubs.into_iter().map(|item| {
            let opts = options.clone();
            async move { Self::enrich_item(item, opts).await }
        }))
        .buffer_unordered(ENRICH_CONCURRENCY)
        .collect::<Vec<_>>()
        .await
    }

    async fn get(
        &self,
        cache: VideoCache,
        page: u8,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let mut requester =
            requester_or_default(&options, module_path!(), "missing_requester");

        let url_str = format!("{}/all/?sort=created_at&page={}", self.url, page);

        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 = match requester.get(&url_str, None).await {
            Ok(text) => text,
            Err(e) => {
                report_provider_error(
                    "paradisehill",
                    "get.request",
                    &format!("url={url_str}; error={e}"),
                )
                .await;
                return Ok(old_items);
            }
        };

        let stubs = self.parse_listing_html(&text);
        if stubs.is_empty() {
            return Ok(old_items);
        }

        let items = Self::enrich_all(stubs, options).await;
        if !items.is_empty() {
            cache.remove(&url_str);
            cache.insert(url_str.clone(), items.clone());
        } else {
            return Ok(old_items);
        }
        Ok(items)
    }

    async fn query(
        &self,
        cache: VideoCache,
        page: u8,
        query: &str,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let mut requester =
            requester_or_default(&options, module_path!(), "missing_requester");
        let search_string = query.replace(" ", "+");
        let url_str = format!(
            "{}/search/?pattern={}&page={}",
            self.url, search_string, page
        );

        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 = match requester.get(&url_str, None).await {
            Ok(text) => text,
            Err(e) => {
                report_provider_error(
                    "paradisehill",
                    "query.request",
                    &format!("url={url_str}; error={e}"),
                )
                .await;
                return Ok(old_items);
            }
        };

        let stubs = self.parse_listing_html(&text);
        if stubs.is_empty() {
            return Ok(old_items);
        }

        let items = Self::enrich_all(stubs, options).await;
        if !items.is_empty() {
            cache.remove(&url_str);
            cache.insert(url_str.clone(), items.clone());
        } else {
            return Ok(old_items);
        }
        Ok(items)
    }

    fn parse_listing_html(&self, html: &str) -> Vec<VideoItem> {
        if html.is_empty() {
            return vec![];
        }
        let mut items: Vec<VideoItem> = Vec::new();
        for video_segment in html.split("item list-film-item").skip(1) {
            let href = video_segment
                .split("<a href=\"")
                .nth(1)
                .and_then(|s| s.split('"').next())
                .unwrap_or_default();
            if href.is_empty() {
                continue;
            }

            let video_url = format!("{}{}", self.url, href);
            let id = href
                .trim_matches('/')
                .split('/')
                .next()
                .unwrap_or_default()
                .to_string();
            if id.is_empty() {
                continue;
            }

            let mut title = video_segment
                .split("itemprop=\"name\">")
                .nth(1)
                .and_then(|s| s.split('<').next())
                .unwrap_or_default()
                .trim()
                .to_string();
            title = decode(title.as_bytes()).to_string().unwrap_or(title);

            let mut thumb = video_segment
                .split("itemprop=\"image\" src=\"")
                .nth(1)
                .and_then(|s| s.split('"').next())
                .unwrap_or_default()
                .to_string();
            if thumb.starts_with('/') {
                thumb = format!("{}{}", self.url, thumb);
            }

            let genre = video_segment
                .split("itemprop=\"genre\">")
                .nth(1)
                .and_then(|s| s.split('<').next())
                .unwrap_or_default()
                .trim()
                .to_string();
            let tags = if genre.is_empty() {
                vec![]
            } else {
                vec![genre]
            };

            items.push(
                VideoItem::new(id, title, video_url, "paradisehill".to_string(), thumb, 0)
                    .aspect_ratio(0.697674419_f32)
                    .tags(tags),
            );
        }

        items
    }
}

#[async_trait]
impl Provider for ParadisehillProvider {
    async fn get_videos(
        &self,
        cache: VideoCache,
        pool: DbPool,
        sort: String,
        query: Option<String>,
        page: String,
        per_page: String,
        options: ServerOptions,
    ) -> Vec<VideoItem> {
        let _ = pool;
        let _ = sort;
        let _ = per_page;
        let videos: std::result::Result<Vec<VideoItem>, Error> = match query {
            Some(q) => {
                self.query(cache, page.parse::<u8>().unwrap_or(1), &q, options)
                    .await
            }
            None => self.get(cache, page.parse::<u8>().unwrap_or(1), options).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))
    }
}
