package com.sjkj.base_lib.net.request;

import android.content.Context;

import com.sjkj.base_lib.camera.dh.bean.CameraBean;
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 java.util.List;

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

/**
 *
 */
public class DeviceListRequest extends UseCase {
    private Context context;
    private RetrofitBaseService service;

    private Params params;

    public Params getParams() {
        return params;
    }

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

    public DeviceListRequest(Context context) {
        this.context = context;
        service = RetrofitFactory.getRetrofit(AppTools.getBaseUrl(), true, 30L, false).create(RetrofitBaseService.class);
    }

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