package entity;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElementFactory;
import config.Config;
import org.apache.http.util.TextUtils;
import utils.CheckUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by dim on 2015/7/15.
 */
public class FieldEntity {

    private String key;
    private String type;
    private String fieldName;
    private String value;
    private String autoCreateFiledName;
    private InnerClassEntity targetClass;
    private boolean generate = true;

    public String getAutoCreateFiledName() {
        return autoCreateFiledName;
    }

    public void setAutoCreateFiledName(String autoCreateFiledName) {
        this.autoCreateFiledName = autoCreateFiledName;
    }

    public InnerClassEntity getTargetClass() {
        return targetClass;
    }

    public void setTargetClass(InnerClassEntity targetClass) {
        this.targetClass = targetClass;
    }

    public boolean isGenerate() {
        return generate;
    }

    public void setGenerate(boolean generate) {
        this.generate = generate;
    }

    public String getFieldName() {
        return fieldName;
    }

    public String getGenerateFieldName() {

        String field = CheckUtil.getInstant().handleArg(fieldName);
        if (CheckUtil.getInstant().checkKeyWord(field)) {
            return field + "X";
        } else {
            return field;
        }
    }

    public void setFieldName(String fieldName) {
        if (TextUtils.isEmpty(fieldName)) {
            return;
        }
        this.fieldName = fieldName;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getType() {
        return type;
    }

    public String getRealType() {
        if (targetClass != null) {
            return String.format(type, targetClass.getClassName());
        }
        return type;
    }
    public String getGenerType(){
        if (targetClass != null) {
            return String.format(type, targetClass.getFieldPackName());
        }
        return type;
    }

    public void setType(String type1) {

        this.type = type1;
    }

    public void checkAndSetType(String s) {

        if (CheckUtil.getInstant().checkSimpleType(type.trim())) {
            //基本类型
            if (CheckUtil.getInstant().checkSimpleType(s.trim())) {
                this.type = s;
            }
        } else {
            //实体类:
            if (targetClass != null) {
                String regex = getType().replaceAll("%s", "(\\w+)").replaceAll(".", "\\.");
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(s);
                if (matcher.find() && matcher.groupCount() > 0) {
                    String temp = matcher.group(1);
                    if (TextUtils.isEmpty(temp)) {
                        targetClass.setClassName(targetClass.getAutoCreateClassName());
                    } else {
                        targetClass.setClassName(temp);
                    }
                }
            }
        }

    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }

    public void generateFiled(PsiElementFactory mFactory, PsiClass mClass, InnerClassEntity classEntity) {

        if (generate) {

            StringBuilder filedSb = new StringBuilder();
            String filedName = getGenerateFieldName();

            if (!TextUtils.isEmpty(classEntity.getExtra())) {
                filedSb.append(classEntity.getExtra()).append("\n");
                classEntity.setExtra(null);
            }
            if (!filedName.equals(getKey()) || Config.getInstant().isUseSerializedName()) {
                filedSb.append(Config.getInstant().geFullNameAnnotation().replaceAll("\\{filed\\}", getKey()));
            }

            if (Config.getInstant().isFieldPrivateMode()) {
                filedSb.append("private  ").append(getGenerType()).append(" ").append(filedName).append(" ; ");
            } else {
                filedSb.append("public  ").append(getGenerType()).append(" ").append(filedName).append(" ; ");
            }
            mClass.add(mFactory.createFieldFromText(filedSb.toString(), mClass));
        }
    }


}

@@ -2078,21 +2078,24 @@
 ring get
-Gener
+FullName
 Type()%7B%0D
@@ -4189,37 +4189,40 @@
 e  %22).append(get
-Gener
+FullName
 Type()).append(%22
@@ -4334,21 +4334,24 @@
 pend(get
-Gener
+FullName
 Type()).

fix bug