package com.wasu.cs.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.wasu.module.datafetch.DataFetchException;
import com.wasu.module.datafetch.ObjectBase;

/**
 * 菜单筛选条件
 * 
 * @author zhangrm@wasu.com
 */
public class FilterCondition extends ObjectBase implements Serializable {

	/**
     */
    private static final long serialVersionUID = 1L;

    private final static String TAG = FilterCondition.class.getSimpleName();
	
	/**
	 * 筛选条件字段
	 */
	public final static String FITER_FIELD = "&fvalues=";

	/**
	 * 条件集合
	 */
	private Map<String, ArrayList<Condition>> conditions;
	/**
	 * 搜索接口
	 */
	private String searchUrl;

	public Map<String, ArrayList<Condition>> getConditions() {
		return conditions;
	}

	public String getSearchUrl() {
		return searchUrl;
	}

	public void setConditions(Map<String, ArrayList<Condition>> conditions) {
		this.conditions = conditions;
	}

	public void setSearchUrl(String searchUrl) {
		this.searchUrl = searchUrl;
	}

	@Override
	public void createFromResponse(String response) throws DataFetchException {
		conditions = new HashMap<String, ArrayList<Condition>>();

		try {
			JSONObject root = new JSONObject(response);

			int code = root.optInt("code");
			if (code != 200) {
				return;
			}

			JSONObject data = root.optJSONObject("data");
			this.searchUrl = data.optString("jsonurl");
			this.searchUrl += FITER_FIELD;
			
			JSONArray filters = data.optJSONArray("filters");
			int length = filters.length();
			for (int i = 0; i < length; i++) {
				JSONObject item = filters.optJSONObject(i);
				
				String id = item.optString("id");
				String name = item.optString("name");
				if (TextUtils.isEmpty(id) || TextUtils.isEmpty(name)) {
					continue;
				}

				ArrayList<Condition> condition = new ArrayList<Condition>();
				JSONArray children = item.optJSONArray("childFilters");
				int childrenLength = children.length();
				for (int j = 0; j < childrenLength; j++) {
					JSONObject childrenItem = children.optJSONObject(j);

					Condition obj = new Condition();
					obj.setId(childrenItem.optString("id"));
					obj.setName(childrenItem.optString("name"));
					obj.setCondition(childrenItem.optString("value"));
					condition.add(obj);
				}
				conditions.put(name, condition);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 具体搜索条件
	 */
	public class Condition implements Serializable {
		/**
         */
        private static final long serialVersionUID = 1L;
        private String id;
		private String name;
		private String condition;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getCondition() {
			return condition;
		}

		public void setCondition(String condition) {
			this.condition = condition;
		}

	}

}
