package com.qmth.boot.tools.excel.model;

import org.apache.poi.ss.usermodel.CellStyle;

import java.util.*;
import java.util.stream.Collectors;

public class CellStyleConfig {

    private static final List<ConfigItem> emptyList = new ArrayList<>();

    private Map<String, List<ConfigItem>> sheetItems;

    public CellStyleConfig() {
        this.sheetItems = new HashMap<>();
    }

    public void addCellStyle(String sheetName, int rowNumber, int[] columnIndex, CellStyle style) {
        ConfigItem item = new ConfigItem();
        item.setRowNumber(rowNumber);
        item.setColumnIndexs(columnIndex);
        item.setStyle(style);
        sheetItems.computeIfAbsent(sheetName, k -> new LinkedList<>()).add(item);
    }

    public CellStyle getStyle(String sheetName, int rowNumber, int columnIndex) {
        return sheetItems.getOrDefault(sheetName, emptyList).stream()
                .filter(i -> i.getRowNumber() == rowNumber && i.getColumnIndexs().contains(columnIndex)).findFirst()
                .orElse(new ConfigItem()).getStyle();
    }

    private class ConfigItem {

        private int rowNumber;

        private Set<Integer> columnIndexs;

        private CellStyle style;

        public int getRowNumber() {
            return rowNumber;
        }

        public void setRowNumber(int rowNumber) {
            this.rowNumber = rowNumber;
        }

        public Set<Integer> getColumnIndexs() {
            return columnIndexs;
        }

        public void setColumnIndexs(int[] columnIndexs) {
            this.columnIndexs = Arrays.stream(columnIndexs).boxed().collect(Collectors.toSet());
        }

        public CellStyle getStyle() {
            return style;
        }

        public void setStyle(CellStyle style) {
            this.style = style;
        }
    }
}
