package com.sjkj.appthreefloor_tsgz.net;

import android.content.Context;

import com.sjkj.appthreefloor_tsgz.bean.EventCenterListBean;
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 EventListRequest 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 EventListRequest(Context context) {
        this.context = context;
        service = RetrofitFactory.getRetrofit(AppTools.getBaseUrl()).create(RetrofitService.class);
    }

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