use chrono::Weekday;
use leptos::*;
use crate::backend::data::{DayHours, WeekHours};
use crate::backend::opening_hours::get_hours;
use crate::components::modal_box::DialogOpener;
use crate::locales::{show_day, trl};
use crate::pages::hours_edit::EditHours;

fn show_time(tm: &str) -> impl Fn() -> String {
    if tm.is_empty() {
        trl("Closed")
    } else {
        trl(tm)
    }
}

#[component]
pub fn OpeningHours() -> impl IntoView {
    let editor = DialogOpener::new();
    let hours = create_blocking_resource(move || editor.visible(), move |_| {get_hours()});
    let hrs = create_rw_signal(WeekHours::default());

    view! {
        <EditHours opener=editor hours=hrs.read_only() />
        <div class="card mb-3">
            <div class="card-body">
                <h5 class="card-title"><i class="bx bxs-watch"></i>" "{trl("Opening hours")}</h5>
                <p class="card-text">
                    <Transition fallback=move || view! {<p>{trl("Loading...")}</p> }>
                        {move || {
                            hours.get().map(|h| match h {
                                Ok(h) => {
                                    let h = create_rw_signal(h);
                                    let d = create_rw_signal((0..7).collect::<Vec<u8>>());
                                    view! {
                                        <div>
                                        <table class="table card-table">
                                            <For each=move || d.get() key=|day| *day
                                                children=move |day| {
                                                    let week_day = Weekday::try_from(day).unwrap_or(Weekday::Mon);
                                                    let hr_c = h.get().get(&week_day).unwrap_or(&Vec::new()).clone();
                                                    let hr_day = hr_c.clone();
                                                    view! {
                                                    <tr>
                                                        <td>{show_day(&week_day)}</td>
                                                        <td>{show_time(&DayHours(hr_day).to_string())}</td>
                                                        <td><a href="javascript:void(0)" class="card-link" on:click = move |_| {
                                                            hrs.set(WeekHours::new(week_day, hr_c.clone()));
                                                            editor.show();
                                                         }><i class="bx bx-edit-alt me-1"></i></a></td>
                                                    </tr>
                                                    }
                                                }
                                            />
                                        </table>
                                        </div>
                                    }
                                }
                                Err(e) => {view! {<div><p>{trl("Error loading data")}</p>
                                    <p>{e.to_string()}</p></div>
                                }}
                            })
                        }}
                    </Transition>
                </p>
            </div>
        </div>
    }
}