package com.tmerclub.cloud.product.strategy;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.tmerclub.cloud.common.constant.Constant;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;
/**
 * @author lanhai
 */
public class TakeStockMergeStrategy extends AbstractMergeStrategy {

    private final List<Integer> groupSizes;

    public TakeStockMergeStrategy(List<Integer> fruitGroupCount) {
        this.groupSizes = fruitGroupCount;
    }

    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
        if (cell.getRowIndex() == Constant.TWO) {
            if (cell.getColumnIndex() < Constant.SIX) {
                int rowCnt = 2;
                for (Integer count : groupSizes) {
                    if (count > 1) {
                        CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCnt, rowCnt + count - 1, cell.getColumnIndex(), cell.getColumnIndex());
                        sheet.addMergedRegionUnsafe(cellRangeAddress);
                    }
                    rowCnt += count;
                }
            }
        }
    }
}
