package cn.xishan.oftenporter.oftendb.db;

import cn.xishan.oftenporter.porter.core.base.AppValues;
import cn.xishan.oftenporter.porter.core.util.WPTool;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用来说明哪些字段有效或无效。
 *
 * @author ZhuiFeng
 */
public class NameValues implements AppValues {

    public interface Foreach {
        /**
         * @param name
         * @param value
         * @return 返回false表示中断循环.
         */
        public boolean forEach(String name, Object value);
    }

    // private Map<String, Object> map;
    private List<String> names;
    private List<Object> values;
    private boolean filterNullAndEmpty = false;
    private Set<String> filterNullKeys;

    public NameValues() {
        names = new ArrayList<>();
        values = new ArrayList<>();
    }

    public NameValues(int capacity) {
        names = new ArrayList<String>(capacity);
        values = new ArrayList<Object>(capacity);
    }

    /**
     * 设置或清除待过滤的空属性.
     * @param filterNullKeys
     */
    public NameValues setFilterNullKeys(String... filterNullKeys) {
        if(filterNullKeys.length==0){
            this.filterNullKeys=null;
        }else{
            Set<String> set = new HashSet<>();
            for(String s:filterNullKeys){
                set.add(s);
            }
        }
        return this;
    }

    /**
     * 是否过滤掉值为null或""的元素,默认为false,请在{@linkplain #append(String, Object)}之前调用。
     *
     * @param filterNullAndEmpty
     * @return
     */
    public NameValues filterNullAndEmpty(boolean filterNullAndEmpty) {
        this.filterNullAndEmpty = filterNullAndEmpty;
        return this;
    }

    /**
     * 添加键值对。
     *
     * @param name
     * @param value
     * @return
     */
    public NameValues append(String name, Object value) {
        if (!(filterNullAndEmpty&&(filterNullKeys==null||filterNullKeys.contains(name))) || WPTool.notNullAndEmpty(value)) {
            names.add(name);
            values.add(value);
        }
        return this;
    }


    public String[] names() {
        return names.toArray(new String[0]);
    }

    public Object[] values() {
        return values.toArray(new Object[0]);
    }

    public void forEach(Foreach foreach) {
        for (int i = 0; i < names.size(); i++) {
            if (!foreach.forEach(names.get(i), values.get(i))) {
                break;
            }
        }

    }

    public void clear() {
        names.clear();
        values.clear();
    }

    public JSONObject toJSON() {
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < names.size(); i++) {
            jsonObject.put(names.get(i), values.get(i));
        }
        return jsonObject;
    }

    public Object value(int index) {
        return values.get(index);
    }

    public String name(int index) {
        return names.get(index);
    }

    public int size() {
        return names.size();
    }

    @Override
    public String[] getNames() {
        return names();
    }

    @Override
    public Object[] getValues() {
        return values();
    }

}
@@ -877,32 +877,135 @@
 ist%3C%3E();%0A    %7D%0A%0A
+    public NameValues(String name, Object value) %7B%0A        this();%0A        append(name, value);%0A    %7D%0A%0A
     public NameV
@@ -1022,24 +1022,24 @@
 capacity) %7B%0A
-
         name
@@ -1161,16 +1161,23 @@
 %E8%BF%87%E6%BB%A4%E7%9A%84%E7%A9%BA%E5%B1%9E%E6%80%A7.%0A
+     *%0A
      * @
@@ -1283,16 +1283,17 @@
       if
+ 
 (filterN
@@ -1310,12 +1310,15 @@
 ngth
+ 
 ==
+ 
 0)
+ 
 %7B%0A  
@@ -1346,17 +1346,19 @@
 NullKeys
-=
+ = 
 null;%0A  
@@ -1368,12 +1368,14 @@
    %7D
+ 
 else
+ 
 %7B%0A  
@@ -1434,16 +1434,17 @@
      for
+ 
 (String 
@@ -1448,9 +1448,11 @@
 ng s
-:
+ : 
 filt
@@ -1462,16 +1462,17 @@
 ullKeys)
+ 
 %7B%0A      
@@ -2032,10 +2032,12 @@
 mpty
+ 
 &&
+ 
 (fil
@@ -2051,16 +2051,20 @@
 Keys
+ 
 ==
+ 
 null
+ 
 %7C%7C
+ 
 filt

修复DefaultParamSource的bug，完善CheckHandle