package de.thpeetz.kontor.comics.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.comics.data.Volume;

@SpringBootTest
class VolumeViewTest {

    @Autowired
    private VolumeView volumeView;

    @Test
    void formShownWhenVolumeSelected() {
        Grid<Volume> grid = volumeView.getGrid();

        Volume firstVolume = getFirstItem(grid);

        VolumeForm form = volumeView.getForm();
        assertFalse(form.isVisible());

        if (firstVolume != null) {
            grid.asSingleSelect().setValue(firstVolume);
            assertTrue(form.isVisible());
            assertEquals(firstVolume.getName(), form.name.getValue());
        }
    }

    private Volume getFirstItem(Grid<Volume> grid) {
        int count = grid.getListDataView().getItemCount();
        List<Volume> volumes = grid.getListDataView().getItems().collect(Collectors.toList());
        assertEquals(0, count);
        if (count > 0) {
            return volumes.get(0);
        } else {
            return null;
        }
    }
}
