use http::StatusCode;
use kittybox_util::micropub::Channel;
use crate::{Feed, VCard};

markup::define! {
    Template<'a>(title: &'a str, blog_name: &'a str, feeds: Vec<Channel>, user: Option<&'a kittybox_indieauth::ProfileUrl>, content: String) {
        @markup::doctype()
        html {
            head {
                title { @title }
                link[rel="preconnect", href="https://fonts.gstatic.com"];
                link[rel="stylesheet", href="/.kittybox/static/style.css"];
                meta[name="viewport", content="initial-scale=1, width=device-width"];

                link[rel="micropub", href="/.kittybox/micropub"];
                link[rel="micropub_media", href="/.kittybox/media"];
                link[rel="indieauth-metadata", href="/.kittybox/indieauth/metadata"];
                link[rel="webmention", href="/.kittybox/webmention"];
                // legacy links for some dumb clients
                link[rel="authorization_endpoint", href="/.kittybox/indieauth/auth"];
                link[rel="token_endpoint", href="/.kittybox/indieauth/token"];
                // LibreJS-compliant JS licensing info (because TypeScript is a bitch)
                link[rel="jslicense", href="/.kittybox/static/jslicense.html"];
                /*@if let Some(endpoints) = endpoints {
                    @if let Some(webmention) = &endpoints.webmention {
                        link[rel="webmention", href=&webmention];
                    }
                    @if let Some(microsub) = &endpoints.microsub {
                        link[rel="microsub", href=&microsub];
                    }
                }*/
            }
            body {
                a[href="#main_content", id="skip-to-content"] { "Skip to content" }
                // TODO Somehow compress headerbar into a menu when the screen space is tight
                nav #headerbar {
                    ul {
                        li { a #homepage[href="/"] { @blog_name } }
                        @for feed in feeds.iter() {
                            li { a[href=&feed.uid] { @feed.name } }
                        }
                        li.shiftright {
                            @if let Some(user) = &user {
                                span {
                                    @if let Some(kittybox_indieauth::Profile { name: Some(name), photo, .. }) = &user.profile {
                                        a[href=user.me.as_str()] { @name }
                                    } else {
                                        @user.me.as_str()
                                    }
                                    " - "
                                    a #logout[href="/.kittybox/login/logout"] { "Sign out" }
                                }
                            } else {
                                a #login[href="/.kittybox/login/start"] { "Sign in" }
                            }
                        }
                    }
                }
                main #main_content {
                    @markup::raw(content)
                }
                footer {
                    p {
                        "Powered by " a[href="https://sr.ht/~vikanezrimaya/kittybox"] {
                            "Kittybox"
                        }
                    }
                }
            }
        }
    }
    MainPage<'a>(feed: &'a serde_json::Value, card: &'a serde_json::Value, cursor: Option<&'a str>, webring: bool) {
        .sidebyside {
            @VCard { card }
            #dynamicstuff {
                div {
                    p { "This section will provide interesting statistics or tidbits about my life in this exact moment (with maybe a small delay)." }
                    p { "It will probably require JavaScript to self-update, but I promise to keep this widget lightweight and open-source!" }
                    p { small {
                        "JavaScript isn't a menace, stop fearing it or I will switch to WebAssembly "
                        "and knock your nico-nico-kneecaps so fast with its speed you won't even notice that... "
                        small { "omae ha mou shindeiru" }
                        @markup::raw("<!-- NANI?!!! -->")
                    } }
                }
                @if *webring {
                    #webring {
                        a[href="https://xn--sr8hvo.ws/previous"] { "←" }
                        " An "
                        a[href="https://xn--sr8hvo.ws"] {
                            "IndieWeb Webring"
                        }
                        " πŸ•ΈπŸ’ "
                        a[href="https://xn--sr8hvo.ws/next"] { "β†’" }
                    }
                }
            }
        }
        @Feed { feed, cursor: *cursor }
    }
    ErrorPage(code: StatusCode, msg: Option<String>) {
        h1 { @format!("HTTP {code}") }
        @match *code {
            StatusCode::UNAUTHORIZED => {
                p { "Looks like you need to authenticate yourself before seeing this page. Try logging in with IndieAuth using the Login button above!" }
            }
            StatusCode::FORBIDDEN => {
                p { "Looks like you're forbidden from viewing this page." }
                p {
                    "This might've been caused by being banned from viewing my website"
                    "or simply by trying to see what you're not supposed to see, "
                    "like a private post that's not intended for you. It's ok, it happens."
                }
            }
            StatusCode::GONE => {
                p { "Looks like the page you're trying to find is gone and is never coming back." }
            }
            StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS => {
            p { "The page is there, but I can't legally provide it to you because the censorship said so." }
            }
            StatusCode::NOT_FOUND => {
                p { "Looks like there's no such page. Maybe you or someone else mistyped a URL or my database experienced data loss." }
            }
            StatusCode::IM_A_TEAPOT => {
                p { "Wait, do you seriously expect my website to brew you coffee? It's not a coffee machine!" }

                p {
                    small {
                        "I could brew you some coffee tho if we meet one day... "
                        small {
                            i {
                                "i-it's nothing personal, I just like brewing coffee, b-baka!!!~ >.<!"
                            }
                        }
                    }
                }
            }
            StatusCode::BAD_REQUEST => {
                @match msg {
                    None => {
                        p {
                            "There was an undescribed error in your request. "
                            "Please try again later or with a different request."
                        }
                        p { small {
                            "Also pester "
                            a[href="https://fireburn.ru/"] { "Vika" }
                            " for better error messages."
                        } }
                    }
                    Some(msg) => {
                        p {
                            "There was a following error in your request:"
                        }
                        blockquote { pre { @msg } }
                    }
                }
            }
            StatusCode::INTERNAL_SERVER_ERROR => {
                @match msg {
                    None => {
                        p { "It seems like you have found an error. Not to worry, it has already been logged." }
                    }
                    Some(msg) => {
                        p { "The server encountered an error while processing your request:" }
                        blockquote { @msg }
                        p { "Don't worry, it has already been logged." }
                    }
                }
            }
            _ => {
                p { "It seems like you have found an error. Not to worry, it has already been logged." }
            }
        }
        P { "For now, may I suggest to visit " a[href="/"] {"the main page"} " of this website?" }

    }
}