package com.wasu.cs.model;

import java.util.ArrayList;
import java.util.List;

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

/**
 * filter data
 */
public class FilterConditionModel extends Model {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8440387881455471989L;

	private String url;
	private String recommendUrl;
	private List<Filter> filters;

	@Override
	public boolean from(JSONObject json) {
		if (super.from(json)) {
			JSONObject dataObj = json.optJSONObject("data");
			if (null != dataObj) {
				url = dataObj.optString("jsonurl");
				recommendUrl = dataObj.optString("recommendUrl");

				JSONArray filterArray = dataObj.optJSONArray("filters");
				if (null != filterArray) {
					Filter filter;
					filters = new ArrayList<Filter>();

					int length = filterArray.length();
					for (int i = 0; i < length; i++) {
						filter = new Filter();
						if (filter.from(filterArray.optJSONObject(i))) {
							filters.add(filter);
						}
					}
				}

				return true;
			}
		}
		return false;
	}

	public String getUrl() {
		return url;
	}

	public String getRecommendUrl() {
		return recommendUrl;
	}

	public List<Filter> getFilters() {
		return filters;
	}

	public static class Filter extends Model {

		/**
		 * 
		 */
		private static final long serialVersionUID = -450581475424980502L;

		private String id;
		private String name;

		private List<ChildFilter> childFilters;

		@Override
		public boolean from(JSONObject json) {
			if (super.from(json)) {
				id = json.optString("id");
				name = json.optString("name");

				JSONArray childFilterArray = json.optJSONArray("childFilters");
				if (null != childFilterArray) {
					ChildFilter childFilter;
					childFilters = new ArrayList<ChildFilter>();

					int length = childFilterArray.length();
					for (int i = 0; i < length; i++) {
						childFilter = new ChildFilter();
						if (childFilter.from(childFilterArray.optJSONObject(i))) {
							childFilters.add(childFilter);
						}
					}
				}

				return true;
			}
			return false;
		}

		public String getId() {
			return id;
		}

		public String getName() {
			return name;
		}

		public List<ChildFilter> getChildFilters() {
			return childFilters;
		}
		public String getValueByName(String name){
			int len=getChildFilters().size();
			for(int i=0;i<len;i++){
				if(name.equals(getChildFilters().get(i).getName())){
					return getChildFilters().get(i).getValue();
				}
			}
			return null;
					
		}
	}

	public static class ChildFilter extends Model {

		/**
		 * 
		 */
		private static final long serialVersionUID = -1424732368346993068L;

		private String id;
		private String name;
		private String value;

		@Override
		public boolean from(JSONObject json) {
			if (super.from(json)) {
				id = json.optString("id");
				name = json.optString("name");
				value = json.optString("value");

				return true;
			}
			return false;
		}

		public String getId() {
			return id;
		}

		public String getName() {
			return name;
		}

		public String getValue() {
			return value;
		}

	}

}
