package de.avatic.lcc.service.bulk.helper;

import de.avatic.lcc.model.bulk.HeaderProvider;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

import java.util.EnumSet;

@Service
public class HeaderGenerator {

    public <H extends Enum<H> & HeaderProvider> boolean validateHeader(Sheet sheet, Class<H> headers) {
        Row row = sheet.getRow(0);
        for(H header : EnumSet.allOf(headers)){
            Cell cell = row.getCell(header.ordinal());
            if(cell == null || !cell.getStringCellValue().equals(header.getHeader())){
                return false;
            }
        }
        return true;
    }

    public boolean validateHeader(Sheet sheet, String[] headers) {
        Row row = sheet.getRow(0);
        int idx = 0;
        for(String header : headers){
            Cell cell = row.getCell(idx++);
            if(cell == null || !cell.getStringCellValue().equals(header)){
                return false;
            }
        }
        return true;
    }

    public <H extends Enum<H> & HeaderProvider> void generateHeader(Sheet worksheet, Class<H> headers, CellStyle style) {
        Row row = worksheet.createRow(0);
        for (H header : EnumSet.allOf(headers)) {
            Cell cell = row.createCell(header.ordinal());
            cell.setCellValue(header.getHeader());
            cell.setCellStyle(style);
            worksheet.autoSizeColumn(header.ordinal());
        }
    }

    public void generateHeader(Sheet worksheet, String[] headers, CellStyle style) {
        Row row = worksheet.createRow(0);
        int idx = 0;
        for (String header : headers) {
            Cell cell = row.createCell(idx);
            cell.setCellValue(header);
            cell.setCellStyle(style);
            worksheet.autoSizeColumn(idx++);
        }
    }


}
