package com.timeyang.jkes.core.util;

import com.timeyang.jkes.core.annotation.Document;
import com.timeyang.jkes.core.annotation.Version;
import com.timeyang.jkes.core.annotation.Field;
import com.timeyang.jkes.core.annotation.MultiFields;
import com.timeyang.jkes.core.elasticsearch.exception.IllegalAnnotatedFieldException;
import com.timeyang.jkes.core.support.Config;

import java.lang.reflect.Method;

/**
 * Document Utils
 *
 * @author chaokunyang
 */
public class DocumentUtils {

    public static String getIndexName(Class<?> clazz) {
        Document document = clazz.getAnnotation(Document.class);
        String indexName = document.indexName();

        if(!StringUtils.hasText(indexName))
            indexName = StringUtils.addUnderscores(clazz.getSimpleName());

        String prefix = Config.getJkesProperties().getClientId();
        if(StringUtils.hasText(prefix)) {
            return prefix + "_" + indexName;
        }

        return indexName;
    }

    public static String getTypeName(Class<?> clazz) {
        Document document = clazz.getAnnotation(Document.class);
        String type = document.type();
        if(!StringUtils.hasText(type))
            type = StringUtils.addUnderscores(clazz.getSimpleName());

        return type;
    }

    public static String getAlias(Class<?> clazz) {
        Document document = clazz.getAnnotation(Document.class);
        String alias = document.alias();
        if(!StringUtils.hasText(alias))
            alias = StringUtils.addUnderscores(clazz.getSimpleName()) + "_alias";

        String prefix = Config.getJkesProperties().getClientId();
        if(StringUtils.hasText(prefix)) {
            return prefix + "_" + alias;
        }

        return alias;
    }

    public static String getFieldName(Method method) {
        Field field = method.getAnnotation(Field.class);
        if (field != null && StringUtils.hasLength(field.name())) {
            return StringUtils.addUnderscores(StringUtils.trimAllWhitespace(field.name()));
        }

        MultiFields multiFields = method.getAnnotation(MultiFields.class);
        if(multiFields != null) {
            Field mainField = multiFields.mainField();
            if(StringUtils.hasText(mainField.name()))
                return StringUtils.addUnderscores(StringUtils.trimAllWhitespace(mainField.name()));
        }

        String methodName = method.getName();
        if(methodName.startsWith("is")) {
            return StringUtils.addUnderscores(methodName.substring(2));
        }else if(methodName.startsWith("get")) {
            return StringUtils.addUnderscores(method.getName().substring(3));
        }else {
            throw new IllegalAnnotatedFieldException(Field.class.getCanonicalName() +
                    " can only be annotated on getter method or isXXX method, " + "can't be annotated on method on ");
        }

    }

    public static String getVersionField(Class<?> clazz) {
        Method[] methods = clazz.getMethods();
        for(Method method : methods) {
            if(method.isAnnotationPresent(Field.class) && method.isAnnotationPresent(Version.class)) {
                return getFieldName(method);
            }
        }

        return null;
    }

}

@@ -86,58 +86,8 @@
 nt;%0A
-import com.timeyang.jkes.core.annotation.Version;%0A
 impo
@@ -270,16 +270,178 @@
 eption;%0A
+import com.timeyang.jkes.core.metadata.DocumentMetadata;%0Aimport com.timeyang.jkes.core.metadata.Metadata;%0Aimport com.timeyang.jkes.core.metadata.VersionMetadata;%0A
 import c
@@ -3059,257 +3059,274 @@
     
-Method%5B%5D methods = clazz.getMethods();%0A        for(Method method : methods) %7B%0A            if(method.isAnnotationPresent(Field.class) && method.isAnnotationPresent(Version.class)) %7B%0A                return getFieldName(method);%0A            %7D%0A        %7D
+DocumentMetadata documentMetadata = Metadata.getMetadata().getMetadataMap().get(clazz);%0A        VersionMetadata versionMetadata = documentMetadata.getVersionMetadata();%0A        if(versionMetadata != null)%0A            return getFieldName(versionMetadata.getMethod());
 %0A%0A  

update version field to use metadata