package de.thpeetz.kontor.tysc.views;

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.data.Sport;
import de.thpeetz.kontor.tysc.services.SportService;
import jakarta.annotation.security.PermitAll;

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

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

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

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

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

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

    public SportForm getForm() {
        return form;
    }

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

    private void saveSport(SportForm.SaveEvent event) {
        service.saveSport(event.getSport());
        updateList();
        closeEditor();
    }

    private void deleteSport(SportForm.DeleteEvent event) {
        service.deleteSport(event.getSport());
        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 addSportButton = new Button("Add sport");
        addSportButton.addClickListener(click -> addSport());

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

    public void editSport(Sport sport) {
        if (sport == null) {
            closeEditor();
        } else {
            form.setSport(sport);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addSport() {
        grid.asSingleSelect().clear();
        editSport(new Sport());
    }

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