package com.wasu.cs.mvp.presenter;

import android.os.Handler;
import android.util.SparseArray;

import com.wasu.cs.model.CatData;
import com.wasu.cs.model.DemandProgram;
import com.wasu.cs.module.WasuCacheModule;
import com.wasu.cs.mvp.IView.INeteaseNewsView;
import com.wasu.cs.mvp.model.NeteaseNewsModel;
import com.wasu.cs.protocol.CatProtocol;
import com.wasu.cs.utils.JsonUtil;
import com.wasu.module.datafetch.DataFetchListener;
import com.wasu.module.datafetch.DataFetchModule;
import com.wasu.module.datafetch.ObjectBase;

import org.json.JSONObject;

/**
 * Created by chenming on 2016/8/1.
 *
 * @author chenming
 * @Description: 网易新闻cs模板的Presenter
 * @email chenming@wasu.com
 * @date ${date}${time}
 */

public class NeteaseNewsPresenter extends BasePresenter<INeteaseNewsView> {
	private static final String TAG = NeteaseNewsPresenter.class.getSimpleName();
	private NeteaseNewsModel model;
	private CatProtocol	mCatProtocol;
	private Handler handler;
	private SparseArray<String> jsonurls;


	/**
	 * 获取网易新闻数据
	 */
	public void getNeteaseNewsData(String url) {

		DataFetchModule.getInstance().fetchJsonGet(url, new DataFetchListener.JsonListener() {
			@Override
			public void onJsonGet(int i, String s, JSONObject jsonObject) {
				//该回调方法可能会被回调2次，加入判断防止第二次回调数据被制 空
				if (model == null && jsonObject != null) {
					model = JsonUtil.fromJson(jsonObject.toString(), NeteaseNewsModel.class);
					if (model != null) {
						if (NeteaseNewsPresenter.this.getMvpView() != null) {
							if (model.getData().getBody().getPlayWin().getJsonUrl()!=null){
								fetchRecVideoInfo(model.getData().getBody().getPlayWin().getJsonUrl());
							}else {
								NeteaseNewsPresenter.this.getMvpView().onGetRecVideoUrl(null,null);
							}
							fetchData(model);
						}
					} else {
						NeteaseNewsPresenter.this.getMvpView().ongetDataFailed(new Throwable("netease news Data fetch Failed"));
					}
				} /*else {
					NeteaseNewsPresenter.this.getMvpView().ongetDataFailed(new Throwable("Data fetch Failed"));
				}*/
			}
		});
	}

	/**
	 * get catdata and callback to caller
	 * @param
	 * @param index
	 */
	public void getCatData(final int index){
		final String url = jsonurls.get(index);
		mCatProtocol.withPageSize(50).withUrl(url);
		mCatProtocol.fetchData(handler, mCatProtocol.getRequestUrl(), new CatProtocol.CatFetchCallback() {
			@Override
			public void onResult(boolean successed, CatData catData) {
				if (successed && catData != null) {

					NeteaseNewsPresenter.this.getMvpView().onGetCatData(catData,index);
					if (index == 0){//cache the first tab catdata
						WasuCacheModule.getInstance().remove(url);
						WasuCacheModule.getInstance().put(url ,catData);
					}
				}
			}
		});
	}

	/**
	 * 获取缓存数据，如果没有缓存，则自动请求新数据
	 */
	public void getCacheData(){
		CatData cacheCatdata= (CatData) WasuCacheModule.getInstance().getAsObject(jsonurls.get(0));
		if (cacheCatdata!=null){
			NeteaseNewsPresenter.this.getMvpView().onGetCatData(cacheCatdata,0);
		}
		getCatData(0);
	}


	/**
	 * get rec video data
	 * @param url
	 */
	private void fetchRecVideoInfo(final String url){
		DataFetchModule.getInstance().fetchObjectGet(url, DemandProgram.class, new DataFetchListener.ObjectListener() {
			@Override
			public void onObjectGet(int i, String s, ObjectBase objectBase) {
				if (objectBase != null) {
					DemandProgram demandProgram = (DemandProgram) objectBase;
					if (demandProgram != null) {
						NeteaseNewsPresenter.this.getMvpView().onGetRecVideoUrl(url,demandProgram);
					}
				}
			}
		});

	}
	private void fetchData(NeteaseNewsModel model) {
		jsonurls = new SparseArray<>();
		final SparseArray<String> titles=new SparseArray<>();
		mCatProtocol= new CatProtocol();
		mCatProtocol.withPageSize(50);//分页加载50
		handler= new Handler();
		NeteaseNewsModel.DataBean.BodyBean bodyBean = model.getData().getBody();
		final int columeCount = bodyBean.getList().size();
		for (int i = 0; i < columeCount; i++) {
			jsonurls.put(i, bodyBean.getList().get(i).getJsonUrl());
			titles.put(i,bodyBean.getList().get(i).getTitle());
		}
		NeteaseNewsPresenter.this.getMvpView().onGetJsonUrls(jsonurls);
		NeteaseNewsPresenter.this.getMvpView().onGetTabTitle(titles);
	}



	@Override
	public void detachView() {
		super.detachView();
		if (model != null) {
			model = null;
		}
	}
}
