use leptos::*;
use magister_engine::card::Subtype;

#[component]
fn SubtypeTest() -> impl IntoView {
    let (subtype_text, set_subtype_text) = create_signal(String::new());
    let subtype = move || Subtype::new(subtype_text.get());
    let (query_text, set_query_text) = create_signal(String::new());

    let subtype_display = move || {
        let subtype = subtype();
        let query = query_text.get();

        let is_member = query.split_whitespace().all(|qp| subtype.is_member(qp));
        let is_quasimember = query
            .split_whitespace()
            .all(|qp| subtype.is_quasimember(qp));

        view! {
            <div class="flex w-full justify-between">
                <p class=("text-primary", is_member) class=("text-error", !is_member)>
                    "Member"
                </p>
                <p class=("text-primary", is_quasimember) class=("text-error", !is_quasimember)>
                    "Quasimember"
                </p>
            </div>
        }
    };

    view! {
        <div class="card card-bordered max-w-md">
            <div class="card-body">
                <p>"Subtype: " {move || format!("{}", subtype())}</p>
                {subtype_display}
                <div class="card-actions">
                    <input
                        type="text"
                        placeholder="Subtype Text"
                        class="input input-bordered w-full"
                        prop:value=move || subtype_text.get()
                        on:input=move |ev| { set_subtype_text.set(event_target_value(&ev)) }
                    />
                    <input
                        type="text"
                        placeholder="Subtype Query"
                        class="input input-bordered w-full"
                        prop:value=move || query_text.get()
                        on:input=move |ev| { set_query_text.set(event_target_value(&ev)) }
                    />
                </div>
            </div>
        </div>
    }
}

#[component]
pub fn App() -> impl IntoView {
    view! {
        <h1 class="text-6xl font-bold">"Magister Studio"</h1>
        <SubtypeTest/>
    }
}