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, VideoItem};

use async_trait::async_trait;
use error_chain::error_chain;
use htmlentity::entity::{ICodedDataTrait, decode};
use std::sync::{Arc, RwLock};
use std::{thread, vec};
use titlecase::Titlecase;
use wreq::Version;

pub const CHANNEL_METADATA: crate::providers::ProviderChannelMetadata =
    crate::providers::ProviderChannelMetadata {
        group_id: "fetish-kink",
        tags: &["hypnosis", "fetish", "sissy"],
    };

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 HypnotubeProvider {
    url: String,
    categories: Arc<RwLock<Vec<FilterOption>>>,
}

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

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

        thread::spawn(async move || {
            let rt = match tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
            {
                Ok(rt) => rt,
                Err(e) => {
                    eprintln!("tokio runtime failed: {e}");
                    send_discord_error_report(
                        e.to_string(),
                        Some(format_error_chain(&e)),
                        Some("HypnoTube Provider"),
                        Some("Failed to create tokio runtime"),
                        file!(),
                        line!(),
                        module_path!(),
                    )
                    .await;
                    return;
                }
            };

            rt.block_on(async {
                if let Err(e) = Self::load_categories(&url, Arc::clone(&categories)).await {
                    eprintln!("load_categories failed: {e}");
                    send_discord_error_report(
                        e.to_string(),
                        Some(format_error_chain(&e)),
                        Some("HypnoTube Provider"),
                        Some("Failed to load categories during initial load"),
                        file!(),
                        line!(),
                        module_path!(),
                    )
                    .await;
                }
            });
        });
    }

    async fn load_categories(base: &str, cats: Arc<RwLock<Vec<FilterOption>>>) -> Result<()> {
        let mut requester = Requester::new();
        let text = requester
            .get(&format!("{base}/channels/"), Some(Version::HTTP_11))
            .await
            .map_err(|e| Error::from(format!("{}", e)))?;

        let block = text
            .split(" title END ")
            .last()
            .ok_or_else(|| ErrorKind::Parse("categories block".into()))?
            .split(" main END ")
            .next()
            .unwrap_or("");

        for el in block.split("<!-- item -->").skip(1) {
            let id = el
                .split("<a href=\"https://hypnotube.com/channels/")
                .nth(1)
                .and_then(|s| s.split("/\"").next())
                .ok_or_else(|| ErrorKind::Parse(format!("category id: {el}").into()))?
                .to_string();

            let title = el
                .split("title=\"")
                .nth(1)
                .and_then(|s| s.split("\"").next())
                .ok_or_else(|| ErrorKind::Parse(format!("category title: {el}").into()))?
                .titlecase();

            Self::push_unique(&cats, FilterOption { id, title });
        }
        Ok(())
    }

    fn build_channel(&self, clientversion: ClientVersion) -> Channel {
        let _ = clientversion;
        Channel {
            id: "hypnotube".to_string(),
            name: "Hypnotube".to_string(),
            description: "free video hypno tube for the sissy hypnosis porn fetish".to_string(),
            premium: false,
            favicon: "https://www.google.com/s2/favicons?sz=64&domain=hypnotube.com".to_string(),
            status: "active".to_string(),
            categories: self
                .categories
                .read()
                .map(|categories| categories.iter().map(|c| c.title.clone()).collect())
                .unwrap_or_else(|e| {
                    eprint!("Hypnotube categories lock error: {e}");
                    crate::providers::report_provider_error_background(
                        "hypnotube",
                        "build_channel.categories_read",
                        &e.to_string(),
                    );
                    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: "most recent".into(),
                        title: "Most Recent".into(),
                    },
                    FilterOption {
                        id: "most viewed".into(),
                        title: "Most Viewed".into(),
                    },
                    FilterOption {
                        id: "top rated".into(),
                        title: "Top Rated".into(),
                    },
                    FilterOption {
                        id: "longest".into(),
                        title: "Longest".into(),
                    },
                ],
                multiSelect: false,
            }],
            nsfw: true,
            cacheDuration: Some(1800),
            ytdlpCommand: Some("yt-dlp --impersonate chrome-120".to_string())
        }
    }

    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,
    ) -> Vec<VideoItem> {
        let sort_string = match sort {
            "top rated" => "top-rated",
            "most viewed" => "most-viewed",
            "longest" => "longest",
            _ => "videos",
        };
        let video_url = format!("{}/{}/page{}.html", 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 items.clone();
                } else {
                    items.clone()
                }
            }
            None => {
                vec![]
            }
        };
        let mut requester =
            crate::providers::requester_or_default(&options, module_path!(), "missing_requester");
        let text = match requester.get(&video_url, Some(Version::HTTP_11)).await {
            Ok(text) => text,
            Err(e) => {
                crate::providers::report_provider_error(
                    "hypnotube",
                    "get.request",
                    &format!("url={video_url}; error={e}"),
                )
                .await;
                return old_items;
            }
        };
        if text.contains("Sorry, no results were found.") {
            return vec![];
        }
        let video_items: Vec<VideoItem> = self.get_video_items_from_html(text.clone()).await;
        if !video_items.is_empty() {
            cache.remove(&video_url);
            cache.insert(video_url.clone(), video_items.clone());
        } else {
            return old_items;
        }
        video_items
    }

    async fn query(
        &self,
        cache: VideoCache,
        page: u8,
        query: &str,
        options: ServerOptions,
    ) -> Vec<VideoItem> {
        let sort_string = match options.sort.as_deref().unwrap_or("") {
            "top rated" => "rating",
            "most viewed" => "views",
            "longest" => "longest",
            _ => "newest",
        };
        let video_url = format!(
            "{}/search/videos/{}/{}/page{}.html",
            self.url,
            query.trim().replace(" ", "%20"),
            sort_string,
            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 items.clone();
                } else {
                    let _ = cache.check().await;
                    return items.clone();
                }
            }
            None => {
                vec![]
            }
        };

        let mut requester =
            crate::providers::requester_or_default(&options, module_path!(), "missing_requester");
        let post_response = match requester
            .post(
                format!("{}/searchgate.php", self.url).as_str(),
                format!("q={}&type=videos", query.replace(" ", "+")).as_str(),
                vec![("Content-Type", "application/x-www-form-urlencoded")],
            )
            .await
        {
            Ok(response) => response,
            Err(e) => {
                crate::providers::report_provider_error(
                    "hypnotube",
                    "query.search_post",
                    &format!("url={video_url}; error={e}"),
                )
                .await;
                return old_items;
            }
        };
        let text = match post_response.text().await {
            Ok(t) => t,
            Err(e) => {
                eprint!("Hypnotube search POST request failed: {}", e);
                crate::providers::report_provider_error_background(
                    "hypnotube",
                    "query.search_post.text",
                    &e.to_string(),
                );
                return vec![];
            }
        };
        // println!("Hypnotube search POST response status: {}", p.text().await.unwrap_or_default());
        // let text = requester.get(&video_url, Some(Version::HTTP_11)).await.unwrap();
        if text.contains("Sorry, no results were found.") {
            return vec![];
        }
        let video_items: Vec<VideoItem> = self.get_video_items_from_html(text.clone()).await;
        if !video_items.is_empty() {
            cache.remove(&video_url);
            cache.insert(video_url.clone(), video_items.clone());
        } else {
            return old_items;
        }
        video_items
    }

    async fn get_video_items_from_html(&self, html: String) -> Vec<VideoItem> {
        if html.is_empty() || html.contains("404 Not Found") {
            eprint!("Hypnotube returned empty or 404 html");
            return vec![];
        }

        let block = match html
            .split("pagination-col col pagination")
            .next()
            .and_then(|s| s.split(" title END ").last())
        {
            Some(b) => b,
            None => {
                eprint!("Hypnotube Provider: Failed to get block from html");
                let e = Error::from(ErrorKind::Parse("html".into()));
                send_discord_error_report(
                    e.to_string(),
                    Some(format_error_chain(&e)),
                    Some("Hypnotube Provider"),
                    Some(&format!("Failed to get block from html:\n```{html}\n```")),
                    file!(),
                    line!(),
                    module_path!(),
                )
                .await;
                return vec![];
            }
        };
        let mut items = vec![];
        for seg in block.split("<!-- item -->").skip(1) {
            let video_url = match seg
                .split(" href=\"")
                .nth(1)
                .and_then(|s| s.split('"').next())
            {
                Some(url) => url.to_string(),
                None => {
                    eprint!("Hypnotube Provider: Failed to parse video url from segment");
                    let e = Error::from(ErrorKind::Parse("video url".into()));
                    send_discord_error_report(
                        e.to_string(),
                        Some(format_error_chain(&e)),
                        Some("Hypnotube Provider"),
                        Some(&format!(
                            "Failed to parse video url from segment:\n```{seg}\n```"
                        )),
                        file!(),
                        line!(),
                        module_path!(),
                    )
                    .await;
                    continue;
                }
            };

            let mut title = seg
                .split(" title=\"")
                .nth(1)
                .and_then(|s| s.split('"').next())
                .unwrap_or_default()
                .trim()
                .to_string();

            title = decode(title.clone().as_bytes())
                .to_string()
                .unwrap_or(title)
                .titlecase();
            let id = video_url
                .split('/')
                .nth(4)
                .and_then(|s| s.split('.').next())
                .ok_or_else(|| ErrorKind::Parse("video id".into()))
                .unwrap_or_else(|_| &title.as_str());
            let thumb = seg
                .split("<img ")
                .nth(1)
                .and_then(|s| s.split("src=\"").nth(1))
                .and_then(|s| s.split("\"").next())
                .ok_or_else(|| ErrorKind::Parse("thumb block".into()))
                .unwrap_or("")
                .to_string();
            let raw_duration = seg
                .split("<span class=\"time\">")
                .nth(1)
                .and_then(|s| s.split('<').next())
                .unwrap_or("")
                .to_string();
            let duration = parse_time_to_seconds(&raw_duration).unwrap_or(0) as u32;

            let views = seg
                .split("<span class=\"icon i-eye\"></span>")
                .nth(1)
                .and_then(|s| s.split("span class=\"sub-desc\">").nth(1))
                .and_then(|s| s.split("<").next())
                .unwrap_or("0")
                .replace(",", "")
                .parse::<u32>()
                .unwrap_or(0);
            let video_item = VideoItem::new(
                id.to_owned(),
                title,
                video_url,
                "hypnotube".into(),
                thumb,
                duration,
            )
            .views(views);
            items.push(video_item);
        }
        items
    }
}

#[async_trait]
impl Provider for HypnotubeProvider {
    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 = page.parse::<u8>().unwrap_or(1);

        let res = match query {
            Some(q) => self.to_owned().query(cache, page, &q, options).await,
            None => self.get(cache, page, &sort, options).await,
        };

        return res;
    }

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