use chrono::{Local, NaiveDate};
use leptos::*;
use rust_decimal::Decimal;
use crate::backend::data::ApiResponse;
use crate::components::modal_box::{DialogOpener, ModalBody, ModalDialog, ModalFooter};
use crate::components::validation_err::ValidationErr;
use crate::locales::{loc_date, trl};
use crate::validator::Validator;

#[component]
pub fn res_error(opener: DialogOpener, validator: Validator) -> impl IntoView {
    view! {
        <ModalDialog opener=opener title="Can't create reservation">
            <ModalBody>
                <ValidationErr validator=validator/>
            </ModalBody>
            <ModalFooter>
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"
                    on:click=move |_| {
                        validator.reset();
                        opener.hide();}>
                    {trl("Close")}
                </button>
            </ModalFooter>
        </ModalDialog>
    }
}

#[component]
pub fn res_saved(
    opener: DialogOpener,
    save_result: RwSignal<Option<Result<ApiResponse<NaiveDate>, ServerFnError>>>,
    day: RwSignal<NaiveDate>,
    price: WriteSignal<Decimal>,
    slots: WriteSignal<Vec<String>>,
    captcha_state: ReadSignal<Option<bool>>) -> impl IntoView {
    view! {{move ||{
        if let Some(r) = save_result.get() {
            match r {
                Ok(ar) => {
                    match ar {
                        ApiResponse::Data(d) => {
                            view! {
                                <div style={ move || if let Some(c) = captcha_state.get() {
                                        if c {"display: none;"} else {"display: block;"}
                                    } else {"display: block;"}}>
                                <ModalDialog opener=opener title="Reservation saved">
                                    <ModalBody>
                                        <p>
                                        {trl("Your reservation has been successfully saved.")}
                                        </p>
                                        <p>
                                        {trl("We look forward to seeing you on")}" "{loc_date(d)}
                                        </p>
                                    </ModalBody>
                                    <ModalFooter>
                                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"
                                            on:click=move |_| {
                                                opener.hide();
                                                if day.get() == Local::now().date_naive() {
                                                    day.set(NaiveDate::parse_from_str("2024-01-01", "%Y-%m-%d").unwrap());
                                                }
                                                slots.set(vec![]);
                                                price.set(Decimal::default());
                                                day.set(Local::now().date_naive());}>
                                                {trl("Ok")}
                                        </button>
                                    </ModalFooter>
                                </ModalDialog>
                                </div>
                            }
                        },
                        ApiResponse::Error(err) => {
                            view! {
                                <div>
                                <ModalDialog opener=opener title="Reservation not saved">
                                    <ModalBody>
                                        <div class="alert alert-danger">
                                            {trl("Reservation cannot be saved.")}<br/>{trl(&err)}
                                        </div>
                                    </ModalBody>
                                    <ModalFooter>
                                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"
                                            on:click=move |_| {
                                                opener.hide();}>
                                                {trl("Close")}
                                        </button>
                                    </ModalFooter>
                                </ModalDialog>
                                </div>
                            }
                        }
                    }
                }
                Err(err) => {
                    view! {
                        <div>
                        <ModalDialog opener=opener title="Save error">
                            <ModalBody>
                                <div class="alert alert-danger">
                                    {trl("Error while saving reservation.")}<br/>{trl(&err.to_string())}
                                </div>
                            </ModalBody>
                            <ModalFooter>
                                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"
                                    on:click=move |_| {
                                        opener.hide();}>
                                        {trl("Close")}
                                </button>
                            </ModalFooter>
                        </ModalDialog>
                        </div>
                    }
                }
            }
        } else {
            view! {<div></div>}
        }
    }}
    }
}
