package com.ecep.contract.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;

import java.time.LocalDate;

/**
 *
 */
public class ExcelUtils {
    public static Row getRow(Sheet sheet, int rowIndex, boolean autoCreate) {
        Row row = sheet.getRow(rowIndex);
        if (row == null) {
            if (autoCreate) {
                return sheet.createRow(rowIndex);
            }
            return null;
        }
        return row;
    }

    public static Cell getCell(Sheet sheet, String cellAddress) {
        return getCell(sheet, cellAddress, false);
    }

    public static Cell getCell(Sheet sheet, String cellAddress, boolean autoCreate) {
        CellAddress address = new CellAddress(cellAddress);
        Row row = getRow(sheet, address.getRow(), autoCreate);
        if (row == null) {
            return null;
        }
        return getCell(row, address.getColumn(), autoCreate);
    }

    public static Cell getCell(Sheet sheet, int rowIndex, int columnIndex, boolean autoCreate) {
        Row row = getRow(sheet, rowIndex, autoCreate);
        if (row == null) {
            return null;
        }
        return getCell(row, columnIndex, autoCreate);
    }

    public static Cell getCell(Row row, int columnIndex, boolean autoCreate) {
        Cell cell = row.getCell(columnIndex);
        if (cell == null) {
            if (autoCreate) {
                return row.createCell(columnIndex);
            }
            return null;
        }
        return cell;
    }

    public static String getCellValue(Sheet sheet, String cellAddress) {
        Cell cell = getCell(sheet, cellAddress);
        if (cell == null) {
            return null;
        }
        return cell.getStringCellValue();
    }


    public static Cell setCellValue(Sheet sheet, String cellAddress, String text) {
        CellAddress address = new CellAddress(cellAddress);
        return setCellValue(sheet, address.getRow(), address.getColumn(), text);
    }

    public static Cell setCellValue(Sheet sheet, String cellAddress, double value) {
        CellAddress address = new CellAddress(cellAddress);
        return setCellValue(sheet, address.getRow(), address.getColumn(), value);
    }

    public static Cell setCellValue(Sheet sheet, String cellAddress, LocalDate date) {
        CellAddress address = new CellAddress(cellAddress);
        return setCellValue(sheet, address.getRow(), address.getColumn(), date);
    }


    public static Cell setCellValue(Sheet sheet, int rowIndex, int columnIndex, String text) {
        Row row = getRow(sheet, rowIndex, true);
        return setCellValue(row, columnIndex, text);
    }

    public static Cell setCellValue(Row row, int columnIndex, String text) {
        assert row != null;
        Cell cell = getCell(row, columnIndex, true);
        assert cell != null;
        cell.setCellValue(text);
        return cell;
    }

    public static Cell setCellValue(Sheet sheet, int rowIndex, int columnIndex, double value) {
        Row row = getRow(sheet, rowIndex, true);
        return setCellValue(row, columnIndex, value);
    }

    public static Cell setCellValue(Row row, int columnIndex, double value) {
        assert row != null;
        Cell cell = getCell(row, columnIndex, true);
        assert cell != null;
        cell.setCellValue(value);
        return cell;
    }

    public static Cell setCellValue(Sheet sheet, int rowIndex, int columnIndex, LocalDate date) {
        Row row = getRow(sheet, rowIndex, true);
        assert row != null;
        return setCellValue(row, columnIndex, date);
    }

    public static Cell setCellValue(Row row, int columnIndex, LocalDate date) {
        assert row != null;
        Cell cell = getCell(row, columnIndex, true);
        assert cell != null;
        cell.setCellValue(date);
        return cell;
    }

    public static Cell setCellValue(Sheet sheet, int rowIndex, int columnIndex, Double value) {
        Row row = getRow(sheet, rowIndex, true);
        assert row != null;
        Cell cell = getCell(row, columnIndex, true);
        assert cell != null;
        if (value == null) {
            cell.setBlank();
        } else {
            cell.setCellValue(value);
        }
        return cell;
    }

    public static void setBorder(CellStyle cellStyle, BorderStyle borderStyle) {
        cellStyle.setBorderTop(borderStyle);
        cellStyle.setBorderBottom(borderStyle);
        cellStyle.setBorderLeft(borderStyle);
        cellStyle.setBorderRight(borderStyle);
    }

    public static void mergedCells(Sheet sheet, String address) {
        CellRangeAddress cellAddresses = CellRangeAddress.valueOf(address);
        sheet.addMergedRegion(cellAddresses);
    }

    public static void mergedCells(Cell cell, int rows, int cols) {
        Sheet sheet = cell.getSheet();
        CellRangeAddress region = new CellRangeAddress(cell.getRowIndex(), cell.getRowIndex() + rows, cell.getColumnIndex(), cell.getColumnIndex() + cols);
        sheet.addMergedRegion(region);
    }

    public static void copyStyle(Sheet source, String sourceCellAddress, Sheet target, String targetCellAddress) {
        Cell sourceCell = getCell(source, sourceCellAddress);
        Cell targetCell = getCell(target, targetCellAddress);
        copyStyle(sourceCell, targetCell);
    }

    public static void copyStyle(Sheet source, String sourceCellAddress, Cell targetCell) {
        Cell sourceCell = getCell(source, sourceCellAddress);
        copyStyle(sourceCell, targetCell);
    }

    public static void copyStyle(Cell source, Cell target) {
        if (source == null) {
            return;
        }
        if (target == null) {
            return;
        }
        target.setCellStyle(source.getCellStyle());
    }
}
