package com.casic.dcms.mvp.model.impl;

import com.casic.dcms.bean.HomePageBean;
import com.casic.dcms.listener.OnGetPageResultListener;
import com.casic.dcms.mvp.model.IHomePageModel;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HomePageModelImpl implements IHomePageModel {

    private final OnGetPageResultListener homePageListener;

    public HomePageModelImpl(OnGetPageResultListener listener) {
        this.homePageListener = listener;
    }

    /**
     * {@link IHomePageModel}
     */
    @Override
    public Subscription sendHomePageRequest(String resourceType) {
        Observable<HomePageBean> observable = RetrofitServiceManager.getHomePageResult(resourceType);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<HomePageBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (homePageListener != null) {
                    homePageListener.onFailure(e);
                }
            }

            @Override
            public void onNext(HomePageBean resultBean) {
                if (resultBean != null) {
                    homePageListener.onSuccess(resultBean);
                }
            }
        });
    }
}
