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

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

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

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

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

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

    private void configureGrid() {
        grid.addClassName("player-grid");
        grid.setSizeFull();
        grid.setColumns("firstName", "lastName");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        grid.asSingleSelect().addValueChangeListener(event -> editPlayer(event.getValue()));
    }

    public PlayerForm getForm() {
        return form;
    }

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

    private void savePlayer(PlayerForm.SaveEvent event) {
        service.savePlayer(event.getPlayer());
        updateList();
        closeEditor();
    }

    private void deletePlayer(PlayerForm.DeleteEvent event) {
        service.deletePlayer(event.getPlayer());
        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 addPlayerButton = new Button("Add player");
        addPlayerButton.addClickListener(click -> addPlayer());

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

    public void editPlayer(Player player) {
        if (player == null) {
            closeEditor();
        } else {
            form.setPlayer(player);
            form.setRoosters(player.getRoosters());
            form.setVisible(true);
            addClassName("editing");
        }
    }

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

    private void addPlayer() {
        grid.asSingleSelect().clear();
        editPlayer(new Player());
    }

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