package org.vaadin.example;

import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

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.grid.GridVariant;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.provider.ListDataProvider;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.StreamResource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Route("multi-grids")
@Slf4j
public class SlowMultiGrids extends VerticalLayout implements Serializable {

    static int NUMBER_OF_GRID_ITEMS = 40;
    static List<GridDto> gridItems;

    static {
        gridItems = new ArrayList<>();
        for (int j = 0; j < NUMBER_OF_GRID_ITEMS; j++) {
            GridDto dto = new GridDto(j, LocalDateTime.now(), "dom", 42, List.of(new PlanungDocumentInfo("filename", j)));
            gridItems.add(dto);
        }
    }

    public SlowMultiGrids() {
        int NUMBER_OF_GRIDS = 20;
        for (int i = 0; i < NUMBER_OF_GRIDS; i++) {
            add(createGrid(i));
        }
        log.info("constructor finished");
    }

    private Component createGrid(int i) {
        var header = new H2("PlanungSystem " + i);

        Grid<GridDto> grid = new Grid<>();
        grid.addThemeVariants(GridVariant.LUMO_ROW_STRIPES);
        //grid.setSelectionMode(Grid.SelectionMode.MULTI);


        grid.addColumn(GridDto::id).setHeader("Name");
        grid.addColumn(GridDto::createdBy).setHeader("von");
        grid.addColumn(item -> {
                List<String> filenames = item.planungDocuments().stream().map(PlanungDocumentInfo::filename).toList();
                return String.join(", ", filenames);
            })
            .setHeader("Datei");

        grid.addColumn(GridDto::numberOfBauteile).setHeader("Bauteile");

        grid.addComponentColumn(item -> {
            return createDownloadComponent(item);
        });

        grid.getColumns().forEach(col -> col.setAutoWidth(true));
        grid.setItems(new ListDataProvider<>(gridItems));

        return new VerticalLayout(header, grid);
    }

    private Component createDownloadComponent(GridDto item) {
        HorizontalLayout hLayout = new HorizontalLayout();
        hLayout.setSpacing(false);
        for (PlanungDocumentInfo documentInfo : item.planungDocuments()) {
            Button button = new Button(VaadinIcon.DOWNLOAD.create());
            Anchor anchor = new Anchor(new StreamResource(documentInfo.filename(), () -> new ByteArrayInputStream("document".getBytes())), "");
            anchor.getElement().setAttribute("download", true);
            anchor.getElement().appendChild(button.getElement());
            hLayout.add(anchor);
        }
        return hLayout;
    }

    record GridDto(Integer id, LocalDateTime createdAt, String createdBy, Integer numberOfBauteile,
                                  List<PlanungDocumentInfo> planungDocuments) {}

    record PlanungDocumentInfo(String filename, Integer documentId) {}
}
