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

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

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

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

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

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

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

    public TeamForm getForm() {
        return form;
    }

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

    private void saveTeam(TeamForm.SaveEvent event) {
        service.saveTeam(event.getTeam());
        updateList();
        closeEditor();
    }

    private void deleteTeam(TeamForm.DeleteEvent event) {
        service.deleteTeam(event.getTeam());
        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 addTeamButton = new Button("Add team");
        addTeamButton.addClickListener(click -> addTeam());

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

    public void editTeam(Team team) {
        if (team == null) {
            closeEditor();
        } else {
            form.setTeam(team);
            form.setRoosters(team.getRoosters());
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addTeam() {
        grid.asSingleSelect().clear();
        editTeam(new Team());
    }

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