package com.wasu.cs.mvp.presenter;

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

import org.json.JSONObject;

/**
 * @Description:首页推荐屏Presenter
 *
 * @Author: Danxingxi
 *
 * @CreateDate: 2016/9/26 9:30
 */
public class MainRecomPagePresenter extends BasePresenter<IRecomMvpView> {

    /**
     * body data数据获取重试次数
     */
    private int tryCount0,tryCount1,tryCount2;

    /**
     * 获取推荐屏总体数据
     * @param url
     */
    public void getRecomPageData(String url){
        DataFetchModule.getInstance().fetchJsonGet(url, new DataFetchListener.JsonListener() {
            @Override
            public void onJsonGet(int i, String s, JSONObject jsonObject) {
            if(MainRecomPagePresenter.this.getMvpView() == null){
                return;
            }
            if(jsonObject == null){
                MainRecomPagePresenter.this.getMvpView().getDataError(new Throwable("Recom data fetch error:"+i+"  "+s));
                return;
            }

            MainHomeDataModel homeDataModel = new MainHomeDataModel();
            homeDataModel.parseJson(jsonObject);

            if(homeDataModel!=null){
                if(homeDataModel.getBannerMiddle()!=null && homeDataModel.getBannerMiddle().size()>0){
                    getMvpView().getBannerMiddleSuccess(homeDataModel.getBannerMiddle());
                }
                if(homeDataModel.getBannerLeft()!=null && homeDataModel.getBannerLeft().size()>0){
                    getMvpView().getBannerLeftSuccess(homeDataModel.getBannerLeft());
                }
                if(homeDataModel.getBannerRight()!=null && homeDataModel.getBannerRight().size()>0){
                    getMvpView().getBannerRightSuccess(homeDataModel.getBannerRight());
                }
                int bodyCount = homeDataModel.getBodyList().size();
                if(bodyCount>0){
                    for(int j=0;j<bodyCount;j++){
                        if(0==j)
                        getBodyList1Data(homeDataModel.getBodyList().get(0).getTitle(),homeDataModel.getBodyList().get(0).getJsonUrl());
                        if(1==j)
                        getBodyList2Data(homeDataModel.getBodyList().get(1).getTitle(),homeDataModel.getBodyList().get(1).getJsonUrl());
                        if(2==j)
                        getBodyList3Data(homeDataModel.getBodyList().get(2).getTitle(),homeDataModel.getBodyList().get(2).getJsonUrl());
                    }
                }else{
                    getMvpView().getDataError(new Throwable("Recom body data fetch error:"+i+"  "+s));
                }
            }else{
                getMvpView().getDataError(new Throwable("Recom data fetch error:"+i+"  "+s));
            }
            }
        });
    }

    /**
     * 获取品牌专区数据
     * @param url
     */
    public void getBodyList1Data(final String rowTitle, String url){
        DataFetchModule.getInstance().fetchJsonGet(url, new DataFetchListener.JsonListener() {
            @Override
            public void onJsonGet(int i, String s, JSONObject jsonObject) {
                if(MainRecomPagePresenter.this.getMvpView() == null){
                    return;
                }
                if(jsonObject == null){
                    getMvpView().getDataError(new Throwable("Recom data fetch error:"+i+"  "+s));
                    return;
                }
                BodyListDataModel bodyListDataModel = new BodyListDataModel();
                bodyListDataModel.parseJson(jsonObject);
                if(bodyListDataModel!=null && bodyListDataModel.getBodyList().size()>0){
                    MainRecomPagePresenter.this.getMvpView().getBody1DataSuccess(rowTitle, bodyListDataModel.getBodyList());
                }else{
                    MainRecomPagePresenter.this.getMvpView().getDataError(new Throwable("Recom data fetch error:"+i+"  "+s));
                }
            }
        });
    }

    /**
     * 获取会员专区数据
     * @param url
     */
    public void getBodyList2Data(final String rowTitle, String url){
        DataFetchModule.getInstance().fetchJsonGet(url, new DataFetchListener.JsonListener() {
            @Override
            public void onJsonGet(int i, String s, JSONObject jsonObject) {
                if(MainRecomPagePresenter.this.getMvpView() == null){
                    return;
                }
                if(jsonObject == null){
                    getMvpView().getDataError(new Throwable("Recom data fetch error:"+i+"  "+s));
                    return;
                }
                BodyListDataModel bodyListDataModel = new BodyListDataModel();
                bodyListDataModel.parseJson(jsonObject);
                if(bodyListDataModel!=null && bodyListDataModel.getBodyList().size()>0){
                    MainRecomPagePresenter.this.getMvpView().getBody2DataSuccess(rowTitle, bodyListDataModel.getBodyList());
                }else{
                    MainRecomPagePresenter.this.getMvpView().getDataError(new Throwable("Recom data fetch error:"+i+"  "+s));
                }
            }
        });
    }

    /**
     * 获取更多惊喜数据
     * @param url
     */
    public void getBodyList3Data(final String rowTitle, String url){
        DataFetchModule.getInstance().fetchJsonGet(url, new DataFetchListener.JsonListener() {
            @Override
            public void onJsonGet(int i, String s, JSONObject jsonObject) {
            if(MainRecomPagePresenter.this.getMvpView() == null){
                return;
            }
            if(jsonObject == null){
                getMvpView().getDataError(new Throwable("Recom data fetch error:"+i+"  "+s));
                return;
            }
            BodyListDataModel bodyListDataModel = new BodyListDataModel();
            bodyListDataModel.parseJson(jsonObject);
            if(bodyListDataModel!=null && bodyListDataModel.getBodyList().size()>0){
                MainRecomPagePresenter.this.getMvpView().getBody3DataSuccess(rowTitle, bodyListDataModel.getBodyList());
            }else{
                MainRecomPagePresenter.this.getMvpView().getDataError(new Throwable("Recom data fetch error:"+i+"  "+s));
            }
            }
        });
    }

    @Override
    public void detachView() {
        super.detachView();
    }
}
