package com.wasu.cs.protocol;

import java.util.ArrayList;
import java.util.List;

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

import com.wasu.cs.model.FilmSpecialModel;
import com.wasu.cs.model.Model;
import com.wasu.module.datafetch.DataFetchListener.JsonListener;
import com.wasu.module.datafetch.DataFetchModule;

public class FilmSpecialProtocol extends BaseProtocol {

	private int scene;
	private int catId;
	private String catName;
	private int page;
	private int total;

	private String bgColor;
	private String bgImage;
	private String nameColor;
	private String coverPicUrl;
	private int winType;
	
	private MoreModel moreModel;
	private List<FilmSpecialModel> filmSpecialList = new ArrayList<FilmSpecialModel>();

	@Override
	public boolean from(JSONObject json) {
		boolean result = super.from(json);

		if (result) {
			json = json.optJSONObject("data");
			JSONObject jsonObj = null;
			JSONArray jsonArr = null;
			if (null != json) {
				scene = json.optInt("scene", 0);
				catId = json.optInt("catId", 0);
				catName = json.optString("catName", "");
				page = json.optInt("page", 0);
				total = json.optInt("total", 0);

				bgColor = json.optString("bgColor", "");
				bgImage = json.optString("bgImage", "");
				nameColor = json.optString("nameColor", "");
				coverPicUrl = json.optString("coverPicUrl", "");
				winType = json.optInt("winType", 0);

				jsonObj = json.optJSONObject("more");
				jsonArr = json.optJSONArray("assets");
			}

			if (jsonObj != null) {
				moreModel = new MoreModel();
				moreModel.from(jsonObj);
			}

			if (null != jsonArr && jsonArr.length() > 0) {
				FilmSpecialModel model = null;
				for (int i = 0, len = jsonArr.length(); i < len; i++) {
					model = new FilmSpecialModel();

					if (model.from(jsonArr.optJSONObject(i))) {
						filmSpecialList.add(model);
					}
				}
			}
		}

		return result;
	}
	
	public static void fetch(String url, final FilmSpecialFetchCallback callback) {
		DataFetchModule.getInstance().fetchJsonGet(url, new JsonListener() {
			
			@Override
			public void onJsonGet(int retcode, String extraMsg, JSONObject jsonObj) {
				
				if(callback == null) {
					try {
						throw new Exception();
					} catch (Exception e) {
						e.printStackTrace();
					}
					
					return;
				}
				
				if(retcode == 0) {
					FilmSpecialProtocol protocol = new FilmSpecialProtocol();
					protocol.from(jsonObj);
					callback.onResult(protocol.successed(), protocol);
				} else {
					callback.onResult(false, null);
				}
			}
		});
	}
	
	public interface FilmSpecialFetchCallback {
		public void onResult(boolean successed, FilmSpecialProtocol filmSpecialProtocol);
	}

	@SuppressWarnings("serial")
	public static class MoreModel extends Model {
		
		private String picUrl;
		private String layout;
		private String jsonUrl;

		@Override
		public boolean from(JSONObject json) {
			boolean result = super.from(json);

			if (result) {
				picUrl = json.optString("picUrl", "");
				layout = json.optString("layout", "");
				jsonUrl = json.optString("jsonUrl", "");
			}

			return result;
		}

		public String getPicUrl() {
			return picUrl;
		}

		public String getLayout() {
			return layout;
		}

		public String getJsonUrl() {
			return jsonUrl;
		}
	}

	public int getScene() {
		return scene;
	}

	public int getCatId() {
		return catId;
	}

	public String getCatName() {
		return catName;
	}

	public int getPage() {
		return page;
	}

	public int getTotal() {
		return total;
	}

	public String getBgColor() {
		return bgColor;
	}

	public String getBgImage() {
		return bgImage;
	}

	public String getNameColor() {
		return nameColor;
	}

	public MoreModel getMoreModel() {
		return moreModel;
	}

	public List<FilmSpecialModel> getFilmSpecialList() {
		return filmSpecialList;
	}

	public String getCoverPicUrl() {
		return coverPicUrl;
	}

	public int getWinType() {
		return winType;
	}

}
