package de.votian.web.view;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
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 de.votian.web.model.SessionData;
import de.votian.web.service.RestClientService;

import java.util.*;

@Route(value = "employees", layout = MainLayout.class)
@PageTitle("Votian - Mitarbeiter")
public class EmployeeListView extends VerticalLayout {

    private final RestClientService restClient;
    private final Grid<Map<String, Object>> grid;

    public EmployeeListView(RestClientService restClient, SessionData sessionData) {
        this.restClient = restClient;
        setSizeFull();
        setPadding(true);

        Button newButton = new Button("Neuer Mitarbeiter", VaadinIcon.PLUS.create(), e ->
                getUI().ifPresent(ui -> ui.navigate(EmployeeDetailView.class)));
        newButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
        newButton.setVisible(sessionData.canManageEmployees());

        HorizontalLayout toolbar = new HorizontalLayout(newButton);
        toolbar.setWidthFull();

        grid = new Grid<>();
        grid.addColumn(m -> m.get("id")).setHeader("ID").setAutoWidth(true);
        grid.addColumn(m -> m.get("userName")).setHeader("Name").setFlexGrow(1);
        grid.addColumn(m -> m.get("userFirstname")).setHeader("Vorname").setFlexGrow(1);
        grid.addColumn(m -> m.get("userEmail")).setHeader("E-Mail").setFlexGrow(1);
        grid.addColumn(m -> userTypeLabel(m.get("userType"))).setHeader("Art").setAutoWidth(true);
        grid.addColumn(m -> m.get("headquarters")).setHeader("Niederlassung").setAutoWidth(true);
        grid.setSizeFull();

        grid.addItemClickListener(e -> {
            Object id = e.getItem().get("id");
            if (id != null) {
                getUI().ifPresent(ui -> ui.navigate("employees/" + id));
            }
        });

        add(toolbar, grid);
        loadEmployees();
    }

    private void loadEmployees() {
        try {
            Object[] employees = restClient.get("/employees", Object[].class);
            if (employees != null) {
                List<Map<String, Object>> items = new ArrayList<>();
                for (Object e : employees) {
                    if (e instanceof Map) {
                        @SuppressWarnings("unchecked")
                        Map<String, Object> map = (Map<String, Object>) e;
                        items.add(map);
                    }
                }
                grid.setItems(items);
            }
        } catch (Exception e) {
            Notification.show("Fehler: " + e.getMessage());
        }
    }

    private String userTypeLabel(Object value) {
        if (value instanceof Number number) {
            return switch (number.intValue()) {
                case 2 -> "Kunde";
                case 4 -> "Lager";
                default -> "HQ";
            };
        }
        return "";
    }
}
