package com.wasu.cs.model;

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

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

import cn.com.wasu.main.Common;

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

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

	private static final String TAG = News.class.getSimpleName();
	
	private int currentPage;
	private int pageNum;
	private int pageSize;
	private int total;
	
	private List<NewsItem> movies = null;
	
	public int getCurrentPage() {
		return currentPage;
	}
	
	public int getPageNum() {
		return pageNum;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public List<NewsItem> getNewss() {
		return movies;
	}

	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) {
				return;
			}
			
			movies = new ArrayList<NewsItem>();
			JSONObject data = root.optJSONObject("data");
			JSONArray list = data.optJSONArray("assets");
			
			this.currentPage = data.optInt("page");
//			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);
				
				NewsItem obj = new NewsItem();
				obj.setId(item.optInt("id"));
				obj.setJsonUrl(item.optString("jsonUrl"));
				obj.setLayoutCode(item.optString("layout"));
				obj.setPicUrl(item.optString("picUrl"));
				obj.setShowType(item.optInt("showType"));
				obj.setTitle(item.optString("title"));
				obj.setYear(item.optString("datetime"));
				
				movies.add(obj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public class NewsItem implements Serializable {
		
		private static final long serialVersionUID = 1L;
		
		private int id;
		private String jsonUrl;
		private String layoutCode;
		private String picUrl;
		private int showType;
		private String title;
		private String year;

		public int getId() {
			return id;
		}
		
		public void setId(int id) {
			this.id = id;
		}
		
		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 int getShowType() {
			return showType;
		}
		
		public void setShowType(int type) {
			showType = type;
		}
		
		public String getTitle() {
			return title;
		}

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

		public String getYear() {
			return year;
		}

		public void setYear(String year) {
			this.year = year;
		}
	}

}
