package de.votian.web.view;

import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.H3;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;
import de.votian.web.model.SessionData;
import de.votian.web.service.ParameterProvider;
import de.votian.web.service.RestClientService;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;

@Route(value = "dashboard", layout = MainLayout.class)
@RouteAlias(value = "", layout = MainLayout.class)
@PageTitle("Votian - Dashboard")
public class DashboardView extends VerticalLayout {

    public DashboardView(RestClientService restClient, SessionData sessionData, ParameterProvider parameterProvider) {
        setPadding(true);
        setSpacing(true);

        H2 welcome = new H2("Willkommen, " + sessionData.getDisplayName());
        Paragraph dateInfo = new Paragraph(LocalDate.now().format(
                DateTimeFormatter.ofPattern("EEEE, dd. MMMM yyyy", java.util.Locale.GERMAN)));

        // Info cards
        HorizontalLayout cards = new HorizontalLayout();
        cards.setWidthFull();
        cards.setSpacing(true);

        // Load HQ info
        String hqName = "Niederlassung";
        if (sessionData.getHeadquartersId() != null) {
            try {
                @SuppressWarnings("unchecked")
                Map<String, Object> hq = restClient.get("/headquarters/" + sessionData.getHeadquartersId(),
                        LinkedHashMap.class);
                if (hq != null) {
                    hqName = (String) hq.getOrDefault("name", "Niederlassung");
                }
            } catch (Exception ignored) {}
        }

        cards.add(createInfoCard("Niederlassung", hqName));

        String userTypeLabel;
        switch (sessionData.getUserType() != null ? sessionData.getUserType() : 0) {
            case 1: userTypeLabel = "Mitarbeiter (HQ)"; break;
            case 2: userTypeLabel = "Kundenbenutzer"; break;
            case 3: userTypeLabel = "Kurier"; break;
            case 4: userTypeLabel = "Lager"; break;
            default: userTypeLabel = "Unbekannt";
        }
        cards.add(createInfoCard("Benutzertyp", userTypeLabel));

        // Today's jobs count
        try {
            Object[] todayJobs = restClient.get("/jobs/today", Object[].class);
            cards.add(createInfoCard("Heutige Auftraege", String.valueOf(todayJobs != null ? todayJobs.length : 0)));
        } catch (Exception e) {
            cards.add(createInfoCard("Heutige Auftraege", "N/A"));
        }

        // Parameter info
        if (parameterProvider.isLoaded()) {
            cards.add(createInfoCard("Parameter", String.valueOf(parameterProvider.size())));
        }

        add(welcome, dateInfo, cards);
    }

    private VerticalLayout createInfoCard(String title, String value) {
        VerticalLayout card = new VerticalLayout();
        card.setPadding(true);
        card.setSpacing(false);
        card.getStyle()
                .set("border", "1px solid var(--lumo-contrast-20pct)")
                .set("border-radius", "var(--lumo-border-radius-l)")
                .set("background", "var(--lumo-base-color)")
                .set("box-shadow", "var(--lumo-box-shadow-s)")
                .set("min-width", "200px");

        H3 cardTitle = new H3(title);
        cardTitle.getStyle().set("margin", "0").set("font-size", "var(--lumo-font-size-s)")
                .set("color", "var(--lumo-secondary-text-color)");

        Span cardValue = new Span(value);
        cardValue.getStyle().set("font-size", "var(--lumo-font-size-xl)")
                .set("font-weight", "bold").set("color", "#1b12b9");

        card.add(cardTitle, cardValue);
        return card;
    }
}
