package de.thpeetz.kontor.tysc.views;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.vaadin.flow.component.grid.Grid;

import de.thpeetz.kontor.tysc.data.Player;

@SpringBootTest
class PlayerViewTest {

    @Autowired
    private PlayerView playerView;

    @Test
    void formShownWhenPlayerSelected() {
        Grid<Player> grid = playerView.getGrid();
        Player firstPlayer = getFirstItem(grid);

        PlayerForm form = playerView.getForm();

        assertFalse(form.isVisible());
        grid.asSingleSelect().setValue(firstPlayer);
        assertTrue(form.isVisible());
        assertEquals(firstPlayer.getLastName(), form.lastName.getValue());
        assertEquals(firstPlayer.getFirstName(), form.firstName.getValue());
    }

    private Player getFirstItem(Grid<Player> grid) {
        int count = grid.getListDataView().getItemCount();
        List<Player> players = grid.getListDataView().getItems().collect(Collectors.toList());
        assertEquals(38, count);
        return players.get(0);
    }
}
