package com.wasu.cs.model;

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

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

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

/**
 * 电影推荐
 * @author zhangrm
 *
 */
public class Recommend extends ObjectBase {

	/**
     */
    private static final long serialVersionUID = 1L;
    private int code;
	private List<RecommendData> data = new ArrayList<RecommendData>();

	public List<RecommendData> getData() {
		return data;
	}

	@Override
	public void createFromResponse(String arg0) throws DataFetchException {
		try {
			JSONObject o = new JSONObject(arg0);

			code = o.optInt("code");

			if (code != 200) {
				return;
			}

			JSONArray dataArray = o.optJSONArray("data");

			if (dataArray == null) {
				return;
			}

			int size = dataArray.length();
			for (int i = 0; i < size; i++) {
				JSONObject item = dataArray.optJSONObject(i);

				RecommendData recommendItem = new RecommendData();
				recommendItem.setCatId(item.optString("catId"));
				recommendItem.setCmark(item.optString("cmark"));
				recommendItem.setId(item.optString("id"));
				recommendItem.setJsonUrl(item.optString("jsonUrl"));
				recommendItem.setLayout(item.optString("layout"));
				recommendItem.setPicUrl(item.optString("picUrl"));
				recommendItem.setSummary(item.optString("summary"));
				recommendItem.setTitle(item.optString("title"));
				recommendItem.setTraceid(item.optString("traceid"));
				recommendItem.setPoints(item.optString("points"));

				data.add(recommendItem);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			throw new DataFetchException(WError.ERROR_INVALID_DATA, "json解析出错");
		}
	}

	public static class RecommendData {
		private String catId = "";
		private String cmark = "";
		private String id = "";
		private String jsonUrl = "";
		private String layout = "";
		private String picUrl = "";
		private String summary = "";
		private String title = "";
		private String traceid = "";
		private String points = "";

		public RecommendData(){}

		public RecommendData(String catId, String cmark, String id, String jsonUrl, String layout, String picUrl, String summary, String title, String traceid, String points) {
			this.catId = catId;
			this.cmark = cmark;
			this.id = id;
			this.jsonUrl = jsonUrl;
			this.layout = layout;
			this.picUrl = picUrl;
			this.summary = summary;
			this.title = title;
			this.traceid = traceid;
			this.points = points;
		}

		public RecommendData(String picUrl, String title, String points) {
			this.picUrl = picUrl;
			this.title = title;
			this.points = points;
		}

		public String getCatId() {
			return catId;
		}

		public void setCatId(String catId) {
			this.catId = catId;
		}

		public String getCmark() {
			return cmark;
		}

		public void setCmark(String cmark) {
			this.cmark = cmark;
		}

		public String getId() {
			return id;
		}

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

		public String getJsonUrl() {
			return jsonUrl;
		}

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

		public String getLayout() {
			return layout;
		}

		public void setLayout(String layout) {
			this.layout = layout;
		}

		public String getPicUrl() {
			return picUrl;
		}

		public void setPicUrl(String picUrl) {
			this.picUrl = picUrl;
		}

		public String getSummary() {
			return summary;
		}

		public void setSummary(String summary) {
			this.summary = summary;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getTraceid() {
			return traceid;
		}

		public void setTraceid(String traceid) {
			this.traceid = traceid;
		}

		public String getPoints() {
			if (points == null || points.equalsIgnoreCase("null")) {
				points = "";
			}
			return points;
		}

		public void setPoints(String points) {
			this.points = points;
		}

	}

}