package com.rc.httpcore.client.retrofit

import android.util.Log
import com.google.gson.Gson
import com.rc.httpcore.HttpClient
import com.rc.httpcore.HttpConfig
import com.rc.httpcore.client.ChemicalClient
import com.rc.httpcore.exception.NetException
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 okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ChemicalRetrofit : ChemicalClient {

    private val apiService by lazy {
        HttpClient.createRetrofitApi(ApiService::class.java)
    }

    /**
     * 查询LOGO配置列表
     */
    override fun queryLogoConfInfo(): Observable<ConfLogo> {
        return apiService.queryLogoConfInfo()
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 查询APK版本
     *
     * @param id 设备唯一编码
     */
    override fun apkVersion(id: String): Observable<ApkInfoResp> {
        return apiService.apkVersion(id)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }
                return@map response.data
            }
    }

    /**
     * 上传APK更新状态
     *
     * @param state 0:升级失败; 1:升级成功; 2:升级中
     */
    override fun onepcApkUpdate(id: String, state: String): Observable<Boolean> {
        return apiService.onepcApkUpdate(id, state)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }
                return@map true
            }
    }

    /**
     * 用户端登录
     *
     * @param param 刷学生卡数据 userName
     */
    override fun learnLogin(param: LearnLoginReq): Observable<LearnLoginVo> {
        Log.d("param:===========", "" + param.machineCode)
        Log.d("param:===========", "" + param.userName)
        Log.d("param:===========", "" + param.type)
        Log.d("param:===========", "" + param.aioType)
        return apiService.learnLogin(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                HttpClient.token = response.data?.access_token
                return@map response.data
            }
    }

    /**
     * 退出登录
     */
    override fun loginOut(): Observable<Boolean> {
        return apiService.loginOut()
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map true
            }
    }

    /**
     * 人脸比对
     */
    override fun faceCompare(param: FaceCompareReq): Observable<Boolean> {
        return apiService.faceCompare(HttpConfig.BASE_PATH_FACE, param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map true
            }
    }

    /**
     * 查询实验室机柜列表
     */
    override fun queryCabinetList(): Observable<List<CabinetBean>> {
        return apiService.queryCabinetList(CabinetBean())
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 查询化学品
     */
    override fun queryChemicalList(param: ChemicalReq): Observable<CommonListResponse<ChemicalBean>> {
        return apiService.queryChemicalList(param.pageNum, param.pageSize, param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response
            }
    }

    /**
     * 查询库存化学品
     */
    override fun queryChemicalByStock(param: ChemicalReq): Observable<CommonListResponse<ChemicalBean>> {
        return apiService.queryChemicalByStock(param.pageNum, param.pageSize, param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response
            }
    }

    /**
     * 双人验证 刷卡识别身份权限
     */
    override fun userValidation(param: UserValidationVo): Observable<UserValidationVo> {
        return apiService.userValidation(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     *
     */
    override fun addStock(param: ChemicalBean): Observable<ChemicalBean> {
        return apiService.addStock(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 获取危化品安全技术说明书详细信息
     */
    override fun hazardBookDetail(id: String): Observable<HazardBook> {
        return apiService.hazardBookDetail(id)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 通过识别卡号或者二维码扫码查询已入库化学品
     */
    override fun queryByTagCode(param: TagCodeReq): Observable<Optional<ChemicalBean>> {
        val message = Gson().toJson(param)
        Log.d("==================",message)
        return apiService.queryByTagCode(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map Optional.ofNullable(response.data)
            }
    }

    /**
     * 领用化学品
     */
    override fun addUserecord(param: UseReq): Observable<Boolean> {
        return apiService.addUserecord(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map true
            }
    }

    /**
     * 归还时查询已领用化学品
     */
    override fun queryByUserecord(param: TagCodeReq): Observable<ChemicalBean> {
        return apiService.queryByUserecord(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 归还化学品
     */
    override fun updateUserecord(param: RevertReq): Observable<Boolean> {
        return apiService.updateUserecord(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map true
            }
    }

    /**
     * 化学品出库
     */
    override fun outStock(param: OutputStockReq): Observable<Boolean> {
        return apiService.outStock(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map true
            }
    }

    /**
     * RFID 标签更换
     */
    override fun updateStock(param: UpdateRfidReq): Observable<Boolean> {
        return apiService.updateStock(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map true
            }
    }

    /**
     * 查询使用记录
     */
    override fun queryRecordList(param: OperRecordReq): Observable<CommonListResponse<AioUserecordVo>> {
        return apiService.queryRecordList(param.pageNum, param.pageSize, param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response
            }
    }

    /**
     * 查询报警记录列表
     */
    override fun queryAlarmRecordList(param: AlarmRecordReq): Observable<CommonListResponse<AioAlarmRecordVo>> {
        return apiService.queryAlarmRecordList(param.pageNum, param.pageSize, param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response
            }
    }

    /**
     * 查询报警记录详情
     */
    override fun queryAlarmRecordDetail(id: String): Observable<AioAlarmRecordVo> {
        return apiService.queryAlarmRecordDetail(id)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 检测类型报警记录手动处理
     */
    override fun updateAlarmRecord(param: UpdateAlarmRecordReq): Observable<Boolean> {
        return apiService.updateAlarmRecord(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map true
            }
    }

    /**
     * 判断当前登录人是否管理员
     */
    override fun authValidation(): Observable<Boolean> {
        return apiService.authValidation()
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 终端登录查询实验室信息
     */
    override fun querySubInfo(): Observable<SubInfoVo> {
        return apiService.querySubInfo()
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 人脸身份验证
     */
    override fun faceAuth(param: SubInfoVo): Observable<UserValidationVo> {
        return apiService.faceAuth(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 查询机柜柜锁列表
     */
    override fun queryCabinetLockList(param: CabinetLockVo): Observable<List<CabinetLockVo>> {
        return apiService.queryCabinetLockList(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 柜锁开锁
     */
    override fun openLock(param: CabinetLockVo): Observable<LockRespBean> {
        val gson = Gson()
        val toJson = gson.toJson(param)
        Log.d("===============开锁", toJson)
        return apiService.openLock(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 柜锁关锁
     */
    override fun closeLock(param: CabinetLockVo): Observable<LockRespBean> {
        return apiService.closeLock(param)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

    /**
     * 上传文件
     */
    override fun fileUpload(file: File): Observable<FileRespBean> {
        val fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file)
        val filePart = MultipartBody.Part.createFormData("file", file.name, fileBody)
        return apiService.fileUpload(HttpConfig.BASE_PATH_BASE, filePart)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.msg)
                }

                return@map response.data
            }
    }

}