package cn.ytxu.http_wrapper.template.expression.record.if_else;

import cn.ytxu.http_wrapper.model.BaseModel;
import cn.ytxu.http_wrapper.template.expression.util.ReflectiveUtil;

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

/**
 * if else 表达式的判断类型枚举
 */
public enum IfElseCondition {
    /**
     * boolean类型判断
     */
    Boolean("isTrue=\"", "\"", Pattern.compile("(isTrue=\")\\w+(\")")) {
        @Override
        public boolean getBoolean(BaseModel reflectModel, String methodName) {
            return ReflectiveUtil.getBoolean(reflectModel, methodName);
        }
    },
    /**
     * String类型判断
     */
    NotEmpty("isNotEmpty=\"", "\"", Pattern.compile("(isNotEmpty=\")\\w+(\")")) {
        @Override
        public boolean getBoolean(BaseModel reflectModel, String methodName) {
            String text = ReflectiveUtil.getString(reflectModel, methodName);
            if (text.trim().isEmpty()) {
                return false;
            }
            return true;
        }
    },
    /**
     * String类型判断：字符串是否一样
     */
    Equals("Equals=\"", "\"", Pattern.compile("(Equals=\")\\w+(,)\\w+(\")")) {
        @Override
        public boolean getBoolean(BaseModel reflectModel, String methodName) {
            String[] datas = methodName.split(",");
            String realMethodName = datas[0];
            String value = datas[1];
            String text = ReflectiveUtil.getString(reflectModel, realMethodName);
            return text.equals(value);
        }
    },
    /**
     * String类型判断：字符串是否一样，不需要大小写判断
     */
    EqualsIgnoreCase("EqualsIgnoreCase=\"", "\"", Pattern.compile("(EqualsIgnoreCase=\")\\w+(,)\\w+(\")")) {
        @Override
        public boolean getBoolean(BaseModel reflectModel, String methodName) {
            String[] datas = methodName.split(",");
            String realMethodName = datas[0];
            String value = datas[1];
            String text = ReflectiveUtil.getString(reflectModel, realMethodName);
            return text.equalsIgnoreCase(value);
        }
    };
//        String("字符串类型判断");


    private final String pattern_front;
    private final String pattern_end;
    private final Pattern pattern;

    IfElseCondition(String pattern_front, String pattern_end, Pattern pattern) {
        this.pattern_front = pattern_front;
        this.pattern_end = pattern_end;
        this.pattern = pattern;
    }

    public Pattern getPattern() {
        return pattern;
    }

    public String getMethodName(String startTagContent) {
        Matcher matcher = pattern.matcher(startTagContent);
        // be sure to match, but also need call matcher.find()
        matcher.find();
        String group = matcher.group();
        int methodNameStart = pattern_front.length();
        int methodNameEnd = group.length() - pattern_end.length();
        String methodName = group.substring(methodNameStart, methodNameEnd);
        return methodName;
    }

    public static IfElseCondition get(String startTagContent) {
        for (IfElseCondition condition : IfElseCondition.values()) {
            if (condition.isThisConditionType(startTagContent)) {
                return condition;
            }
        }
        throw new IllegalStateException("can not find condition, and this content is " + startTagContent);
    }

    /**
     * 是否为该判断类型
     */
    private boolean isThisConditionType(String startTagContent) {
        Matcher matcher = pattern.matcher(startTagContent);
        return matcher.find();
    }

    public abstract boolean getBoolean(BaseModel reflectModel, String methodName);

    public static Pattern[] getPatterns() {
        return Store.PATTERNS;
    }

}

class Store {
    /**
     * if else condition all pattern
     */
    static final Pattern[] PATTERNS;

    static {
        IfElseCondition[] conditions = IfElseCondition.values();
        PATTERNS = new Pattern[conditions.length];
        for (int i = 0; i < conditions.length; i++) {
            PATTERNS[i] = conditions[i].getPattern();
        }
    }

}
@@ -174,16 +174,39 @@
 eUtil;%0A%0A
+import java.util.List;%0A
 import j
@@ -958,15 +958,8 @@
 ext.
-trim().
 isEm
@@ -1053,32 +1053,633 @@
 %0A    %7D,%0A    /**%0A
+     * String%E7%B1%BB%E5%9E%8B%E5%88%A4%E6%96%AD%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E4%B8%80%E6%A0%B7%EF%BC%8C%E4%B8%8D%E9%9C%80%E8%A6%81%E5%A4%A7%E5%B0%8F%E5%86%99%E5%88%A4%E6%96%AD%0A     */%0A    ListSize(%22ListSize=%5C%22%22, %22%5C%22%22, Pattern.compile(%22(ListSize=%5C%22)%5C%5Cw+(,)%5C%5Cw+(%5C%22)%22)) %7B%0A        @Override%0A        public boolean getBoolean(BaseModel reflectModel, String methodName) %7B%0A            String%5B%5D datas = methodName.split(%22,%22);%0A            String realMethodName = datas%5B0%5D;%0A            int expectedSize = Integer.parseInt(datas%5B1%5D);%0A            List list = ReflectiveUtil.getList(reflectModel, realMethodName);%0A            int actualSize = Objects.isNull(list) ? 0 : list.size();%0A            return actualSize == expectedSize;%0A        %7D%0A    %7D,%0A    /**%0A
      * String%E7%B1%BB%E5%9E%8B%E5%88%A4

add ListSize IfElseCondition; remove trim for NotEmpty IfElseCondition;