//! # Rust Letter Frontend
//!
//! Rendering functions for the site using [Dioxus](https://dioxuslabs.com/).

#![allow(non_snake_case)]

/// A module that handles the functions needed
/// to render the site.
pub mod void_app {
    use std::borrow::Borrow;

    // import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types
    pub use dioxus::prelude::*;
    use rust_embed::RustEmbed;

    // #[derive(PartialEq, Props)]
    // pub struct Poems {
    //     pub poems: Vec<String>,
    // }

    #[derive(PartialEq, Props)]
    struct PoemContent {
        content: String,
    }
    #[derive(RustEmbed)]
    #[folder = "data/poems"]
    pub struct Poems;

    /// Renders the app and returns the rendered Element.
    pub fn App(cx: Scope) -> Element {
        cx.render(rsx!(
            div { class: "min-h-screen font-nerd bg-alice-werefox-grey-light dark:bg-alice-werefox-grey",
                div { class: "container space-y-4 mx-auto p-4",
                    Poems::iter().map(|p| {
                                let poem_content = Poems::get(&p).expect("Found poem {&p:?}");
                                let poem_to_str = std::str::from_utf8(poem_content.data.as_ref()).expect("Content is valid UT8.");
                                let poem_to_html_string = markdown::to_html(poem_to_str);
                                rsx!{ MakePoem{ content: poem_to_html_string  } }
                            })
                }
            }
        ))
    }

    #[cfg(any(target_family = "unix", target_family = "windows"))]
    fn RenderPoemElement(cx: Scope<PoemContent>) -> Element {
        cx.render(rsx!(
            div { class: "font-nerd flex flex-col space-y-4 mx-4 py-4", "{cx.props.content}" }
        ))
    }

    #[cfg(target_family = "wasm")]
    fn RenderPoemElement(cx: Scope<PoemContent>) -> Element {
        cx.render(rsx!(div {
            class: "font-nerd flex flex-col space-y-4 mx-4 py-4",
            dangerous_inner_html: "{cx.props.content}"
        }))
    }

    fn MakePoem(cx: Scope<PoemContent>) -> Element {
        cx.render(rsx!(
            div { class: "flex-col space-y-4",
                p { class: "mx-auto max-w-fit flex justify-center bg-alice-werefox-grey-lightest dark:bg-alice-werefox-grey-dark border-4 border-alice-werefox-red-dark dark:border-alice-werefox-red text-alice-werefox-red-dark dark:text-alice-werefox-red-light p-4",
                    "POEM NAME HERE"
                }
                details { class: "mx-auto max-w-fit space-y-4 bg-alice-werefox-grey-lightest dark:bg-alice-werefox-grey-dark border-4 border-alice-werefox-red-dark dark:border-alice-werefox-red text-alice-werefox-red-dark dark:text-alice-werefox-red-light p-4",
                    summary { class: "flex justify-center border-4 bg-alice-werefox-grey-lightest dark:bg-alice-werefox-grey-dark border-alice-werefox-red-dark dark:border-alice-werefox-red text-alice-werefox-red-dark dark:text-alice-werefox-red-light p-4",
                        "Open"
                    }
                    RenderPoemElement { content: cx.props.content.clone() }
                }
            }
        ))
    }
}
