
package com.ydl.ydlcommon.data.http

import com.ydl.ydlcommon.base.BaseApp
import com.yidianling.common.tools.RxNetTool
import io.reactivex.Observable
import io.reactivex.ObservableTransformer
import io.reactivex.schedulers.Schedulers
import java.net.ConnectException

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
*/
object RxUtils {

    // 在子线程中把实体参数类转换成 map
    @JvmStatic
    fun <T> mapObservable(t: T): Observable<HashMap<String, String>> {
        return Observable.just(t)
                .compose(netCheck())
                .subscribeOn(Schedulers.io())
                .map { MapUtils.bean2Map(it) }
    }

    /**
     * 若网络未连接，抛出未连接异常
     */
    @JvmStatic
    fun <T>netCheck(): ObservableTransformer<T, T> {
        return ObservableTransformer {
            if (RxNetTool.isConnected(BaseApp.getApp())) it
            else Observable.error(ConnectException())
        }
    }

    /**
     * 若状态码不为0，抛出接口异常
     */
    @JvmStatic
    fun <T> handleResult(): ObservableTransformer<Response<T>, Response<T>> {
        return ObservableTransformer {
            it.flatMap {
                if (it.code == 0) Observable.just(it)
                else Observable.error(ApiThrowable(it.msg))
            }
        }
    }

    /**
     * 根据返回数据，得到真正需要的数据
     * 若状态码不为0，抛出接口异常
     */
    @JvmStatic
    fun <T> resultData(): ObservableTransformer<BaseResponse<T>, T> {
        return ObservableTransformer {
            it.flatMap {
                if (it.code == 0) Observable.just(it.data)
                else Observable.error(Throwable(it.msg, Throwable(it.code.toString())))
            }
        }
    }

    /**
     * java跟php返回结构不一致 所以新写了一个方法
     * 根据返回数据，得到真正需要的数据
     * 若状态码不为200，抛出接口异常
     */
    @JvmStatic
    fun <T> resultJavaData(): ObservableTransformer<BaseAPIResponse<T>, T> {
        return ObservableTransformer {
            it.flatMap {
                if (it.code == "200") Observable.just(it.data)
                else {
                    Observable.error(Throwable(it.msg, Throwable(it.code)))
                }
            }
        }
    }
}