use std::collections::HashMap;

use crate::domain::Relations;
use crate::domain::book::Book;
use crate::domain::episode::Episode;
use crate::domain::media::Media;
use crate::domain::movie::Movie;
use crate::domain::person::Person;
use crate::domain::serie::Serie;
use crate::{CustomParamTypes, PluginCredential};
use crate::request::RsGroupDownload;
use crate::domain::rs_ids::{ApplyRsIds, RsIds};
use crate::request::RsRequest;
use serde::{Deserialize, Serialize};
use strum_macros::EnumString;

#[derive(
    Debug, Serialize, Deserialize, Clone, PartialEq, strum_macros::Display, EnumString, Default,
)]
#[serde(rename_all = "camelCase")]
#[strum(serialize_all = "camelCase")]
pub enum RsLookupSourceResult {
    Requests(Vec<RsRequest>),
    GroupRequest(Vec<RsGroupDownload>),
    NotFound,
    #[default]
    NotApplicable,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupPerson {
    pub name: Option<String>,
    pub ids: Option<RsIds>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_key: Option<String>

    
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupSerie {
    pub name: Option<String>,
    pub ids: Option<RsIds>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_key: Option<String>
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupSerieSeason {
    pub name: Option<String>,
    pub ids: Option<RsIds>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_key: Option<String>
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupEpisode {
    pub name: Option<String>,
    pub ids: Option<RsIds>,

    pub season: u32,
    pub number: Option<u32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_key: Option<String>
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupBook {
    pub name: Option<String>,
    pub ids: Option<RsIds>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_key: Option<String>
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupSong {
    pub title: Option<String>,
    pub author: Option<String>,
    pub album: Option<String>,
    pub ids: Option<RsIds>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_key: Option<String>
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupMedia {
    pub search: Option<String>,
    pub ids: Option<RsIds>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_key: Option<String>
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupMovie {
    pub name: Option<String>,
    pub ids: Option<RsIds>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_key: Option<String>
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, strum_macros::Display, EnumString)]
#[serde(rename_all = "camelCase")]
#[strum(serialize_all = "camelCase")]
pub enum RsLookupQuery {
    Book(RsLookupBook),
    Media(RsLookupMedia),
    Episode(RsLookupEpisode),
    Movie(RsLookupMovie),
    Person(RsLookupPerson),
    Serie(RsLookupSerie),
    SerieSeason(RsLookupSerieSeason),
    Song(RsLookupSong),
}

#[derive(
    Debug, Serialize, Deserialize, Clone, PartialEq, strum_macros::Display, EnumString,
)]
#[serde(rename_all = "camelCase")]
#[strum(serialize_all = "camelCase")]
pub enum RsLookupMatchType {
    ExactId,
    ExactText,
}

#[derive(
    Debug, Serialize, Deserialize, Clone, PartialEq, strum_macros::Display, EnumString, Default,
)]
#[serde(rename_all = "camelCase")]
#[strum(serialize_all = "camelCase")]
pub enum RsLookupMetadataResult {
    Book(Book),
    Media(Media),
    Episode(Episode),
    Movie(Movie),
    Person(Person),
    Serie(Serie),
    #[default]
    None,
}

impl RsLookupMetadataResult {
    /// Extract all IDs from the inner entity as an `RsIds` set.
    pub fn extract_ids(&self) -> Option<RsIds> {
        match self {
            Self::Movie(m) => Some(RsIds::from(m.clone())),
            Self::Serie(s) => Some(RsIds::from(s.clone())),
            Self::Book(b) => Some(RsIds::from(b.clone())),
            Self::Person(p) => Some(RsIds::from(p.clone())),
            Self::Episode(e) => Some(RsIds::from(e.clone())),
            Self::Media(_) | Self::None => None,
        }
    }

    /// Apply merged IDs back onto the inner entity.
    pub fn apply_ids(&mut self, ids: &RsIds) {
        match self {
            Self::Movie(m) => m.apply_rs_ids(ids),
            Self::Serie(s) => s.apply_rs_ids(ids),
            Self::Book(b) => b.apply_rs_ids(ids),
            Self::Person(p) => p.apply_rs_ids(ids),
            Self::Episode(e) => e.apply_rs_ids(ids),
            Self::Media(_) | Self::None => {}
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupMetadataResults {
    pub results: Vec<RsLookupMetadataResultWrapper>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_page_key: Option<String>,
    
}

#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupMetadataResultWrapper {
    pub metadata: RsLookupMetadataResult,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub relations: Option<Relations>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub match_type: Option<RsLookupMatchType>,

}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RsLookupWrapper {
    pub query: RsLookupQuery,
    pub credential: Option<PluginCredential>,
    pub params: Option<HashMap<String, CustomParamTypes>>,
}
