package com.eactive.eai.rms.common.util;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PoiUtils {
    public static CellStyle createHeaderCellStyle(Workbook workbook){
        CellStyle cellStyle = workbook.createCellStyle();
        // 사용자 정의 색상 설정 (파스텔 톤 청녹색)
        if (workbook instanceof XSSFWorkbook) {
            XSSFCellStyle xssfHeaderStyle = (XSSFCellStyle) cellStyle;
            xssfHeaderStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(204, 236, 255)));
            xssfHeaderStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        } else {
            // HSSF 사용 시 IndexedColors로 대체
            cellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
        }

        // 테두리 설정 추가
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerFont.setColor(IndexedColors.DARK_BLUE.getIndex()); // 글꼴 색상 변경
        cellStyle.setFont(headerFont);
        return cellStyle;
    }

    public static CellStyle createTextCellStyle(Workbook workbook){
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        Font textFont = workbook.createFont();
        textFont.setFontHeightInPoints((short)12);
        textFont.setColor(IndexedColors.DARK_GREEN.getIndex()); // 글꼴 색상 변경
        cellStyle.setFont(textFont);

        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        return cellStyle;
    }

    public static CellStyle createTitleCellStyle(Workbook workbook){
        CellStyle cellStyle = workbook.createCellStyle();
        // 사용자 정의 색상 설정 (파스텔 톤 청녹색)
        if (workbook instanceof XSSFWorkbook) {
            XSSFCellStyle xssfHeaderStyle = (XSSFCellStyle) cellStyle;
            xssfHeaderStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 235, 156)));
            xssfHeaderStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        } else {
            // HSSF 사용 시 IndexedColors로 대체
            cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        }

        // 테두리 설정 추가
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short)18);
        font.setColor(IndexedColors.BROWN.getIndex()); // 글꼴 색상 변경
        cellStyle.setFont(font);
        return cellStyle;
    }


    public static String getCellValue(Sheet sheet, int rowNum, int columnNum){
        Cell cell = sheet.getRow(rowNum).getCell(columnNum);
        if (cell == null) {
            return ""; // 셀이 null인 경우 빈 문자열 반환
        }

        DataFormatter formatter = new DataFormatter(); // 셀 포맷에 맞춰 문자열로 변환
        return formatter.formatCellValue(cell);
    }

    public static int convertCellToInt(Cell cell) {
        if (cell == null) {
            return 0;
        }

        CellType cellType = cell.getCellTypeEnum();
        switch (cellType) {
            case NUMERIC:
                return (int)cell.getNumericCellValue();
            case STRING:
                return NumberUtils.toInt(cell.getStringCellValue(), 0);
            default:
                return 0;
        }
    }

}
