package com.ywt.models;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Excel style POJO
 *
 * @author Walker
 * Created on 2019/7/2
 */
public class ExcelStyleMap extends LinkedHashMap<String, Map<String, Object>> {

    public static final String STYLE_COLUMN_WIDTH = "STYLE_COLUMN_WIDTH";
    public static final String STYLE_ROW_HEIGHT = "STYLE_ROW_HEIGHT";

    public ExcelStyleMap(String[] columnNames) {
        super();
        for (String columnName : columnNames) {
            this.put(columnName, new HashMap<>());
        }
    }

    public void putValueSafely(String column, String styleName, Object styleValue) {
        if (this.containsKey(column)) {
            Map<String, Object> map = this.get(column);
            if (map == null) map = new HashMap<>();
            map.put(styleName, styleValue);
        } else {
            HashMap<String, Object> map = new HashMap<>();
            map.put(styleName, styleValue);
            this.put(column, map);
        }
    }

    public Object getValueSafely(String column, String styleName, Object defVal) {
        if (this.containsKey(column)) {
            Map<String, Object> map = this.get(column);
            if (map == null) return defVal;
            return map.getOrDefault(styleName, defVal);
        } else {
            return defVal;
        }
    }

    public int getIntValueSafely(String column, String styleName, Integer defVal) {
        Object result = getValueSafely(column, styleName, defVal);
        if (result instanceof Integer) return (int) result;
        return 0;
    }
}
