package com.wasu.cs.mvp.presenter;

import com.wasu.cs.mvp.IView.ILifeMvpView;
import com.wasu.cs.mvp.model.BodyListDataModel;
import com.wasu.module.datafetch.DataFetchListener;
import com.wasu.module.datafetch.DataFetchModule;

import org.json.JSONObject;

/**
 * @Description: 生活屏数据解析类
 * @Author: Danxingxi
 * @CreateDate: 2016/12/1 9:41
 */
public class MainLifePagePresenter extends BasePresenter<ILifeMvpView> {
    /**
     * 获取首页数据
     * @param url
     */
    public void getLifePageData(String url){
        DataFetchModule.getInstance().fetchJsonGet(url, new DataFetchListener.JsonListener() {
            @Override
            public void onJsonGet(int i, String s, JSONObject jsonObject) {
                if(getMvpView() == null){
                    return;
                }
                if(jsonObject == null){
                    getMvpView().getDataError(i, s);
                    return;
                }
                BodyListDataModel bodyListDataModel = new BodyListDataModel();
                bodyListDataModel.parseJson(jsonObject);
                if(bodyListDataModel!=null && bodyListDataModel.getBlocksList()!=null){
                    //获取第一行第二行的数据
                    int count = bodyListDataModel.getBlocksList().size();
                    for(int j=0;j<count;j++){
                        switch (j){
                            case 0:
                                getData1(bodyListDataModel.getBlocksList().get(0).getJsonUrl());
                                break;
                            case 1:
                                getData2(bodyListDataModel.getBlocksList().get(1).getJsonUrl());
                                break;
                        }
                    }
                }else{
                    if(getMvpView()!=null){
                        getMvpView().getDataError(i, "生活屏数据获取错误");
                    }
                }


            }
        });
    }

    /**
     *  第一排数据
     */
    private void getData1(String url1){
        DataFetchModule.getInstance().fetchJsonGet(url1, new DataFetchListener.JsonListener() {
            @Override
            public void onJsonGet(int i, String s, JSONObject jsonObject) {
                if(getMvpView() == null){
                    return;
                }
                if(jsonObject == null){
                    getMvpView().getDataError(i, s);
                    return;
                }
                BodyListDataModel data1 = new BodyListDataModel();
                data1.parseJson(jsonObject);
                if(data1.getChannelsWin() != null){
                    getMvpView().getLoopDataSuccess(data1.getChannelsWin());
                }
                if(data1.getLinkImagesList() !=null && data1.getLinkImagesList().size()>0){
                    getMvpView().getDataSuccess1(data1.getLinkImagesList());
                }

            }
        });
    }

    /**
     * 第二排数据
     */
    private void getData2(String url2){
        DataFetchModule.getInstance().fetchJsonGet(url2, new DataFetchListener.JsonListener() {
            @Override
            public void onJsonGet(int i, String s, JSONObject jsonObject) {

                if(jsonObject == null && getMvpView()!=null){
                    getMvpView().getDataError(i, s);
                    return;
                }
                BodyListDataModel data2 = new BodyListDataModel();
                data2.parseJson(jsonObject);
                if(data2.getLinkImagesList()!=null && data2.getLinkImagesList().size()>0){
                    getMvpView().getDataSuccess2(data2.getLinkImagesList());
                }

            }
        });
    }

}
