package com.common.lib_base.common.http.rx;


import com.common.lib_base.common.http.bean.BaseBean;
import com.common.lib_base.common.http.exception.ApiException;
import com.common.lib_base.utils.LogUtils;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by admin on 2018/3/15.
 *
 * @version $Rev$
 * @time 2018/3/15 21:24
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDate $Date$
 * @updateDes ${TODO}
 */

public class RxHttpResponseCompat {

    // 当前类名
    public static String mTag = "RxHttpResponseCompat--";

    //RxJava1写法
    /*public static  <T> Observable.Transformer<BaseBean<T>,T> compatResult(){

        return  new Observable.Transformer<BaseBean<T>, T>() {
            @Override
            public Observable<T> call(Observable<BaseBean<T>> baseBeanObservable) {



                return baseBeanObservable.flatMap(new Func1<BaseBean<T>, Observable<T>>() {
                    @Override
                    public Observable<T> call(final BaseBean<T> tBaseBean) {

                        if(tBaseBean.success()){
//                            Log.e("Test","RxHttpResponseCompat->success");
                            LogUtils.e("RxHttpResponseCompat->success");


                            return Observable.create(new Observable.OnSubscribe<T>() {
                                @Override
                                public void call(Subscriber<? super T> subscriber) {

                                    try {
//                                        Log.e("Test","RxHttpResponseCompat->onNext");
//                                        Log.e("Test","RxHttpResponseCompat->"+tBaseBean.getStatus());
//                                        Log.e("Test","RxHttpResponseCompat->"+tBaseBean.getMessage());
//                                        Log.e("Test","RxHttpResponseCompat->"+tBaseBean.getData());
                                        LogUtils.e("RxHttpResponseCompat->onNext");
                                        LogUtils.e("RxHttpResponseCompat->Code:"+tBaseBean.getCode());
                                        LogUtils.e("RxHttpResponseCompat->Msg:"+tBaseBean.getMessage());
                                        LogUtils.e("RxHttpResponseCompat->Data:"+tBaseBean.getData());
                                        LogUtils.e("RxHttpResponseCompat->Success:"+tBaseBean.isSuccess());

                                        if(tBaseBean.getModel()!=null){
                                            //门店设置数据在model里
                                            subscriber.onNext(tBaseBean.getModel());
                                        }else if(tBaseBean.getPage()!=null){
                                            //商品管理数据在page里
                                            subscriber.onNext(tBaseBean.getPage());
                                        }else if(tBaseBean.getLeaseOrder()!=null){
                                            //订单详情数据在leaseOrder里
                                            subscriber.onNext(tBaseBean.getLeaseOrder());
                                        }else if(tBaseBean.getStoreList()!=null){
                                            //订单门店列表数据在StoreList里
                                            subscriber.onNext(tBaseBean.getStoreList());
                                        }else {
                                            subscriber.onNext(tBaseBean.getData());
                                        }
                                        subscriber.onCompleted();
                                    }
                                    catch (Exception e){
//                                        Log.e("Test","RxHttpResponseCompat->onError");
//                                        Log.e("Test","RxHttpResponseCompat->"+e.getMessage());
                                        LogUtils.e("RxHttpResponseCompat->onError");
                                        LogUtils.e("RxHttpResponseCompat->"+e.getMessage());
                                        subscriber.onError(e);
                                    }


                                }
                            });


                        }
                        else {
//                            Log.e("Test","RxHttpResponseCompat->fail");
                            LogUtils.e("RxHttpResponseCompat->fail,请求成功，但是非正常结果");
                            LogUtils.e("RxHttpResponseCompat->Code:"+tBaseBean.getCode());
                            LogUtils.e("RxHttpResponseCompat->Msg:"+tBaseBean.getMessage());

                            return  Observable.error(new ApiException(tBaseBean.getCode(),tBaseBean.getMessage()));
                        }

                    }
                }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());
            }
        };


    }*/


    //RxJava2写法
    public static <T> ObservableTransformer<BaseBean<T>, T> compatResult() {


        return new ObservableTransformer<BaseBean<T>, T>() {
            @Override
            public ObservableSource<T> apply(Observable<BaseBean<T>> baseBeanObservable) {

                return baseBeanObservable.flatMap(new Function<BaseBean<T>, ObservableSource<T>>() {
                    @Override
                    public ObservableSource<T> apply(@NonNull final BaseBean<T> tBaseBean) throws Exception {

//                        if (tBaseBean.success()) {
//                        int code = Integer.parseInt(tBaseBean.getCode());
                        int code = tBaseBean.getCode();

                        LogUtils.e(mTag + "code:" + code + "  message:" + tBaseBean.getMsg());
                        LogUtils.e(mTag + "tBaseBean:" + tBaseBean.toString());

                        // 服务端正确请求码
                        if (code == 200 || code == 100000 || code == 10000) {
                            return Observable.create(new ObservableOnSubscribe<T>() {
                                @Override
                                public void subscribe(ObservableEmitter<T> subscriber) throws Exception {

                                    try {
                                        // 返回结果。不能是null，要是{}
                                        if (tBaseBean.getData() != null) {
                                            subscriber.onNext(tBaseBean.getData());
                                        } else if (tBaseBean.getResult() != null) {
                                            subscriber.onNext(tBaseBean.getResult());
                                        }
                                        // 服务器返回的名字，具体根据后台来
//                                        if (tBaseBean.getResult() == null) {
//                                            subscriber.onError(new Exception("无数据"));
//                                        }
                                        subscriber.onComplete();
                                    } catch (Exception e) {
                                        LogUtils.e(mTag + "e--" + e.toString());
                                        subscriber.onError(e);
                                    }
                                }
                            });
                        } else {
                            // 针对用户名或密码错误的次数
                            if (code == 200003) {
                                return Observable.create(new ObservableOnSubscribe<T>() {
                                    @Override
                                    public void subscribe(ObservableEmitter<T> subscriber) throws Exception {

                                        try {
                                            // 返回结果
                                            if (tBaseBean.getData() != null) {
                                                subscriber.onNext(tBaseBean.getData());
                                            } else if (tBaseBean.getResult() != null) {
                                                subscriber.onNext(tBaseBean.getResult());
                                            }
                                            subscriber.onComplete();
                                        } catch (Exception e) {
                                            subscriber.onError(e);
                                        }
                                    }
                                });
                            }
                            LogUtils.e(mTag + "错误code:" + code + "  message:" + tBaseBean.getMessage());
                            // 错误消息提示
                            return Observable.error(new ApiException(tBaseBean.getCode() + "", tBaseBean.getMessage() + ""));
//                            return Observable.error(new ApiException(tBaseBean.getCode(), tBaseBean.getMsg()+""));
                        }

                    }
                }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());
            }
        };

    }


}
