package com.wasu.cs.model;

import java.util.ArrayList;

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

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

public class DemandRecommand extends ObjectBase {
	private static final long serialVersionUID = 5454449627069444362L;
	ArrayList<RecommendItem> recommendItems = new ArrayList<RecommendItem>();

	@Override
	public void createFromResponse(String response) throws DataFetchException {
		try {
			JSONObject JsonData = new JSONObject(response);
			if (JsonData.getInt("code") != 200) {
				throw new DataFetchException(WError.ERROR_INVALID_DATA,
						JsonData.getString("message"));
			}
			JSONArray jsonArray = JsonData.getJSONArray("data");
			int len = jsonArray.length();
			recommendItems.clear();
			for (int i = 0; i < len; i++) {
				recommendItems
						.add(new RecommendItem(jsonArray.getJSONObject(i)));
			}
		} catch (JSONException e) {
			e.printStackTrace();
			throw new DataFetchException(WError.ERROR_INVALID_DATA,"json解析出错");
		}
	}
	public ArrayList<RecommendItem> getRecommendItems(){
		return recommendItems;
	}
	public class RecommendItem {
		private String id;//资产（节目）ID资产
		private String img_id;
		private String jsonUrl;//资产（节目）请求详情内容地址
		private String layout;//目标页面模板
		private String picUrl;//资产（节目）图片
		private String summary;//资产（节目）看点
		private String title;//资产（节目）标题
		private String traceid;

		public RecommendItem(JSONObject obj) {
			id = obj.optString("id", "");
			img_id = obj.optString("img_id", "");
			jsonUrl = obj.optString("jsonUrl", "");
			layout = obj.optString("layout", "");
			picUrl = obj.optString("picUrl", "");
			summary = obj.optString("summary", "");
			title = obj.optString("title", "");
			traceid=obj.optString("traceid","");
		}

		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 String getTraceid(){
			return traceid;
		}
	}
}
