package com.wasu.cs.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

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

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

/**
 * 频道首页数据
 * 
 * @author zhangrm@wasu.com
 */
public class Category extends ObjectBase {

	private static final long serialVersionUID = 1L;

	private static final String TAG = Category.class.getSimpleName();

	/**
	 * 中文名称
	 */
	private String chineseName;
	/**
	 * 英文名称
	 */
	private String englishName;
	/**
	 * 菜单
	 */
	private List<Menu> menus;

	public String getChineseName() {
		return chineseName;
	}

	public String getEnglishName() {
		return englishName;
	}

	public List<Menu> getMenus() {
		return menus;
	}

	@Override
	public void createFromResponse(String response) throws DataFetchException {
		menus = new ArrayList<Menu>();
		
		try {
			JSONObject root = new JSONObject(response);
			
			int code = root.optInt("code");
			if (code != 200) {
				return;
			}
			
			JSONObject data = root.optJSONObject("data");
			JSONObject channel = data.optJSONObject("cat");
			JSONArray menu = data.optJSONArray("childCats");
			
			this.chineseName = channel.optString("name");
			this.englishName = channel.optString("engName");
			
			int length = menu.length();
			for (int i = 0; i < length; i++) {
				JSONObject item = menu.optJSONObject(i);
				
				Menu obj = new Menu();
				obj.setName(item.optString("name"));
				obj.setLayoutCode(item.optString("layout"));
				obj.setJsonUrl(item.optString("jsonUrl"));
				obj.setFilterJsonUrl(item.optString("filterUrl"));
				
				menus.add(obj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 菜单
	 */
	public class Menu implements Serializable {
		
		private static final long serialVersionUID = 1L;
		
		private String name;
		private String layoutCode;
		private String jsonUrl;
		private String filterJsonUrl;
		private int id;
		
		private FilterCondition conditon;

		public String getName() {
			return name;
		}

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

		public String getLayoutCode() {
			return layoutCode;
		}

		public void setLayoutCode(String layoutCode) {
			this.layoutCode = layoutCode;
		}

		public String getJsonUrl() {
			return jsonUrl;
		}

		public void setJsonUrl(String jsonUrl) {
			this.jsonUrl = jsonUrl;
		}

		public String getFilterJsonUrl() {
			return filterJsonUrl;
		}

		public void setFilterJsonUrl(String filterJsonUrl) {
			this.filterJsonUrl = filterJsonUrl;
		}

		public FilterCondition getConditon() {
			return conditon;
		}

		public void setConditon(FilterCondition conditon) {
			this.conditon = conditon;
		}

		public int getId() {
			return id;
		}

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

}
