use assetserver::Asset;
use maud::{DOCTYPE, Markup, PreEscaped, Render, html};

use crate::{components::misc::FarLink, routes::assets::Styles_Main};

pub struct PageMetadata {
	pub title: String,
	pub author: Option<String>,
	pub description: Option<String>,
	pub image: Option<String>,
}

impl Render for PageMetadata {
	fn render(&self) -> Markup {
		let empty = String::new();
		let title = &self.title;
		let author = &self.author.as_ref().unwrap_or(&empty);
		let description = &self.description.as_ref().unwrap_or(&empty);
		let image = &self.image.as_ref().unwrap_or(&empty);

		html!(
			meta property="og:site_name" content=(title) {}
			meta name="title" content=(title) {}
			meta property="og:title" content=(title) {}
			meta property="twitter:title" content=(title) {}

			meta name="author" content=(author) {}

			meta name="description" content=(description) {}
			meta property="og:description" content=(description) {}
			meta property="twitter:description" content=(description) {}

			meta content=(image) property="og:image" {}
			link rel="shortcut icon" href=(image) type="image/x-icon" {}
		)
	}
}

pub struct BasePage<T: Render>(pub PageMetadata, pub T);

impl<T: Render> Render for BasePage<T> {
	fn render(&self) -> Markup {
		let meta = &self.0;

		html! {
			(DOCTYPE)
			html {
				head {
					meta charset="UTF" {}
					meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" {}
					meta content="text/html; charset=UTF-8" http-equiv="content-type" {}
					meta property="og:type" content="website" {}

					link rel="stylesheet" href=(Styles_Main::URL) {}


					(meta)
					title { (PreEscaped(meta.title.clone())) }
				}

				body {
					div class="wrapper" {
						main { (self.1) }

						footer {
							hr class = "footline" {}
							div class = "footContainer" {
								p {
									"This site was built by hand using "
									(FarLink("https://rust-lang.org", "Rust"))
									", "
									(FarLink("https://maud.lambda.xyz", "Maud"))
									", "
									(FarLink("https://github.com/connorskees/grass", "Grass"))
									", and "
									(FarLink("https://docs.rs/axum/latest/axum", "Axum"))
									"."
								}
							}
						}
					}
				}
			}
		}
	}
}
