package com.wasu.cs.protocol;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.wasu.cs.model.CatModel;
import com.wasu.cs.model.HomeColumnModel;
import com.wasu.module.datafetch.DataFetchListener.JsonListener;
import com.wasu.module.datafetch.DataFetchModule;
import com.wasu.util.StringUtils;

public class HomeProtocol extends BaseProtocol {
	
	private int total;
	private CatModel cat;
	private int stime;
	private String bgImage;
	private Map<String, String> cornerIconMap = new HashMap<String, String>();
	private List<HomeColumnModel> columnList = new ArrayList<HomeColumnModel>();
	
	@Override
	public boolean from(JSONObject json) {
		boolean result = super.from(json);
		
		if(result) {
			json = json.optJSONObject("data");
			JSONObject jsonObj = null;
			JSONArray jsonArr = null;
			JSONObject configObj = null;
			JSONArray marksJsonArr = null;
			if (null != json) {
				total = json.optInt("total", 0);
				bgImage = json.optString("bgImage");
				jsonObj = json.optJSONObject("cat");
				configObj = json.optJSONObject("config");
				jsonArr = json.optJSONArray("elements");
			}
			
			if(jsonObj != null) {
				cat = new CatModel();
				cat.from(jsonObj);
			}
			
			stime = 0;
			if(configObj != null) {
				stime = configObj.optInt("stime", 0);
				marksJsonArr = configObj.optJSONArray("cornerMarks");
			}
			
			if (null != marksJsonArr && marksJsonArr.length() > 0) {
				String cmark = null;
				String picUrl = null;
				for (int i = 0, len = marksJsonArr.length(); i < len; i++) {
					configObj = marksJsonArr.optJSONObject(i);
					cmark = null;
					picUrl = null;
					if(configObj != null) {
						cmark = configObj.optString("cmark", "");
						picUrl = configObj.optString("picUrl", "");
					}
					
					if(!StringUtils.isBlank(cmark) && !StringUtils.isBlank(picUrl)) {
						cornerIconMap.put(cmark, picUrl);
					}
				}
			}
			
			if(null != jsonArr && jsonArr.length() > 0) {
				HomeColumnModel model = null;
				for (int i=0, len=jsonArr.length(); i<len; i++) {
					model = new HomeColumnModel();
					
					if(model.from(jsonArr.optJSONObject(i))) {
						model.setCarouselTime(stime);
						model.setCornerIconMap(cornerIconMap);
						columnList.add(model);
					}
				}
			}
		}
		
		return result;
	}
	
	public String getBgImage() {
		return bgImage;
	}

	public void setBgImage(String bgImage) {
		this.bgImage = bgImage;
	}

	public static void fetch(String url, final HomeFetchCallback 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) {
					HomeProtocol protocol = new HomeProtocol();
					protocol.from(jsonObj);
					callback.onResult(protocol.successed(), protocol);
				} else {
					callback.onResult(false, null);
				}
			}
		});
	}
	
	public interface HomeFetchCallback {
		public void onResult(boolean successed, HomeProtocol homeProtocol);
	}

	public int getTotal() {
		return total;
	}

	public CatModel getCat() {
		return cat;
	}

	public List<HomeColumnModel> getColumnList() {
		return columnList;
	}

	public int getStime() {
		return stime;
	}

	public Map<String, String> getCornerIconMap() {
		return cornerIconMap;
	}

}
