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 Subject extends ObjectBase {
	
	private static final long serialVersionUID = 1L;

	private static final String TAG = Subject.class.getSimpleName();
	
	private int currentPage;
	private int pageNum;
	private int pageSize;
	private int total;
	
	private List<SubjectItem> subjects = null;
	
	public int getCurrentPage() {
		return currentPage;
	}
	
	public int getPageNum() {
		return pageNum;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public List<SubjectItem> getSubjects() {
		return subjects;
	}

	public int getTotal() {
		return total;
	}

	@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"));
			}
			
			subjects = new ArrayList<SubjectItem>();
			JSONObject data = root.optJSONObject("data");
			JSONArray list = data.optJSONArray("elements");
			
			this.currentPage = data.optInt("currentPage");
			this.pageNum = data.optInt("pageNum");
//			this.pageSize = data.optInt("pageSize");
			
			this.pageSize = Common.DEFAULT_PAGE_SIZE;
			this.total = data.optInt("total");
			
			int length = list.length();
			for (int i = 0; i < length; i++) {
				JSONObject item = list.optJSONObject(i);
				
				SubjectItem obj = new SubjectItem();
				obj.setName(item.optString("title"));
				obj.setLayoutCode(item.optString("layout"));
				obj.setJsonUrl(item.optString("jsonUrl"));
				obj.setPicUrl(item.optString("content"));
				obj.setLinkUrl(item.optString("linkUrl"));
				
				subjects.add(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataFetchException(WError.ERROR_INVALID_DATA, null);
		}
	}
	
	public class SubjectItem implements Serializable {
		
		private static final long serialVersionUID = 1L;
		
		private String name;
		private String layoutCode;
		private String picUrl;
		private String jsonUrl;
		private String linkUrl;

		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 getPicUrl() {
			return picUrl;
		}

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

		public String getName() {
			return name;
		}

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

		public String getLinkUrl() {
			return linkUrl;
		}

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

}
