package de.thpeetz.kontor.admin.views;

import de.thpeetz.kontor.admin.data.Permission;
import org.springframework.context.annotation.Scope;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.annotation.SpringComponent;

import de.thpeetz.kontor.admin.AdminConstants;
import de.thpeetz.kontor.admin.services.AdminService;
import de.thpeetz.kontor.common.views.MainLayout;
import jakarta.annotation.security.RolesAllowed;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringComponent
@Scope("prototype")
@RolesAllowed("ROLE_ADMIN")
@Route(value = AdminConstants.PERMISSION_ROUTE, layout = MainLayout.class)
@PageTitle("Permissions | Admin | Kontor")
public class PermissionView extends VerticalLayout {

    Grid<Permission> grid = new Grid<>(Permission.class);
    TextField filterText = new TextField();
    PermissionForm form;
    AdminService service;

    public PermissionView(AdminService service) {
        this.service = service;
        addClassName("permission-view");
        setSizeFull();
        configureGrid();
        configureForm();

        add(getToolbar(), getContent());
        updateList();
    }

    private void configureGrid() {
        grid.addClassName("permission-grid");
        grid.setSizeFull();
        grid.setColumns("name");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        grid.asSingleSelect().addValueChangeListener(event -> editPermission(event.getValue()));
    }

    private void configureForm() {
        form = new PermissionForm();
        form.setWidth("25em");
        form.addSaveListener(this::savePermission);
        form.addDeleteListener(this::deletePermission);
        form.addCloseListener(e -> closeEditor());
    }

    private void savePermission(PermissionForm.SaveEvent event) {
        service.savePermission(event.getPermission());
        updateList();
        closeEditor();
    }

    private void deletePermission(PermissionForm.DeleteEvent event) {
        service.deletePermission(event.getPermission());
        updateList();
        closeEditor();
    }

    private Component getContent() {
        HorizontalLayout content = new HorizontalLayout(grid, form);
        content.setFlexGrow(2, grid);
        content.setFlexGrow(1, form);
        content.addClassName("content");
        content.setSizeFull();
        return content;
    }

    private HorizontalLayout getToolbar() {
        filterText.setPlaceholder("Filter by user name...");
        filterText.setClearButtonVisible(true);
        filterText.setValueChangeMode(ValueChangeMode.LAZY);
        filterText.addValueChangeListener(e -> updateList());
        Button addUserButton = new Button("Add user", click -> addPermission());
        HorizontalLayout toolbar = new HorizontalLayout(filterText, addUserButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }

    public void editPermission(Permission permission) {
        if (permission == null) {
            closeEditor();
        } else {
            form.setPermission(permission);
            form.setVisible(true);
            addClassName("editing");
        }
    }

    public void closeEditor() {
        form.setPermission(null);
        form.setVisible(false);
        removeClassName("editing");
    }

    private void addPermission() {
        grid.asSingleSelect().clear();
        editPermission(new Permission());
    }

    private void updateList() {
        grid.setItems(service.findAllPermissions(filterText.getValue()));
    }
}
