package com.maintain.common.utils.poi;

import org.apache.commons.lang3.StringUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * 注解工具类
 */
public class AnnotationUtils {

    /**
     * 变更注解的属性值再处理业务，处理完业务之后恢复类的属性
     *
     * @param clazz     注解所在的实体类
     * @param tClass    注解类
     * @param attrName 要修改的注解属性名
     * @param attrTypeEnum 要修改的注解属性的类型
     * @param valueMap  要设置的属性值
     */
    public static <A extends Annotation> void changeAnnotationValueToDealProcess(
            Class<?> clazz,
            Class<A> tClass,
            String attrName,
            AttrTypeEnum attrTypeEnum,
            Map<String, String> valueMap,
            DealProcess dealProcess) {
        try {

            Map<String, Object> fieldAnnotationValueMap = new HashMap<>();

            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                A annotation = field.getAnnotation(tClass);
                if (annotation == null) continue;
                Object value = setAnnotationValue(annotation, attrName, attrTypeEnum, valueMap);
                String fieldName = field.getName();
                fieldAnnotationValueMap.put(fieldName, value);
            }

            // 处理业务逻辑
            dealProcess.deal();

            // 恢复
            for (Field field : fields) {
                A annotation = field.getAnnotation(tClass);
                String fieldName = field.getName();
                if (annotation == null) continue;
                Object value = fieldAnnotationValueMap.get(fieldName);

                InvocationHandler handler = Proxy.getInvocationHandler(annotation);
                Field memberValuesField = handler.getClass().getDeclaredField("memberValues");
                memberValuesField.setAccessible(true);
                @SuppressWarnings("all")
                Map<String, Object> memberValues = (Map) memberValuesField.get(handler);
                memberValues.put(attrName, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置注解中的字段值
     *
     * @param annotation 要修改的注解实例
     * @param attrName  要修改的注解属性名
     * @param attrTypeEnum 要修改的注解属性的类型
     * @param valueMap   替换属性集的map
     */
    @SuppressWarnings("all")
    private static Object setAnnotationValue(Annotation annotation, String attrName,
                                             AttrTypeEnum attrTypeEnum, Map<String, String> valueMap) throws NoSuchFieldException, IllegalAccessException {
        InvocationHandler handler = Proxy.getInvocationHandler(annotation);
        Field field = handler.getClass().getDeclaredField("memberValues");
        field.setAccessible(true);
        Map memberValues = (Map) field.get(handler);
        Object value = memberValues.get(attrName);
        switch (attrTypeEnum) {
            case STRING: {
                String oldValue = (String) value;
                String newValue = valueMap.get(oldValue);
                if (StringUtils.isNotBlank(newValue)) {
                    memberValues.put(attrName, newValue);
                }
            }
            break;
            case STRING_ARR: {
                String[] oldValue = (String[]) value;
                String[] newValue = new String[oldValue.length];
                for (int i = 0; i < oldValue.length; i++) {
                    String replace = valueMap.get(oldValue[i]);
                    newValue[i] = replace != null ? replace : oldValue[i];
                }
                memberValues.put(attrName, newValue);
            }
            break;
        }
        return value;
    }

    public enum AttrTypeEnum {
        STRING,
        STRING_ARR
    }

    public interface DealProcess {

        void deal() throws Exception;

    }

}
