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

import de.avatic.lcc.model.bulk.header.HeaderProvider;
import de.avatic.lcc.util.exception.internalerror.ExcelValidationError;
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 {

    private static final int ADD_COLUMN_SIZE = (10*256);

    public <H extends Enum<H> & HeaderProvider> void 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())){
                throw new ExcelValidationError("Unable to validate header \"" + header.getHeader() + "\": Header of column " + toExcelLetter(header.ordinal()) + " has to be \"" + header.getHeader() + "\"");
            }
        }
    }

    public void 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)){
                throw new ExcelValidationError("Unable to validate header \"" + header + "\": Header of column " + toExcelLetter(idx) + " has to be \"" + header + "\"");

            }
        }
    }

    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++);
        }
    }


    public void fixWidth(Sheet sheet, String[] headers) {
        int idx = 0;
        for (String header : headers) {
            sheet.autoSizeColumn(idx);
            sheet.setColumnWidth(idx,sheet.getColumnWidth(idx)+ADD_COLUMN_SIZE);
            idx++;
        }
    }

    public <H extends Enum<H> & HeaderProvider> void fixWidth(Sheet sheet, Class<H> headers) {
        for (H header : EnumSet.allOf(headers)) {
            sheet.autoSizeColumn(header.ordinal());
            sheet.setColumnWidth(header.ordinal(),sheet.getColumnWidth(header.ordinal())+ADD_COLUMN_SIZE);
        }
    }

    private String toExcelLetter(int columnIdx) {
        StringBuilder result = new StringBuilder();
        columnIdx++; // Convert from 0-based to 1-based for the algorithm
        while (columnIdx > 0) {
            columnIdx--; // Adjust for 1-based indexing
            result.insert(0, (char) ('A' + columnIdx % 26));
            columnIdx /= 26;
        }
        return result.toString();
    }
}
