package de.assecutor.votianlt.pages.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.html.Div;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.HasDynamicTitle;
import com.vaadin.flow.router.Route;
import de.assecutor.votianlt.model.AppUser;
import de.assecutor.votianlt.pages.base.ui.component.ViewToolbar;
import de.assecutor.votianlt.pages.service.AppUserService;
import jakarta.annotation.security.RolesAllowed;
import org.springframework.beans.factory.annotation.Autowired;

@Route(value = "app-user", layout = de.assecutor.votianlt.pages.base.ui.view.MainLayout.class)
@RolesAllowed({ "USER", "ADMIN" })
public class AppUserView extends VerticalLayout implements HasDynamicTitle {

    private final AppUserService appUserService;
    private final Grid<AppUser> appUserGrid;

    @Autowired
    public AppUserView(AppUserService appUserService) {
        this.appUserService = appUserService;

        setSizeFull();
        setPadding(true);
        setSpacing(true);
        addClassName("data-view");

        Button addButton = new Button(getTranslation("appuser.button.add"), new Icon(VaadinIcon.PLUS));
        addButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
        addButton.addClickListener(e -> navigateToAddAppUser());

        add(new ViewToolbar(getTranslation("appuser.title"), addButton));

        // Grid für App-Nutzer
        appUserGrid = new Grid<>(AppUser.class, false);
        appUserGrid.setSizeFull();
        appUserGrid.addClassName("data-grid");

        // Grid-Spalten konfigurieren
        appUserGrid.addColumn(AppUser::getBezeichnung).setHeader(getTranslation("appuser.column.designation"))
                .setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getVorname).setHeader(getTranslation("appuser.column.firstname"))
                .setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getNachname).setHeader(getTranslation("appuser.column.lastname"))
                .setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getTelefon).setHeader(getTranslation("appuser.column.phone")).setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getAppCode).setHeader(getTranslation("appuser.column.appcode"))
                .setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getEmail).setHeader(getTranslation("appuser.column.email")).setAutoWidth(true);

        // Make grid rows clickable
        appUserGrid.setSelectionMode(Grid.SelectionMode.SINGLE);
        appUserGrid.getStyle().set("cursor", "pointer");

        // Add click listener to navigate to edit view
        appUserGrid.addItemClickListener(event -> {
            AppUser appUser = event.getItem();
            if (appUser != null && appUser.getId() != null) {
                getUI().ifPresent(ui -> ui.navigate("edit-app-user/" + appUser.getId().toHexString()));
            }
        });

        Div gridPanel = new Div(appUserGrid);
        gridPanel.addClassNames("surface-panel", "data-grid-panel");
        gridPanel.setSizeFull();

        add(gridPanel);

        // Daten laden
        loadAppUsers();
    }

    private void loadAppUsers() {
        var appUsers = appUserService.findByCurrentUser();
        appUserGrid.setItems(appUsers);
    }

    private void navigateToAddAppUser() {
        getUI().ifPresent(ui -> ui.navigate("add-app-user"));
    }

    @Override
    public String getPageTitle() {
        return getTranslation("page.title.appusers");
    }
}
