package com.sckw.excel.config.easyexcel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.sckw.core.model.constant.NumberConstant;
import com.sckw.excel.annotation.EasyExcel;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author lfdc
 * @description 状态枚举信息获取（code->value，1代表失败 进行转换）
 * @date 2023-07-28 10:07:51
 */
@Slf4j
public class ExcelConverter implements Converter<String> {
    @Override
    public Class supportJavaTypeKey() {
        //指定状态码类型
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        //指定需要获取的参数类型
        return CellDataTypeEnum.STRING;
    }

    /**
     * 导出时转换
     *
     * @param value               状态码
     * @param contentProperty     字段属性
     * @param globalConfiguration 全局配置
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Field field = contentProperty.getField();
        EasyExcel easyExcel = field.getAnnotation(EasyExcel.class);
        Class<? extends Enum> type = easyExcel.type();
        int status = easyExcel.status();
        boolean flag = easyExcel.flag();
        Object invoke = null;
        if (flag) {
            invoke = verifyTranslate(value, status, type);
        } else {
            return new WriteCellData(value.toString());
        }
        if (invoke == null) {
            return new WriteCellData();
        }
        return new WriteCellData(invoke.toString());
    }

    /**
     * 应对不同枚举处理
     *
     * @param value
     * @param status
     * @param type
     * @return
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    private Object verifyTranslate(String value, int status, Class<? extends Enum> type) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Object invoke = null;
        try {
            if (NumberConstant.ONE == status) {
                Method codeOf = type.getMethod("getDestination", String.class);
                invoke = codeOf.invoke(type, value);
            } else if (NumberConstant.TWO == status) {
                Method codeOf = type.getMethod("getName", Integer.class);
                invoke = codeOf.invoke(type, Integer.valueOf(value));
            } else if (NumberConstant.THREE == status) {
                Method codeOf = type.getMethod("getEntityByCode", Integer.class);
                invoke = codeOf.invoke(type, value);
//                Method getName = invoke.getClass().getMethod("getDestination");
//                invoke = String.valueOf(getName.invoke(invoke));
            } else {
                invoke = value;
            }
        } catch (NoSuchMethodException e) {
            log.info("NoSuchMethodException ERROR:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        } catch (SecurityException e) {
            log.info("SecurityException ERROR:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            log.info("IllegalAccessException ERROR:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        } catch (IllegalArgumentException e) {
            log.info("IllegalArgumentException ERROR:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            log.info("InvocationTargetException ERROR:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
        return invoke;
    }

}
