use actix_web::{get, HttpRequest, Responder};
use maud::html;

use super::components::build_site;

#[get("/watch")]
pub async fn video_page(r: HttpRequest) -> impl Responder {
    let library: &actix_web::web::Data<crate::library::Library> = r.app_data().unwrap();
    let query = web_base::parse_query_string(r.query_string());
    if let Some(video_id) = query.get("v") {
        let (channel, mut video) =
            if let Some((channel, video)) = library.get_video_by_hash(video_id) {
                (channel, video)
            } else {
                library.get_video_by_youtube_id(video_id).unwrap()
            };

        let uploader_id = video.uploader_id().map(std::borrow::ToOwned::to_owned);

        let content = html! {
            div style="position: relative; padding-bottom: 56.25%; overflow: hidden; max-width: 100%;" {
            video style="margin-bottom: 50px;position: absolute; top: 0; left: 0; width: 100%; height: 100%;" height="420" controls autoplay src=(format!("/video/raw?v={video_id}")) {
                source src=(format!("/video/raw?v={video_id}")) type="video/x-matroska";
            };
        };
            h2 style="margin-bottom: 10px;margin-top: 10px;" { ( &video.title().unwrap() ) }
            hr;
            p { "In: " a href=(format!("/d/{channel}")) { (channel)};};
            @if video.youtube_id().is_some() {
            article class="container-fluid" id="youtube" {
                @if let Some(id) = &video.youtube_id() {
                    a href=(format!("https://youtube.com/watch?v={id}")) { "Watch on YouTube" };
                };
                p {
                    "Uploaded"
                @if let Some(uploader) = &video.uploader_name() {
                     " by " a href=(format!("/yt/c/{}", uploader_id.unwrap_or_default())) style="font-weight: bold;" { (uploader) };
                }
                @if let Some(upload_date) = &video.upload_date() {
                    " on " span style="font-weight: bold;" { (
                        chrono::NaiveDate::parse_from_str(upload_date, "%Y%m%d").unwrap().format("%d.%m.%Y")
                    ) };
                }
            };
                @if let Some(views) = video.views() {
                    p { "Views: " (views) };
                };
                @if let Some(cats) = video.categories() {
                    @if !cats.is_empty() {
                    p {
                        "Categories: "
                    @for cat in cats {
                        code { (cat) };
                    };
                };
                };
                };
                @if let Some(tags) = video.tags() {
                    @if !tags.is_empty() {
                    p {
                        "Tags: "
                        @for tag in tags {
                            a href=(format!("/yt/tag/{tag}")) { code style="margin: 2px;" { (tag) }; }; " "
                        };
                    };
                };
                };
                @if let Some(desc) = video.description() {
                    details open {
                        summary { b { ("Description:" )}};
                        @for line in desc.lines() {
                            (line);
                            br;
                        };
                    };
            };
            };
        };
        }
        .into_string();
        return build_site(&r, video.title().unwrap(), &content);
    }
    build_site(&r, "Video not found", "<p> Video not found </p>")
}
