package com.gci.boot.common.dict.excel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.gci.boot.common.dict.annotation.ExcelEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class EnumConverter implements Converter<Object> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Converter.super.supportJavaTypeKey();
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        try {
            Field field = contentProperty.getField();
            if(field.isAnnotationPresent(ExcelEnum.class)){
                ExcelEnum enumAnno = field.getAnnotation(ExcelEnum.class);
                Map<String, String> enumMap = getEnumMap(enumAnno.value(), false);
                Class<?> type = field.getType();
                String value = enumMap.get(cellData.getStringValue());
                if(Integer.class.getName().equals(type.getName())) {
                    return Integer.parseInt(value);
                }else{
                    return value;
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        try {
            Field field = contentProperty.getField();
            if(field.isAnnotationPresent(ExcelEnum.class)){
                ExcelEnum enumAnno = field.getAnnotation(ExcelEnum.class);
                Map<String, String> enumMap = getEnumMap(enumAnno.value(), true);
                return new WriteCellData<>(enumMap.get(value.toString()));
            }
        } catch (Exception e) {
            log.error("枚举转换出错", e);
        }
        return new WriteCellData<>(value.toString());
    }

    
    private Map<String, String> getEnumMap(String enumStr, boolean valueKey){
        Map<String, String> result = new HashMap<>();
        if(StringUtils.isBlank(enumStr)){
            return result;
        }
        String[] es = enumStr.split(",");
        for(String e : es){
            String[] k = e.trim().split("_");
            if(k.length < 2){
                continue;
            }
            if(valueKey) {
                result.put(k[0], k[1]);
            }else{
                result.put(k[1], k[0]);
            }
        }
        return result;
    }
}
