package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.ActionResultBean;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;

import java.io.File;

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

public class UploadImageModelImpl implements IUploadFileModel {

    private OnUploadImageListener listener;

    public UploadImageModelImpl(OnUploadImageListener uploadImageListener) {
        this.listener = uploadImageListener;
    }

    /**
     * 数据回调接口
     */
    public interface OnUploadImageListener {
        void onSuccess(ActionResultBean resultBean);

        void onFailure(Throwable throwable);
    }


    @Override
    public Subscription sendRetrofitRequest(File image) {
        Observable<ActionResultBean> observable = RetrofitServiceManager.getImageUploadResult(image);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ActionResultBean>() {
            @Override
            public void onCompleted() {

            }

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

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