package com.rc.httpcore.client

import com.rc.httpcore.HttpClient
import com.rc.httpcore.vo.CommonListResponse
import com.rc.httpcore.vo.Optional
import com.rc.httpcore.vo.request.*
import com.rc.httpcore.vo.response.*
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.io.File

object ApiRepository {

    private val mClientFactory by lazy { HttpClient.createClientFactory() }

    /**
     * 查询LOGO配置列表
     */
    fun queryLogoConfInfo(): Observable<ConfLogo> {
        return mClientFactory.createLabClient()
            .queryLogoConfInfo()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询APK版本
     *
     * @param id 设备唯一编码
     */
    fun apkVersion(id: String): Observable<ApkInfoResp> {
        return mClientFactory.createLabClient()
            .apkVersion(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 上传APK更新状态
     *
     * @param state 0:升级失败; 1:升级成功; 2:升级中
     */
    fun onepcApkUpdate(id: String, state: String): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .onepcApkUpdate(id, state)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 登录+实验室管理员
     */
    fun learnLogin(
        username: String,
        androidId: String,
        realLogin: Boolean = false
    ): Observable<LearnLoginVo> {
        val client = mClientFactory.createLabClient()
        val param = LearnLoginReq().apply {
            userName = username
            machineCode = androidId
            type = if (realLogin) "2" else "1"
        }
        return if (realLogin) {
            val observable1 = client.learnLogin(param)
            val observable2 = client.authValidation()
            Observable.zip(observable1, observable2, { response1, response2 ->
                response1.isManager = response2
                response1
            })
        } else {
            client.learnLogin(param)
        }.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 退出登录
     */
    fun loginOut(): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .loginOut()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 人脸比对
     */
    fun faceCompare(param: FaceCompareReq): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .faceCompare(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询实验室机柜列表
     */
    fun queryCabinetList(): Observable<List<CabinetBean>> {
        return mClientFactory.createLabClient()
            .queryCabinetList()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询化学品
     *
     * @param byStock ture: 查询库存化学品
     */
    fun queryChemicals(
        byStock: Boolean,
        param: ChemicalReq
    ): Observable<CommonListResponse<ChemicalBean>> {
        return if (byStock) queryChemicalByStock(param) else queryChemicalList(param)
    }

    /**
     * 查询化学品
     */
    fun queryChemicalList(param: ChemicalReq): Observable<CommonListResponse<ChemicalBean>> {
        return mClientFactory.createLabClient()
            .queryChemicalList(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询库存化学品
     */
    fun queryChemicalByStock(param: ChemicalReq): Observable<CommonListResponse<ChemicalBean>> {
        return mClientFactory.createLabClient()
            .queryChemicalByStock(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 双人验证 刷卡识别身份权限
     */
    fun userValidation(param: UserValidationVo): Observable<UserValidationVo> {
        return mClientFactory.createLabClient()
            .userValidation(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 化学品入库
     */
    fun addStock(param: ChemicalBean): Observable<ChemicalBean> {
        return mClientFactory.createLabClient()
            .addStock(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 获取危化品安全技术说明书详细信息
     */
    fun hazardBookDetail(id: String): Observable<HazardBook> {
        return mClientFactory.createLabClient()
            .hazardBookDetail(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 通过识别卡号或者二维码扫码查询已入库化学品
     */
    fun queryByTagCode(param: TagCodeReq): Observable<Optional<ChemicalBean>> {
        return mClientFactory.createLabClient()
            .queryByTagCode(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 领用化学品
     */
    fun addUserecord(param: UseReq): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .addUserecord(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 归还时查询已领用化学品
     */
    fun queryByUserecord(param: TagCodeReq): Observable<ChemicalBean> {
        return mClientFactory.createLabClient()
            .queryByUserecord(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 归还化学品
     */
    fun updateUserecord(param: RevertReq): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .updateUserecord(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 化学品出库
     */
    fun outStock(param: OutputStockReq): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .outStock(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * RFID 标签更换
     */
    fun updateStock(param: UpdateRfidReq): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .updateStock(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询使用记录
     */
    fun queryRecordList(param: OperRecordReq): Observable<CommonListResponse<AioUserecordVo>> {
        return mClientFactory.createLabClient()
            .queryRecordList(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询报警记录列表
     */
    fun queryAlarmRecordList(param: AlarmRecordReq): Observable<CommonListResponse<AioAlarmRecordVo>> {
        return mClientFactory.createLabClient()
            .queryAlarmRecordList(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询报警记录详情
     */
    fun queryAlarmRecordDetail(id: String): Observable<AioAlarmRecordVo> {
        return mClientFactory.createLabClient()
            .queryAlarmRecordDetail(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 检测类型报警记录手动处理
     */
    fun updateAlarmRecord(param: UpdateAlarmRecordReq): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .updateAlarmRecord(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 判断当前登录人是否管理员
     */
    fun authValidation(): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .authValidation()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 终端登录查询实验室信息
     */
    fun querySubInfo(): Observable<SubInfoVo> {
        return mClientFactory.createLabClient()
            .querySubInfo()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 人脸身份验证
     */
    fun faceAuth(param: SubInfoVo): Observable<UserValidationVo> {
        return mClientFactory.createLabClient()
            .faceAuth(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询机柜柜锁列表
     */
    fun queryCabinetLockList(param: CabinetLockVo): Observable<List<CabinetLockVo>> {
        return mClientFactory.createLabClient()
            .queryCabinetLockList(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 柜锁开锁
     */
    fun openLock(param: CabinetLockVo): Observable<LockRespBean> {
        return mClientFactory.createLabClient()
            .openLock(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 柜锁关锁
     */
    fun closeLock(param: CabinetLockVo): Observable<LockRespBean> {
        return mClientFactory.createLabClient()
            .closeLock(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 上传文件
     */
    fun fileUpload(file: File): Observable<FileRespBean> {
        return mClientFactory.createLabClient()
            .fileUpload(file)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

}