package com.wasu.cs.mvp.presenter;

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

import org.json.JSONObject;

import java.util.List;

/**
 * @Description: 点播屏数据操作Presenter
 *
 * @Author: Danxingxi
 *
 * @CreateDate: 2016/9/27 15:59
 */
public class MainDemandPagePresenter extends BasePresenter<IDemandMvpView> {
    /**
     * 获取推荐屏总体数据
     * @param url
     */
    public void getDemandPageData(final String url){
        DataFetchModule.getInstance().fetchJsonGet(url, new DataFetchListener.JsonListener() {
            @Override
            public void onJsonGet(int i, String s, JSONObject jsonObject) {
                if(MainDemandPagePresenter.this.getMvpView() == null){
                    return;
                }
                if(jsonObject == null){
                    MainDemandPagePresenter.this.getMvpView().getDataError(new Throwable("Demand data fetch error:"+i+"  "+s));
                    return;
                }
                MainHomeDataModel homeDataDataModel = new MainHomeDataModel();
                homeDataDataModel.parseJson(jsonObject);
                if(homeDataDataModel!=null){
                    /***依次获取四排数据***/
                    if(homeDataDataModel.getBannerList()!=null && homeDataDataModel.getBannerList().size()>0){
                        getBodyList1Data("所有分类", homeDataDataModel.getBannerList());
                    }
                    if(homeDataDataModel.getBannerList() !=null && homeDataDataModel.getBodyList().size()>0){
                        int bodyCount = homeDataDataModel.getBodyList().size();
                        for(int j=0;j<bodyCount;j++){
                            switch (j){
                                case 0:
                                    String url2 = homeDataDataModel.getBodyList().get(0).getJsonUrl();
                                    String title2 = homeDataDataModel.getBodyList().get(0).getTitle();
                                    getBodyList2Data(title2, url2);
                                    break;
                                case 1:
                                    String url3 = homeDataDataModel.getBodyList().get(1).getJsonUrl();
                                    String title3 = homeDataDataModel.getBodyList().get(1).getTitle();
                                    getBodyList3Data(title3, url3);
                                    break;
                                case 2:
                                    String url4 = homeDataDataModel.getBodyList().get(2).getJsonUrl();
                                    String title4 = homeDataDataModel.getBodyList().get(2).getTitle();
                                    getBodyList4Data(title4, url4);
                                    break;
                            }
                        }
                    }

                }else{
                    MainDemandPagePresenter.this.getMvpView().getDataError(new Throwable("Demand data fetch error:"+i+"  "+s));
                }
            }
        });
    }

    /**
     * 获取品牌专区数据
     */
    public void getBodyList1Data(String rowTitle, List<AssetsDataModel> data){
        if(MainDemandPagePresenter.this.getMvpView() == null){
            return;
        }
        if(data == null){
            MainDemandPagePresenter.this.getMvpView().getDataError(new Throwable("Demand 1 data fetch error"));
            return;
        }
        if(data.size()>0){
            MainDemandPagePresenter.this.getMvpView().getBody1DataSuccess(rowTitle, data);
        }else{
            MainDemandPagePresenter.this.getMvpView().getDataError(new Throwable("Demand 1 data fetch error"));
        }
    }

    /**
     * 获取会员专区数据
     * @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(MainDemandPagePresenter.this.getMvpView() == null){
                    return;
                }
                if(jsonObject == null){
                    MainDemandPagePresenter.this.getMvpView().getDataError(new Throwable("Demand data fetch error:"+i+"  "+s));
                    return;
                }
                BodyListDataModel bodyListDataModel =  new BodyListDataModel();
                bodyListDataModel.parseJson(jsonObject);
                if(bodyListDataModel!=null && bodyListDataModel.getBodyList().size()>0){
                    MainDemandPagePresenter.this.getMvpView().getBody2DataSuccess(rowTitle, bodyListDataModel.getBodyList());
                }else{
                    MainDemandPagePresenter.this.getMvpView().getDataError(new Throwable("Demand 2 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(MainDemandPagePresenter.this.getMvpView() == null){
                    return;
                }
                if(jsonObject == null){
                    MainDemandPagePresenter.this.getMvpView().getDataError(new Throwable("Demand data fetch error:"+i+"  "+s));
                    return;
                }
                BodyListDataModel bodyListDataModel =  new BodyListDataModel();
                bodyListDataModel.parseJson(jsonObject);
                if(bodyListDataModel!=null && bodyListDataModel.getBodyList().size()>0){
                    MainDemandPagePresenter.this.getMvpView().getBody3DataSuccess(rowTitle, bodyListDataModel.getBodyList());
                }else{
                    MainDemandPagePresenter.this.getMvpView().getDataError(new Throwable("Demand 3 data fetch error:"+i+"  "+s));
                }
            }
        });
    }

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

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