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

	private static final String TAG = Movie.class.getSimpleName();
	
	private int currentPage;
	private int pageNum;
	private int pageSize;
	private int total;
	
	private List<MovieItem> movies = null;
	
	public int getCurrentPage() {
		return currentPage;
	}
	
	public int getPageNum() {
		return pageNum;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public List<MovieItem> getMovies() {
		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) {
				throw new DataFetchException(WError.ERROR_INVALID_DATA, root.optString("message"));
			}
			
			movies = new ArrayList<MovieItem>();
			JSONObject data = root.optJSONObject("data");
			if (data == null /** 筛选的结果是数组 **/){
				JSONArray tmp = root.optJSONArray("data");
				if (tmp == null || tmp.length() == 0){
					return;
				}
				data = tmp.optJSONObject(0); //暂时取第一个
			}
			JSONArray list = data.optJSONArray("assets");
			
			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");
			
			if (list == null){
				return;
			}
			
			int length = list.length();
			for (int i = 0; i < length; i++) {
				JSONObject item = list.optJSONObject(i);
				
				MovieItem obj = new MovieItem();
				obj.setId(item.optInt("id"));
				obj.setJsonUrl(item.optString("jsonUrl"));
				obj.setLayoutCode(item.optString("layout"));
				obj.setPicUrl(item.optString("picUrl"));
				obj.setTitle(item.optString("title"));
				obj.setYear(item.optString("datetime"));
				
				movies.add(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataFetchException(WError.ERROR_INVALID_DATA, null);
		}
	}
	
	public class MovieItem 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;
		}
	}

}
