package com.wasu.cs.model;

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

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

import com.wasu.module.log.WLog;

/**
 * recommend data
 */
public class RecommendModel extends Model {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7450402270155873281L;
	
	private List<Recommend> recommends = null;
	private List<Recommend> hotRecommends = new ArrayList<Recommend>();
	private List<Recommend> watchRecommends = new ArrayList<Recommend>();
	
	@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) {
					recommends = new ArrayList<Recommend>();
					Recommend recommend = null;
					for (int i = 0; i < length; i++) {
						recommend = new Recommend();
						recommend.from(dataArr.optJSONObject(i));
						
						recommends.add(recommend);
					}	
					classifyRecommends(recommends);
					return true;
				}
			}
		}
		return false;
	}

	private void classifyRecommends(List<Recommend> recommends) {
		if (null != recommends && !recommends.isEmpty()) {
			for (Recommend recommend : recommends) {
				// limit in 4
				if (recommend.isHotRecommend() && hotRecommends.size() < 4) {
					hotRecommends.add(recommend);
				} else if (recommend.isWatchRecommend()) {
					watchRecommends.add(recommend);
				} else {
					WLog.w("RecommendModel", "classifyRecommends() unknow recommend type: " + recommend.getRgroup());
				}
			}

			// in case of no hotRecommends
			if (hotRecommends.isEmpty()) {
				int size = recommends.size();
				for (int i = 0; i < size; i++) {
					if (i < 4) {
						hotRecommends.add(recommends.get(i));
						watchRecommends.remove(recommends.get(i));
					} else {
						if (watchRecommends.isEmpty()) {
							watchRecommends.add(recommends.get(i));
						} else {
							break;
						}
					}
				}
			}
		}
	}

	public List<Recommend> getRecommends() {
		return recommends;
	}

	public List<Recommend> getHotRecommends() {
		return hotRecommends;
	}

	public List<Recommend> getWatchRecommends() {
		return watchRecommends;
	}

	/**
	 * recommend item data
	 */
	public static class Recommend extends Model {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 2029603105105961890L;
		
		/**
		 * 最新上线
		 */
		private final int TYPE_RECOMMEND_HOT = 1;
		/**
		 * 大家都在看
		 */
		private final int TYPE_RECOMMEND_WATCH = 2;
		
		private String id;
		private String img_id;
		private String jsonUrl;
		private String layout;
		private String picUrl;
		private String summary;
		private String title;
		private String rgroupval;
		
		private int rgroup;
		
		@Override
		public boolean from(JSONObject json) {
			if (super.from(json)) {
				id = json.optString("id");
				img_id = json.optString("img_id");
				jsonUrl = json.optString("jsonUrl");
				layout = json.optString("layout");
				picUrl = json.optString("picUrl");
				summary = json.optString("summary");
				title = json.optString("title");
				rgroupval = json.optString("rgroupVal");
				rgroup = json.optInt("rgroup", 0);
			}
			return false;
		}

		public String getId() {
			return id;
		}

		public String getImgId() {
			return img_id;
		}

		public String getJsonUrl() {
			return jsonUrl;
		}

		public String getLayout() {
			return layout;
		}

		public String getPicUrl() {
			return picUrl;
		}

		public String getSummary() {
			return summary;
		}

		public String getTitle() {
			return title;
		}

		public int getRgroup() {
			return rgroup;
		}

		public String getRgroupval() {
			return rgroupval;
		}

		public boolean isHotRecommend() {
			return TYPE_RECOMMEND_HOT == rgroup;
		}
		
		public boolean isWatchRecommend() {
			return TYPE_RECOMMEND_WATCH == rgroup;
		}
	}
	
}
