use std::{borrow::Cow, hash::{Hash, Hasher}, path::PathBuf};

use anyhow::{Result, anyhow};
use serde::{Deserialize, Deserializer, Serialize};
use typed_path::{UnixPath, UnixPathBuf};

use crate::{loc::{Loc, LocBuf, LocCow}, path::{PathBufDyn, PathCow, PathDyn}, pool::SymbolCow, scheme::{AsScheme, Scheme, SchemeCow, SchemeKind, SchemeRef}, url::{AsUrl, Url, UrlBuf}};

#[derive(Clone, Debug)]
pub enum UrlCow<'a> {
	Regular(LocCow<'a>),
	Search { loc: LocCow<'a>, domain: SymbolCow<'a, str> },
	Archive { loc: LocCow<'a>, domain: SymbolCow<'a, str> },
	Sftp { loc: LocCow<'a, &'a UnixPath, UnixPathBuf>, domain: SymbolCow<'a, str> },
}

// FIXME: remove
impl Default for UrlCow<'_> {
	fn default() -> Self { Self::Regular(Default::default()) }
}

impl<'a> From<Url<'a>> for UrlCow<'a> {
	fn from(value: Url<'a>) -> Self {
		match value {
			Url::Regular(loc) => Self::Regular(loc.into()),
			Url::Search { loc, domain } => Self::Search { loc: loc.into(), domain: domain.into() },
			Url::Archive { loc, domain } => Self::Archive { loc: loc.into(), domain: domain.into() },
			Url::Sftp { loc, domain } => Self::Sftp { loc: loc.into(), domain: domain.into() },
		}
	}
}

impl<'a, T> From<&'a T> for UrlCow<'a>
where
	T: AsUrl + ?Sized,
{
	fn from(value: &'a T) -> Self { value.as_url().into() }
}

impl From<UrlBuf> for UrlCow<'_> {
	fn from(value: UrlBuf) -> Self {
		match value {
			UrlBuf::Regular(loc) => Self::Regular(loc.into()),
			UrlBuf::Search { loc, domain } => Self::Search { loc: loc.into(), domain: domain.into() },
			UrlBuf::Archive { loc, domain } => {
				Self::Archive { loc: loc.into(), domain: domain.into() }
			}
			UrlBuf::Sftp { loc, domain } => Self::Sftp { loc: loc.into(), domain: domain.into() },
		}
	}
}

impl From<PathBuf> for UrlCow<'_> {
	fn from(value: PathBuf) -> Self { UrlBuf::from(value).into() }
}

impl From<UrlCow<'_>> for UrlBuf {
	fn from(value: UrlCow<'_>) -> Self { value.into_owned() }
}

impl From<&UrlCow<'_>> for UrlBuf {
	fn from(value: &UrlCow<'_>) -> Self { value.as_url().into() }
}

impl<'a> TryFrom<&'a [u8]> for UrlCow<'a> {
	type Error = anyhow::Error;

	fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> { SchemeCow::parse(value)?.try_into() }
}

impl TryFrom<Vec<u8>> for UrlCow<'_> {
	type Error = anyhow::Error;

	fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
		Ok(UrlCow::try_from(value.as_slice())?.into_owned().into())
	}
}

impl<'a> TryFrom<&'a str> for UrlCow<'a> {
	type Error = anyhow::Error;

	fn try_from(value: &'a str) -> Result<Self, Self::Error> { Self::try_from(value.as_bytes()) }
}

impl TryFrom<String> for UrlCow<'_> {
	type Error = anyhow::Error;

	fn try_from(value: String) -> Result<Self, Self::Error> {
		Ok(UrlCow::try_from(value.as_str())?.into_owned().into())
	}
}

impl<'a> TryFrom<Cow<'a, str>> for UrlCow<'a> {
	type Error = anyhow::Error;

	fn try_from(value: Cow<'a, str>) -> Result<Self, Self::Error> {
		match value {
			Cow::Borrowed(s) => UrlCow::try_from(s),
			Cow::Owned(s) => UrlCow::try_from(s),
		}
	}
}

impl<'a> TryFrom<(SchemeRef<'a>, PathDyn<'a>)> for UrlCow<'a> {
	type Error = anyhow::Error;

	fn try_from((scheme, path): (SchemeRef<'a>, PathDyn<'a>)) -> Result<Self, Self::Error> {
		(SchemeCow::Borrowed(scheme), path).try_into()
	}
}

impl<'a> TryFrom<(SchemeRef<'a>, PathCow<'a>)> for UrlCow<'a> {
	type Error = anyhow::Error;

	fn try_from((scheme, path): (SchemeRef<'a>, PathCow<'a>)) -> Result<Self, Self::Error> {
		(SchemeCow::Borrowed(scheme), path).try_into()
	}
}

impl TryFrom<(Scheme, PathBufDyn)> for UrlCow<'_> {
	type Error = anyhow::Error;

	fn try_from((scheme, path): (Scheme, PathBufDyn)) -> Result<Self, Self::Error> {
		(SchemeCow::Owned(scheme), path).try_into()
	}
}

impl<'a> TryFrom<(SchemeCow<'a>, PathCow<'a>)> for UrlCow<'a> {
	type Error = anyhow::Error;

	fn try_from((scheme, path): (SchemeCow<'a>, PathCow<'a>)) -> Result<Self, Self::Error> {
		match path {
			PathCow::Borrowed(path) => (scheme, path).try_into(),
			PathCow::Owned(path) => (scheme, path).try_into(),
		}
	}
}

impl<'a> TryFrom<(SchemeCow<'a>, PathDyn<'a>)> for UrlCow<'a> {
	type Error = anyhow::Error;

	fn try_from((scheme, path): (SchemeCow<'a>, PathDyn<'a>)) -> Result<Self, Self::Error> {
		let kind = scheme.as_scheme().kind();
		let (uri, urn) = scheme.as_scheme().ports();
		let domain = scheme.into_domain();
		Ok(match kind {
			SchemeKind::Regular => Self::Regular(Loc::bare(path.as_os()?).into()),
			SchemeKind::Search => Self::Search {
				loc:    Loc::with(path.as_os()?, uri, urn)?.into(),
				domain: domain.ok_or_else(|| anyhow!("missing domain for search scheme"))?,
			},
			SchemeKind::Archive => Self::Archive {
				loc:    Loc::with(path.as_os()?, uri, urn)?.into(),
				domain: domain.ok_or_else(|| anyhow!("missing domain for archive scheme"))?,
			},
			SchemeKind::Sftp => Self::Sftp {
				loc:    Loc::with(path.as_unix()?, uri, urn)?.into(),
				domain: domain.ok_or_else(|| anyhow!("missing domain for sftp scheme"))?,
			},
		})
	}
}

impl<'a> TryFrom<(SchemeCow<'a>, PathBufDyn)> for UrlCow<'a> {
	type Error = anyhow::Error;

	fn try_from((scheme, path): (SchemeCow<'a>, PathBufDyn)) -> Result<Self, Self::Error> {
		let kind = scheme.as_scheme().kind();
		let (uri, urn) = scheme.as_scheme().ports();
		let domain = scheme.into_domain();
		Ok(match kind {
			SchemeKind::Regular => {
				Self::Regular(LocBuf::<std::path::PathBuf>::from(path.into_os()?).into())
			}
			SchemeKind::Search => Self::Search {
				loc:    LocBuf::<std::path::PathBuf>::with(path.try_into()?, uri, urn)?.into(),
				domain: domain.ok_or_else(|| anyhow!("missing domain for search scheme"))?,
			},
			SchemeKind::Archive => Self::Archive {
				loc:    LocBuf::<std::path::PathBuf>::with(path.try_into()?, uri, urn)?.into(),
				domain: domain.ok_or_else(|| anyhow!("missing domain for archive scheme"))?,
			},
			SchemeKind::Sftp => Self::Sftp {
				loc:    LocBuf::<UnixPathBuf>::with(path.try_into()?, uri, urn)?.into(),
				domain: domain.ok_or_else(|| anyhow!("missing domain for sftp scheme"))?,
			},
		})
	}
}

// --- Eq
impl PartialEq for UrlCow<'_> {
	fn eq(&self, other: &Self) -> bool { self.as_url() == other.as_url() }
}

impl PartialEq<UrlBuf> for UrlCow<'_> {
	fn eq(&self, other: &UrlBuf) -> bool { self.as_url() == other.as_url() }
}

impl Eq for UrlCow<'_> {}

// --- Hash
impl Hash for UrlCow<'_> {
	fn hash<H: Hasher>(&self, state: &mut H) { self.as_url().hash(state); }
}

impl<'a> UrlCow<'a> {
	pub fn is_owned(&self) -> bool {
		match self {
			Self::Regular(loc) => loc.is_owned(),
			Self::Search { loc, .. } => loc.is_owned(),
			Self::Archive { loc, .. } => loc.is_owned(),
			Self::Sftp { loc, .. } => loc.is_owned(),
		}
	}

	pub fn into_owned(self) -> UrlBuf {
		match self {
			Self::Regular(loc) => UrlBuf::Regular(loc.into_owned()),
			Self::Search { loc, domain } => {
				UrlBuf::Search { loc: loc.into_owned(), domain: domain.into() }
			}
			Self::Archive { loc, domain } => {
				UrlBuf::Archive { loc: loc.into_owned(), domain: domain.into() }
			}
			Self::Sftp { loc, domain } => {
				UrlBuf::Sftp { loc: loc.into_owned(), domain: domain.into() }
			}
		}
	}

	pub fn into_pair(self) -> (SchemeCow<'a>, PathCow<'a>) {
		let (uri, urn) = self.as_url().scheme().ports();
		match self {
			Self::Regular(loc) => match loc {
				LocCow::Borrowed(_) => (SchemeRef::Regular { uri, urn }.into(), loc.into_path()),
				LocCow::Owned(_) => (Scheme::Regular { uri, urn }.into(), loc.into_path()),
			},
			Self::Search { loc, domain } => match domain {
				SymbolCow::Borrowed(domain) => {
					(SchemeRef::Search { domain, uri, urn }.into(), loc.into_path())
				}
				SymbolCow::Owned(domain) => (Scheme::Search { domain, uri, urn }.into(), loc.into_path()),
			},
			Self::Archive { loc, domain } => match domain {
				SymbolCow::Borrowed(domain) => {
					(SchemeRef::Archive { domain, uri, urn }.into(), loc.into_path())
				}
				SymbolCow::Owned(domain) => (Scheme::Archive { domain, uri, urn }.into(), loc.into_path()),
			},
			Self::Sftp { loc, domain } => match domain {
				SymbolCow::Borrowed(domain) => {
					(SchemeRef::Sftp { domain, uri, urn }.into(), loc.into_path())
				}
				SymbolCow::Owned(domain) => (Scheme::Sftp { domain, uri, urn }.into(), loc.into_path()),
			},
		}
	}

	pub fn into_scheme(self) -> SchemeCow<'a> { self.into_pair().0 }

	pub fn into_path(self) -> PathCow<'a> { self.into_pair().1 }

	pub fn to_owned(&self) -> UrlBuf { self.as_url().into() }
}

impl Serialize for UrlCow<'_> {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		self.as_url().serialize(serializer)
	}
}

impl<'de> Deserialize<'de> for UrlCow<'_> {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: Deserializer<'de>,
	{
		UrlBuf::deserialize(deserializer).map(UrlCow::from)
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::url::UrlLike;

	#[test]
	fn test_parse() -> Result<()> {
		struct Case {
			url:   &'static str,
			urn:   &'static str,
			uri:   &'static str,
			trail: &'static str,
			base:  &'static str,
		}

		let cases = [
			// Regular
			Case {
				url:   "/root/music/rock/song.mp3",
				urn:   "song.mp3",
				uri:   "song.mp3",
				trail: "/root/music/rock/",
				base:  "/root/music/rock/",
			},
			// Search portal
			Case {
				url:   "search://keyword//root/Documents/reports",
				urn:   "",
				uri:   "",
				trail: "search://keyword//root/Documents/reports",
				base:  "search://keyword//root/Documents/reports",
			},
			// Search item
			Case {
				url:   "search://keyword:2:2//root/Documents/reports/2023/summary.docx",
				urn:   "2023/summary.docx",
				uri:   "2023/summary.docx",
				trail: "search://keyword//root/Documents/reports/",
				base:  "search://keyword//root/Documents/reports/",
			},
			// Archive portal
			Case {
				url:   "archive://domain//root/Downloads/images.zip",
				urn:   "",
				uri:   "",
				trail: "archive://domain//root/Downloads/images.zip",
				base:  "archive://domain//root/Downloads/images.zip",
			},
			// Archive item
			Case {
				url:   "archive://domain:2:1//root/Downloads/images.zip/2025/city.jpg",
				urn:   "city.jpg",
				uri:   "2025/city.jpg",
				trail: "archive://domain:1:1//root/Downloads/images.zip/2025/",
				base:  "archive://domain//root/Downloads/images.zip/",
			},
			// SFTP
			Case {
				url:   "sftp://my-server//root/docs/report.pdf",
				urn:   "report.pdf",
				uri:   "report.pdf",
				trail: "sftp://my-server//root/docs/",
				base:  "sftp://my-server//root/docs/",
			},
		];

		for case in cases {
			let url = UrlCow::try_from(case.url)?;
			assert_eq!(url.urn().to_str()?, case.urn);
			assert_eq!(url.uri().to_str()?, case.uri);
			assert_eq!(
				format!("{:?}", url.trail()),
				format!("{:?}", UrlCow::try_from(case.trail)?.as_url())
			);
			assert_eq!(
				format!("{:?}", url.base()),
				format!("{:?}", UrlCow::try_from(case.base)?.as_url())
			);
		}

		Ok(())
	}
}
