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

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

    Grid<Rooster> grid = new Grid<>(Rooster.class);
    RoosterForm form;
    SportService service;

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

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

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

    private void configureGrid() {
        grid.addClassName("rooster-grid");
        grid.setSizeFull();
        grid.setColumns("year", "team.name", "player.fullName", "position.name");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        grid.asSingleSelect().addValueChangeListener(event -> editRooster(event.getValue()));
    }

    public RoosterForm getForm() {
        return form;
    }

    private void configureForm() {
        form = new RoosterForm(service.findAllTeams(null), service.findAllPlayers(null), service.findAllPositions(null));
        form.setWidth("25em");
        form.setVisible(false);
        form.addSaveListener(this::saveRooster);
        form.addDeleteListener(this::deleteRooster);
        form.addCloseListener(e -> closeEditor());
    }

    private void saveRooster(RoosterForm.SaveEvent event) {
        service.saveRooster(event.getRooster());
        updateList();
        closeEditor();
    }

    private void deleteRooster(RoosterForm.DeleteEvent event) {
        service.deleteRooster(event.getRooster());
        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() {
        Button addRoosterButton = new Button("Add Rooster");
        addRoosterButton.addClickListener(click -> addRooster());

        HorizontalLayout toolbar = new HorizontalLayout(addRoosterButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }

    public void editRooster(Rooster rooster) {
        if (rooster == null) {
            closeEditor();
        } else {
            form.setRooster(rooster);
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addRooster() {
        grid.asSingleSelect().clear();
        editRooster(new Rooster());
    }

    public void updateList() {
        grid.setItems(service.findAllRoosters());
    }
}