use crate::components::icons;
use crate::models;
use leptos::prelude::*;

#[component]
#[tracing::instrument(skip_all)]
pub fn Attempt(#[prop(into)] date: Signal<chrono::DateTime<chrono::Utc>>, #[prop(into)] attempt: Signal<Option<models::Attempt>>) -> impl IntoView {
    tracing::trace!("Enter");

    let s = time_ago(date.get());

    let text = move || match attempt.get() {
        Some(models::Attempt::Flash) => "Flash",
        Some(models::Attempt::Send) => "Send",
        Some(models::Attempt::Attempt) => "Learning experience",
        None => "No attempt",
    };

    let text_color = match attempt.get() {
        Some(models::Attempt::Flash) => "text-cyan-500",
        Some(models::Attempt::Send) => "text-teal-500",
        Some(models::Attempt::Attempt) => "text-pink-500",
        None => "",
    };

    let icon = move || match attempt.get() {
        Some(models::Attempt::Flash) => view! { <icons::BoltSolid /> }.into_any(),
        Some(models::Attempt::Send) => view! { <icons::Trophy /> }.into_any(),
        Some(models::Attempt::Attempt) => view! { <icons::ArrowTrendingUp /> }.into_any(),
        None => view! { <icons::NoSymbol /> }.into_any(),
    };

    let classes = format!("flex flex-row gap-3 {}", text_color);

    view! {
        <div class="flex flex-row justify-between my-2">
            <div>{s}</div>

            <div class=classes>
                <span>{text}</span>
                <span>{icon}</span>
            </div>
        </div>
    }
}

fn time_ago(dt: chrono::DateTime<chrono::Utc>) -> String {
    let now = chrono::Utc::now();
    let duration = now.signed_duration_since(dt);

    if duration.num_days() == 0 {
        "Today".to_string()
    } else if duration.num_days() == 1 {
        "1 day ago".to_string()
    } else {
        format!("{} days ago", duration.num_days())
    }
}
