package com.sjkj.appthreefloor_tsgz.net;

import android.content.Context;

import com.sjkj.appthreefloor_tsgz.bean.VersionBean;
import com.sjkj.base_lib.entity.Params;
import com.sjkj.base_lib.entity.ResponseBean;
import com.sjkj.base_lib.net.RetrofitFactory;
import com.sjkj.base_lib.net.data.exception.BusinessException;
import com.sjkj.base_lib.net.domain.interactor.UseCase;
import com.sjkj.base_lib.utils.AppTools;

import rx.Observable;
import rx.functions.Func1;

/**
 * 校验版本信息
 */
public class CheckVersionNet extends UseCase {
    private Context context;
    private RetrofitService service;

    private Params params;

    public Params getParams() {
        return params;
    }

    public void setParams(Params params) {
        this.params = params;
    }

    public CheckVersionNet(Context context) {
        this.context = context;

        service = RetrofitFactory.getRetrofit(AppTools.getBaseUrl(), true, 10L, false).create(RetrofitService.class);
    }

    @Override
    protected Observable buildUseCaseObservable() {
        return service.checkVersion(params).flatMap(new Func1<ResponseBean<VersionBean>, Observable<ResponseBean<VersionBean>>>() {
            @Override
            public Observable<ResponseBean<VersionBean>> call(ResponseBean<VersionBean> responseBean) {
                if (!ResponseBean.CODE_SUCCESS.equals(responseBean.getCode())) {
                    return Observable.error(new BusinessException(responseBean.getMsg()));
                }
                return Observable.just(responseBean);
            }
        });
    }
}
