package com.wasu.cs.mvp.presenter;

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

import org.json.JSONObject;

/**
 * Created by Danxx on 2016/6/23.
 * 首页VIP屏Presenter
 */
public class MainVipPagePresenter extends BasePresenter<IVIPMvpView> {

    /**
     * 获取首页数据
     * @param url
     */
    public void getVipPageData(String url){
        DataFetchModule.getInstance().fetchJsonGet(url, new DataFetchListener.JsonListener() {
            @Override
            public void onJsonGet(int i, String s, JSONObject jsonObject) {
                if(jsonObject == null&& getMvpView()!=null){
                    MainVipPagePresenter.this.getMvpView().getDataError(new Throwable("VIP data fetch error:"+i+"  "+s));
                    return;
                }

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

                if(mainHomeDataModel.getBannerLeft() != null){
                    if(MainVipPagePresenter.this.getMvpView() != null)
                        MainVipPagePresenter.this.getMvpView().getBannerLeftSuccess(mainHomeDataModel.getBannerLeft());

                } else {
                    if(MainVipPagePresenter.this.getMvpView() != null)
                        MainVipPagePresenter.this.getMvpView().getDataError(new Throwable("VIP BannerLeft data fetch error:" + i + "  " + s));
                }

                if(mainHomeDataModel.getBannerMiddle() != null){
                    if(MainVipPagePresenter.this.getMvpView() != null)
                        MainVipPagePresenter.this.getMvpView().getBannerMiddleSuccess(mainHomeDataModel.getBannerMiddle());
                } else {
                    if(MainVipPagePresenter.this.getMvpView() != null)
                        MainVipPagePresenter.this.getMvpView().getDataError(new Throwable("VIP BannerMiddle data fetch error:" + i + "  " + s));
                }

                if(mainHomeDataModel.getBannerRight() != null){
                    if(MainVipPagePresenter.this.getMvpView() != null)
                        MainVipPagePresenter.this.getMvpView().getBannerRightSuccess(mainHomeDataModel.getBannerRight());

                } else {
                    if(MainVipPagePresenter.this.getMvpView() != null)
                        MainVipPagePresenter.this.getMvpView().getDataError(new Throwable("VIP BannerRight data fetch error:" + i + "  " + s));
                }

                if(mainHomeDataModel.getBodyList() != null){
                    if(MainVipPagePresenter.this.getMvpView() != null)
                        MainVipPagePresenter.this.getMvpView().getBodySuccess(mainHomeDataModel.getBodyList());

                } else {
                    if(MainVipPagePresenter.this.getMvpView() != null)
                        MainVipPagePresenter.this.getMvpView().getDataError(new Throwable("VIP BodyList data fetch error:" + i + "  " + s));
                }
            }
        });
    }

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