package de.thpeetz.kontor.tysc.views;

import de.thpeetz.kontor.tysc.data.FieldPosition;
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.common.views.MainLayout;
import de.thpeetz.kontor.tysc.TyscConstants;
import de.thpeetz.kontor.tysc.services.SportService;
import jakarta.annotation.security.PermitAll;

@SpringComponent
@Scope("prototype")
@PermitAll
@Route(value = TyscConstants.POSITION_ROUTE, layout = MainLayout.class)
@PageTitle("Position | Tysc | Kontor")
public class PositionView extends VerticalLayout {

    Grid<FieldPosition> grid = new Grid<>(FieldPosition.class);
    TextField filterText = new TextField();
    PositionForm form;
    SportService service;

    public PositionView(SportService service) {
        this.service = service;
        addClassName("position-view");
        setSizeFull();
        configureGrid();
        configureForm();

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

    public Grid<FieldPosition> getGrid() {
        return grid;
    }

    private void configureGrid() {
        grid.addClassName("position-grid");
        grid.setSizeFull();
        grid.setColumns("shortName", "name", "sport.name");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        grid.asSingleSelect().addValueChangeListener(event -> editPosition(event.getValue()));
    }

    public PositionForm getForm() {
        return form;
    }

    private void configureForm() {
        form = new PositionForm();
        form.setWidth("25em");
        form.setVisible(false);
        form.addSaveListener(this::savePosition);
        form.addDeleteListener(this::deletePosition);
        form.addCloseListener(e -> closeEditor());
    }

    private void savePosition(PositionForm.SaveEvent event) {
        service.savePosition(event.getPosition());
        updateList();
        closeEditor();
    }

    private void deletePosition(PositionForm.DeleteEvent event) {
        service.deletePosition(event.getPosition());
        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 name...");
        filterText.setClearButtonVisible(true);
        filterText.setValueChangeMode(ValueChangeMode.LAZY);
        filterText.addValueChangeListener(e -> updateList());

        Button addPositionButton = new Button("Add position");
        addPositionButton.addClickListener(click -> addPosition());

        HorizontalLayout toolbar = new HorizontalLayout(filterText, addPositionButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }

    public void editPosition(FieldPosition fieldPosition) {
        if (fieldPosition == null) {
            closeEditor();
        } else {
            form.setPosition(fieldPosition);
            form.setRoosters(fieldPosition.getRoosters());
            form.setVisible(true);
            addClassName("editing");
        }
    }

    private void closeEditor() {
        form.setPosition(null);
        form.setVisible(false);
        removeClassName("editing");
    }

    private void addPosition() {
        grid.asSingleSelect().clear();
        editPosition(new FieldPosition());
    }

    public void updateList() {
        grid.setItems(service.findAllPositions(filterText.getValue()));
    }
}
