package vn.azteam.tpf.service.util;

import org.apache.poi.ss.usermodel.BorderStyle;
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.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.persistence.Tuple;
import java.math.BigInteger;
import java.sql.Date;

@Service
public class ExportExcelUtil {

    public ExportExcelUtil() {
    }
    public void setSequenceNoCellValue(CellStyle cellStyle, Row row, int cellIndex, int cellValue) {
        Cell cell = row.createCell(cellIndex);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(cellValue);
    }

    public void setSequenceNoCellValue(CellStyle cellStyle, Row row, int cellIndex, Double cellValue) {
        Cell cell = row.createCell(cellIndex);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(cellValue);
    }

    public void setSequenceNoCellValue(CellStyle cellStyle, Row row, int cellIndex, String cellValue) {
        Cell cell = row.createCell(cellIndex);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(cellValue);
    }

    public void setSequenceNoCellValue(CellStyle cellStyle, Row row, int cellIndex, Date cellValue) {
        Cell cell = row.createCell(cellIndex);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(cellValue);
    }

    public CellStyle createBorderCellStyle(XSSFWorkbook workbook) {
        CellStyle borderCellStyle = workbook.createCellStyle();
        borderCellStyle.setBorderTop(BorderStyle.THIN);
        borderCellStyle.setBorderBottom(BorderStyle.THIN);
        borderCellStyle.setBorderLeft(BorderStyle.THIN);
        borderCellStyle.setBorderRight(BorderStyle.THIN);
        return borderCellStyle;
    }

    public void setCellValue(CellStyle cellStyle, Tuple data, Row row, int cellIndex, String fieldName, Boolean isBooleanField) {
        Cell cell = row.createCell(cellIndex);
        cell.setCellStyle(cellStyle);
        char st = '\u2713'; //check mark
        if (data.get(fieldName) != null) {
            Class<?> clazz = data.get(fieldName).getClass();
            if (clazz == Double.class) {
                Double fieldValue = Double.valueOf((Double) data.get(fieldName));
                cell.setCellValue(fieldValue);
            } else if (clazz == Integer.class || clazz == BigInteger.class) {
                Long fieldValue;
                if (clazz == BigInteger.class) {
                    fieldValue = ((BigInteger) data.get(fieldName)).longValue();
                } else {
                    fieldValue = ((Integer) data.get(fieldName)).longValue();
                }
                // bool field
                if (isBooleanField) {
                    Boolean blnFieldValue = fieldValue == 1 ? true : false;
                    if (blnFieldValue) {
                        cell.setCellValue(String.valueOf(st));
                    }
                } else {
                    cell.setCellValue(fieldValue);
                }
            } else if (clazz == Boolean.class) {
                Boolean fieldValue = Boolean.valueOf((Boolean) data.get(fieldName));
                if (fieldValue) {
                    cell.setCellValue(String.valueOf(st));
                }
            } else if (clazz == java.sql.Date.class) {
                java.sql.Date fieldValue = (java.sql.Date) data.get(fieldName);
                cell.setCellValue(fieldValue);
            } else {
                String fieldValue = String.valueOf(data.get(fieldName));
                cell.setCellValue(fieldValue);
            }

        }
    }
    public void setCellValue(
        CellStyle cellStyle,
        Tuple data,
        Row row,
        int cellIndex,
        String fieldName) {
        setCellValue(cellStyle, data, row, cellIndex, fieldName, false);
    }
}
