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.Worktype;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootTest
class WorktypeViewTest {

    @Autowired
    private WorktypeView worktypeView;

    @Test
    void formShownWhenWorktypeSelected() {
        Grid<Worktype> grid = worktypeView.getGrid();

        Worktype firstWorktype = getFirstItem(grid);

        WorktypeForm form = worktypeView.getForm();
        assertFalse(form.isVisible());

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

    private Worktype getFirstItem(Grid<Worktype> grid) {
        int count = grid.getListDataView().getItemCount();
        List<Worktype> worktypes = grid.getListDataView().getItems().collect(Collectors.toList());
        log.info("found worktypes: {}", worktypes);
        assertEquals(3, count);
        return worktypes.get(0);
    }
}
