package com.wasu.cs.model;

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

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

import cn.com.wasu.main.Common;

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

public class MovieSpecialTopic extends ObjectBase {
	
	private static final long serialVersionUID = 1L;

	private static final String TAG = MovieSpecialTopic.class.getSimpleName();
	
	private int currentPage;
	private int pageNum;
	private int pageSize;
	
	private List<SpecialTopicItem> specialTopics = null;
	
	public List<SpecialTopicItem> getSpecialTopics() {
		return specialTopics;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}
	
	public int getPageNum() {
		return pageNum;
	}
	
	public int getPageSize() {
		return pageSize;
	}

	@Override
	public void createFromResponse(String response) throws DataFetchException {
		
		try {
			JSONObject root = new JSONObject(response);
			
			
			int code = root.optInt("code");
			if (code != 200) {
				throw new DataFetchException(WError.ERROR_INVALID_DATA, root.optString("message"));
			}
			
			specialTopics = new ArrayList<SpecialTopicItem>();
			JSONObject data = root.optJSONObject("data");
			JSONArray subjects = data.optJSONArray("elements");
			
			this.currentPage = data.optInt("currentPage");
			this.pageNum = data.optInt("pageNum");
//			this.pageSize = data.optInt("pageSize");
			this.pageSize = Common.DEFAULT_PAGE_SIZE;
			
			int length = subjects.length();
			for (int i = 0; i < length; i++) {
				JSONObject item = subjects.optJSONObject(i);
				
				SpecialTopicItem obj = new SpecialTopicItem();
				obj.setJsonUrl(item.optString("jsonUrl"));
				obj.setLayoutCode(item.optString("layout"));
				obj.setLinkUrl(item.optString("linkUrl"));
				obj.setName(item.optString("title"));
				obj.setPicUrl(item.optString("content"));
				
				specialTopics.add(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataFetchException(WError.ERROR_INVALID_DATA, null);
		}
	}
	
	public class SpecialTopicItem implements Serializable {
		
		private static final long serialVersionUID = 1L;
		
		private String jsonUrl;
		private String layoutCode;
		private String linkUrl;
		private String picUrl;
		private String name;

		public String getJsonUrl() {
			return jsonUrl;
		}
		
		public void setJsonUrl(String url) {
			jsonUrl = url;
		}
		
		public String getLayoutCode() {
			return layoutCode;
		}

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

		public void setLinkUrl(String linkUrl) {
			this.linkUrl = linkUrl;
		}
		
		public String getPicUrl() {
			return picUrl;
		}

		public void setPicUrl(String url) {
			picUrl = url;
		}
		
		public String getName() {
			return name;
		}

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

}
