#![allow(non_snake_case)]

use std::str::FromStr;

use dioxus::prelude::*;
use dioxus_sdk::i18n::*;

use tracing::Level;

pub mod components;
mod consulting;
mod cv;
mod home;
mod impressum;
mod languages;
mod layout;
mod publications;

use crate::consulting::Consulting;
use crate::cv::CV;
use crate::home::Home;
use crate::impressum::Impressum;
use crate::languages::DE_DE;
use crate::languages::EN_GB;
use crate::layout::Layout;
use crate::publications::PublicationsProjects;

#[derive(Clone, Routable, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Route {
    #[layout(Layout)]
    #[route("/")]
    Home {},
    #[route("/impressum")]
    Impressum {},
    #[route("/publications")]
    PublicationsProjects {},
    #[route("/resume")]
    CV {},
    #[route("/consulting")]
    Consulting {},
    #[end_layout]
    #[route("/:..route")]
    PageNotFound { route: Vec<String> },
}

fn main() {
    dioxus_logger::init(Level::DEBUG).expect("failed to init logger");
    LaunchBuilder::fullstack()
        .with_cfg(server_only!(dioxus::fullstack::Config::new().addr(
            std::net::SocketAddrV4::new(std::net::Ipv4Addr::new(0, 0, 0, 0), 8080,)
        )))
        .launch(App)
}

fn App() -> Element {
    use_init_i18n("en-GB".parse().unwrap(), "en-GB".parse().unwrap(), || {
        let en_gb = Language::from_str(EN_GB).unwrap();
        let de_de = Language::from_str(DE_DE).unwrap();
        vec![en_gb, de_de]
    });

    rsx! {
        meta {
            name: "robots",
            content: "noindex",
        },
        div {
            class: "bg-white dark:bg-gray-900 min-h-screen",
            Router::<Route> {},
        }
    }
}

#[component]
fn PageNotFound(route: Vec<String>) -> Element {
    rsx! {
        div {
            class: "h-screen items-center justify-center",
            img {
                class: "size-auto",
                src: "https://raw.githubusercontent.com/SAWARATSUKI/ServiceLogos/main/404Notfound/NotFound.png"
            }
        }
    }
}

#[derive(PartialEq, Props, Clone)]
pub struct BodyProp {
    children: Element,
}

pub fn Body(prop: BodyProp) -> Element {
    rsx! {
        div {
            class: "flex-grow container mx-auto p-4",
            {prop.children}
        }
    }
}
