package com.heyu.api.data.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class AnnotationUtils {


    public static boolean hasAnnotation(Class type, String name) {
        Annotation[] annotations = type.getAnnotations();
        if (annotations != null && annotations.length > 0) {
            for (Annotation annotation : annotations) {
                if (name.equals(getAnnotationName(annotation))) {
                    return true;
                }
            }
        }
        return false;
    }

    public static String getAnnotation(Class type, String name) {
        Annotation[] annotations = type.getAnnotations();
        if (annotations != null && annotations.length > 0) {
            for (Annotation annotation : annotations) {
                if (name.equals(getAnnotationName(annotation))) {
                    return getAnnotationValue(annotation,"value");
                }
            }
        }
        return null;
    }


    public static boolean hasAnnotation(Method method, String name) {
        Annotation[] annotations = method.getAnnotations();
        if (annotations != null && annotations.length > 0) {
            for (Annotation annotation : annotations) {
                if (name.equals(getAnnotationName(annotation))) {
                    return true;
                }
            }
        }
        return false;
    }

    public static String getAnnotation(Method method, String name) {
        Annotation[] annotations = method.getAnnotations();
        if (annotations != null && annotations.length > 0) {
            for (Annotation annotation : annotations) {
                if (name.equals(getAnnotationName(annotation))) {
                    return getAnnotationValue(annotation,"tencent");
                }
            }
        }
        return null;
    }



    public static String getAnnotationName(Annotation annotation) {
        String annotionStr = annotation.toString();
        int a = annotionStr.indexOf("(", 0);
        if (a != -1) {
            annotionStr = annotionStr.substring(0, a);
            String strs[] = annotionStr.split("\\.");
            if (strs != null && strs.length > 0) {
                return strs[strs.length - 1];
            }
        }
        return annotionStr;
    }


    public static <T> T getAnnotationValueByMethod(Method method, String annotationName, String key) {
        Annotation annotation[] = method.getAnnotations();
        if (annotation != null && annotation.length > 0) {
            for (Annotation annotation1 : annotation) {

                if (annotationName.equals(getAnnotationName(annotation1))) {
                    return getAnnotationValue(annotation1, key);
                }


            }
        }


        return null;
    }




    public static <T> T getAnnotationValueByClass(Class clazz, String annotationName, String key) {
        Annotation annotation[] = clazz.getAnnotations();
        if (annotation != null && annotation.length > 0) {
            for (Annotation annotation1 : annotation) {
                if (annotationName.equals(getAnnotationName(annotation1))) {
                    return getAnnotationValue(annotation1, key);
                }
            }
        }
        return null;
    }





    public static <T> T getAnnotationValue(Annotation annotation,String key) {
        try {
            Method method = annotation.getClass().getMethod(key);
            if (method != null) {
                T paramName = (T) method.invoke(annotation);
                return paramName;
            }
        } catch (NoSuchMethodException e) {

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }




}
