use crate::DbPool;
use crate::api::ClientVersion;
use crate::providers::Provider;
use crate::status::*;
use crate::util::cache::VideoCache;
use crate::util::requester::Requester;
use crate::videos::ServerOptions;
use crate::videos::VideoItem;
use async_trait::async_trait;
use error_chain::error_chain;
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);
    }
}

#[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,
        }
    }
    async fn get(
        &self,
        cache: VideoCache,
        page: u8,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        let mut requester =
            crate::providers::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) => {
                crate::providers::report_provider_error(
                    "paradisehill",
                    "get.request",
                    &format!("url={url_str}; error={e}"),
                )
                .await;
                return Ok(old_items);
            }
        };
        // Pass a reference to options if needed, or reconstruct as needed
        let video_items: Vec<VideoItem> = self
            .get_video_items_from_html(text.clone(), 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,
        page: u8,
        query: &str,
        options: ServerOptions,
    ) -> Result<Vec<VideoItem>> {
        // Extract needed fields from options at the start
        let mut requester =
            crate::providers::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
        );
        // 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 = match requester.get(&url_str, None).await {
            Ok(text) => text,
            Err(e) => {
                crate::providers::report_provider_error(
                    "paradisehill",
                    "query.request",
                    &format!("url={url_str}; error={e}"),
                )
                .await;
                return Ok(old_items);
            }
        };
        let video_items: Vec<VideoItem> = self
            .get_video_items_from_html(text.clone(), 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,
        _requester: Requester,
    ) -> Vec<VideoItem> {
        if html.is_empty() {
            println!("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 as 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))
    }
}
