use crate::{
    language::language,
    page::{MyMessage, Page},
};
use axum::{extract::Query, http::HeaderMap};
use axum_extra::extract::CookieJar;
use maud::{html, Markup, PreEscaped};
use serde::Deserialize;

pub(super) async fn index(cookies: CookieJar, headers: HeaderMap) -> Markup {
    let lang = language(&cookies, &headers);
    rust_i18n::set_locale(lang.to_locale());

    let page = Page::new(lang);
    page.content(html! {
        h1 { (t!("digital_shadows")) }
        hgroup {
            h2 { (PreEscaped(t!("who_owns_data"))) }
            p { (t!("digital_shadow_description")) }
        }
        p {
            (t!("artists_list"))
            span.easteregg { (t!("artists_easter_egg")) }
        }
        blockquote {
            (t!("project_quote"))
            footer {
                cite { (t!("project_quote_attribution")) }
            }
        }
        p { (t!("project_description")) }

        h2 { (t!("what_to_do_title")) }

        div.grid.gap-lg.grid-cols-2 {
            article.col-span-2 {
                header { (t!("tour_aef_title")) }

                (PreEscaped(t!("tour_aef_description")))

                a
                    href="https://www.jku.at/ars-electronica-2025-panic-yes-no/digital-shadows/"
                    target="_blank"
                    class="btn mt-1 block"
                    role="button"
                    title=(t!("tour_register_title")) { (t!("tour_register")) }

                footer { (t!("location_postcity")) }
            }
            article {
                header { (t!("play_game_title")) }

                (t!("play_game_description"))

                a href="/game" title=(t!("game_link_title")) { (t!("find_out_more")) }

                footer { (t!("location_linz")) }
            }
            article {
                header { (t!("visit_booth_title")) }

                (t!("visit_booth_description"))

                a
                    href="https://www.jku.at/ars-electronica-2025-panic-yes-no/digital-shadows/"
                    target="_blank"
                    title=(t!("jku_link_title")) { (t!("find_out_more")) }

                footer { (t!("location_postcity")) }
            }
        }
    })
}

pub(super) async fn cam(cookies: CookieJar, headers: HeaderMap) -> Markup {
    let lang = language(&cookies, &headers);
    rust_i18n::set_locale(lang.to_locale());

    let page = Page::new(lang);
    page.content(html! {
    hgroup {
    h1 { (PreEscaped(t!("cam_title"))) }
    p { (t!("cam_title2")) }
    }
    hgroup {
        h2 { (t!("cam_subtitle")) }
        p { (t!("cam_description")) }
    }
    p {
        (t!("cam_project_by"))
        span.highlight { (t!("cam_institute")) }
    }
    blockquote {
        (t!("cam_mission_quote"))
        footer {
            cite { (t!("cam_mission_attribution")) }
        }
    }
    p { (t!("cam_project_description")) }

    h2 { (t!("cam_how_it_works")) }

    div.grid.gap-lg {
        article {
            header { (t!("cam_tech_setup_title")) }

            p { (t!("cam_tech_setup_p1")) }

            p { (t!("cam_tech_setup_p2")) }
        }
        article {
            header { (PreEscaped(t!("cam_data_processing_title"))) }

            p { (PreEscaped(t!("cam_data_processing_p1"))) }
        }
    }

    h2 { (t!("cam_festival_details")) }

    div.info-box {
        h3 { (t!("cam_when_where_title")) }
        p { 
            (t!("cam_festival_info")) br;
            (t!("cam_festival_dates")) br;
            (t!("cam_festival_location"))
        }
    }

    h2 { (t!("cam_legal_compliance")) }

    p { (t!("cam_legal_description")) }

    div.legal-docs {
        a href="/static/dsb-request.pdf" target="_blank" title=(t!("cam_legal_request_title")) { (t!("cam_legal_request")) }
        " | "
        a href="/static/dsb-accept.pdf" target="_blank" title=(t!("cam_legal_decision_title")) { (t!("cam_legal_decision")) }
    }
})

}

#[derive(Deserialize)]
pub(super) struct PrivacyQuery {
    deleted: Option<u8>,
}

pub(super) async fn data(
    cookies: CookieJar,
    headers: HeaderMap,
    Query(query): Query<PrivacyQuery>,
) -> Markup {
    let lang = language(&cookies, &headers);
    rust_i18n::set_locale(lang.to_locale());

    let mut page = Page::new(lang);

    // Show success message if data was deleted
    if query.deleted == Some(1) {
        page.set_message(MyMessage::DataDeleted);
    }
    page.content(html! {
        h1 { (PreEscaped(t!("privacy_policy_title"))) }
        h2 { (t!("data_controller")) }
        p {
            (PreEscaped(t!("data_controller_info")))
        }
        h2 { (t!("overview")) }
        p {
            (PreEscaped(t!("privacy_overview")))
        }
        h2 { (t!("data_we_collect")) }
        h3 { (t!("cookies")) }
        p {
            (t!("cookies_description"))
            ol {
                li {
                    kbd { "client_id" }
                    " "
                    (t!("cookie_client_id"))
                }
                li {
                    kbd { "lang" }
                    " "
                    (t!("cookie_lang"))
                }
            }
        }
        h3 { (t!("game_data")) }
        p {
            (t!("game_data_description"))
            ul {
                li { (t!("chosen_name")) }
                li { (t!("game_progress")) }
                li {
                    (PreEscaped(t!("random_client_id")))
                }
            }

        }
        h2 { (t!("purpose_legal_basis")) }
        ul {
            li { (t!("game_functionality")) }
            li { (t!("language_preference")) }
            li { (t!("statistical_analysis")) }
        }
        h2 { (t!("data_retention")) }
        p {
            (t!("data_retention_description"))
        }
        h2 { (t!("data_sharing")) }
        p {
            (t!("data_sharing_description"))
        }
        h2 { (t!("server_logfiles")) }
        p {
            (t!("server_logfiles_description"))
        }
        h2 { (t!("data_security")) }
        p {
            (t!("data_security_description"))
        }
        h2 { (t!("minors")) }
        p {
            (t!("minors_description"))
        }
        h2 { (t!("data_protection_officer")) }
        p {
            (PreEscaped(t!("data_protection_officer_contact_full")))
        }
        h2 { (t!("data_collection_timing")) }
        p {
            (t!("data_collection_timing_description"))
        }
        h2 { (t!("your_rights_gdpr")) }
        p {
            (t!("rights_description"))
            ul {
                li { (t!("right_access")) }
                li { (t!("right_rectification")) }
                li { (t!("right_erasure")) }
                li { (t!("right_restriction")) }
                li { (t!("right_portability")) }
            }
        }
        h3 { (t!("how_to_exercise_rights")) }
        ul {
            li { (t!("clear_cookies")) }
            li {
                (PreEscaped(t!("contact_us")))
            }
        }
        h3 { (t!("delete_personal_data")) }
        p {
            (t!("delete_data_description"))
        }
        form method="POST" action="/delete-data" onsubmit={"return confirm('" (t!("delete_confirmation")) "');"} {
            button type="submit" class="secondary" {
                (t!("delete_my_data"))
            }
        }
    })
}
