package com.wasu.cs.model;

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

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

/**
 * search data
 */
public class SearchModel extends Model {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 78233680098689151L;
	
	private List<Assets> assetes = null;
	
	@Override
	public boolean from(JSONObject json) {
		if (super.from(json)) {
			JSONArray dataArr = json.optJSONArray("data");
			if (null != dataArr) {
				int length = dataArr.length();
				if (length > 0) {
					assetes = new ArrayList<Assets>();
					Assets assets = null;
					for (int i = 0; i < length; i++) {
						assets = new Assets();
						if (assets.from(dataArr.optJSONObject(i))) {
							if(assets.getTotal()==0){//服务端的异常容错处理
								continue;
							}
							assetes.add(assets);
						}
					}
					if(assetes.size()<=0){//服务端的异常容错处理
						return false;
					}
					return true;
				}
			}
		}
		return false;
	}
	
	public List<Assets> getAssetes() {
		return assetes;
	}

	public String[] getALLItemIds() {
		for (Assets assets : assetes) {
			if (assets.isAll(assets.getCatName())) {
				return assets.getmAllItemIds();
			}
		}
		return null;
	}

	public static class Assets extends Model {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 2294059669190826531L;

		private List<AssetsItem> assetsItems;
		
		private String[] mAllItemIds;
		
		private String catId;
		private String catName;
		private int total;
		private int page;
		private String layout;
		private String jsonUrl;
		
		@Override
		public boolean from(JSONObject json) {
			if (super.from(json)) {
				catId = json.optString("catId");
				catName = json.optString("catName");
				total = json.optInt("total");
				page = json.optInt("page");
				layout = json.optString("layout");
				jsonUrl = json.optString("jsonUrl");
				
				JSONArray assetArr = json.optJSONArray("assets");
				if (null != assetArr) {
					int length = assetArr.length();
					length=length>100?100:length;
					if (length > 0) {
						
						// TODO 4 test add here?!
						// XXX have any better idea
						if (isAll(catName)) {
							mAllItemIds = new String[length];
						}

						assetsItems = new ArrayList<AssetsItem>();
						AssetsItem assetsItem = null;
						for (int i = 0; i < length; i++) {
							assetsItem = new AssetsItem();
							assetsItem.from(assetArr.optJSONObject(i));

							if (isAll(catName)) {
								mAllItemIds[i] = assetsItem.getId();
							}

							assetsItems.add(assetsItem);
						}
					}
				}
				return true;
			}
			return false;
		}

		public List<AssetsItem> getAssetsItems() {
			return assetsItems;
		}

		public String getCatId() {
			return catId;
		}

		public String getCatName() {
			return catName;
		}

		public int getTotal() {
			return total;
		}
		public void setPage(int page){
			this.page=page;
		}
		public int getPage() {
			return page;
		}

		public String getLayout() {
			return layout;
		}

		public String getJsonUrl() {
			return jsonUrl;
		}

		public String[] getmAllItemIds() {
			return mAllItemIds;
		}
		
		// XXX have any better idea?!
		public boolean isAll(String catName) {
			return "全部".equals(catName);
		}
	}
	
	public static class AssetsItem extends Model {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 212382906404265017L;

		private final String TYPE_MOVIE = "movie";
		private final String TYPE_SERIES = "series";
		private final String UNION="union";
		private String id;
		private String title;
		private String assetType;
		private String assetTypeVal;
		private String layout;
		private String summary;
		private String picUrl;
		private String jsonUrl;

		@Override
		public boolean from(JSONObject json) {
			if (super.from(json)) {
				id = json.optString("id");
				title = json.optString("title");
				assetType = json.optString("assetType");
				assetTypeVal = json.optString("assetTypeVal");
				layout = json.optString("layout");
				summary = json.optString("summary");
				picUrl = json.optString("picUrl");
				jsonUrl = json.optString("jsonUrl");

				return true;
			}
			return false;
		}

		public String getId() {
			return id;
		}

		public String getTitle() {
			return title;
		}

		public String getAssetType() {
			return assetType;
		}

		public String getAssetTypeVal() {
			return assetTypeVal;
		}

		public String getLayout() {
			return layout;
		}

		public String getSummary() {
			return summary;
		}

		public String getPicUrl() {
			return picUrl;
		}

		public String getJsonUrl() {
			return jsonUrl;
		}
		
		public boolean isMovieOrSeries() {
			return TYPE_MOVIE.equalsIgnoreCase(assetType) || TYPE_SERIES.equalsIgnoreCase(assetType);
		}
		public boolean isUnion() {//合集
			return UNION.equalsIgnoreCase(assetType);
		}
		public boolean isUnknow() {
			return "".equalsIgnoreCase(assetType);
		}
	}
}
