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.TradePaperback;

@SpringBootTest
class TradePaperbackViewTest {

    @Autowired
    private TradePaperbackView tradePaperbackView;

    @Test
    void formShownWhenVolumeSelected() {
        Grid<TradePaperback> grid = tradePaperbackView.getGrid();

        TradePaperback firstTradePaperback = getFirstItem(grid);

        TradePaperBackForm form = tradePaperbackView.getForm();
        assertFalse(form.isVisible());

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

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