package com.rc.httpcore.client

import com.rc.httpcore.HttpClient
import com.rc.httpcore.vo.CommonListResponse
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

object ApiRepository {

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

    /**
     * 登录获取token
     */
    fun authOneLogin(): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .authOneLogin()
            .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())
    }

    /**
     * 查询轮播图
     *
     * @param id 设备唯一编码
     */
    fun bannerImages(id: String): Observable<List<BannerImageBean>> {
        return mClientFactory.createLabClient()
            .bannerImages(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

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

    /**
     * 学习一体机 用户端登录
     *
     * @param username 刷学生卡数据
     * @param androidId 设备唯一码
     * @param realLogin false:第一次登录，获取token；ture:第二次登录，真正的登录
     */
    fun learnLogin(username: String, androidId: String, realLogin: Boolean = false,isLogins:String): Observable<LearnLoginVo> {
        val param = LearnLoginReq().apply {
            userName = username
            type = if (realLogin) "2" else "1"
            machineCode = androidId
            isLogin=isLogins
        }
        return mClientFactory.createLabClient()
            .learnLogin(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    fun loginToken(username: String, androidId: String): Observable<LearnLoginVo> {
        val param = LearnLoginReq().apply {
            userName = username
            machineCode = androidId

        }
        return mClientFactory.createLabClient()
            .loginToken(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 安全学习-学习分类
     */
    fun categoryTreeList(): Observable<List<CategoryTree>> {
        return mClientFactory.createLabClient()
            .categoryTreeList()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 安全学习-查询学习资源列表
     */
    fun examCourseList(param: CourseListReq): Observable<CommonListResponse<ExamCourseVo>> {
        return mClientFactory.createLabClient()
            .examCourseList(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 安全学习-获取学习资源详细信息
     */
    fun examCourseDetail(id: String): Observable<ExamCourseVo> {
        return mClientFactory.createLabClient()
            .examCourseDetail(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 安全学习-开始学习
     */
    fun examLearnStart(param: ExamLearnReq): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .examLearnStart(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 安全学习-开始学习
     */
    fun examLearnFinish(param: ExamLearnReq): Observable<LearnBonusBean> {
        return mClientFactory.createLabClient()
            .examLearnFinish(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 安全学习-个人学习记录
     */
    fun examLearnRecord(param: LearnRecordReq): Observable<CommonListResponse<LearnRecordVo>> {
        return mClientFactory.createLabClient()
            .examLearnRecord(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询题目类型下拉框
     */
    fun classifyQueryOption(): Observable<List<TopicClassifyVo>> {
        return mClientFactory.createLabClient()
            .classifyQueryOption()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询模拟列表
     */
    fun onlineList(param: MockTestReq): Observable<CommonListResponse<MockTestVo>> {
        return mClientFactory.createLabClient()
            .onlineList(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 开始-查询考试前后否需要人脸验证
     *
     * @param examId 非模拟考试模式下都传-1
     */
    fun examStartVerify(type: String, examId: String = "-1"): Observable<ExamVerify> {
        return mClientFactory.createLabClient()
            .examStartVerify(type, examId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 开始考试-创建考卷
     *
     * @param examId 非模拟考试模式下都传-1
     */
    fun examStart(type: String, examId: String = "-1"): Observable<ExamTopic> {
        return mClientFactory.createLabClient()
            .examStart(type, examId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 考试 保存用户答案 一题一交
     */
    fun examFillAnswer(param: ExamAnswerReq): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .examFillAnswer(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 交卷- 完成考试
     */
    fun examHandPaper(param: HandPaperReq): Observable<HandPaperBean> {
        return mClientFactory.createLabClient()
            .examHandPaper(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 个人考试成绩列表查询
     */
    fun paperMyList(param: ExamScoreReq): Observable<CommonListResponse<ExamTopic>> {
        return mClientFactory.createLabClient()
            .paperMyList(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 个人考试成绩详情查询
     */
    fun paperDetail(id: String): Observable<ExamTopic> {
        return mClientFactory.createLabClient()
            .paperDetail(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 考试 查询问题和选项 - 一题一查
     */
    fun paperExamQuDetail(param: PaperQuReq): Observable<ExamTopic.ElPaperQu> {
        return mClientFactory.createLabClient()
            .paperExamQuDetail(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 查询我的证书列表
     */
    fun queryMyCert(pageNum: Int, pageSize: Int): Observable<CommonListResponse<CertVo>> {
        return mClientFactory.createLabClient()
            .queryMyCert(pageNum, pageSize)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 模拟练习-开始练习
     */
    fun mockTestStart(ids: String): Observable<ExamTopic> {
        return mClientFactory.createLabClient()
            .mockTestStart(ids)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 练习 查询问题和选项 - 一题一查
     */
    fun paperMockQuDetail(param: PaperQuReq): Observable<ExamTopic.ElPaperQu> {
        return mClientFactory.createLabClient()
            .paperMockQuDetail(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 练习 保存用户答案 - 一题一交
     */
    fun mockAnswer(param: MockAnswerReq): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .mockAnswer(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 结束练习
     */
    fun handPractise(param: HandPractiseReq): Observable<HandPractiseBean> {
        return mClientFactory.createLabClient()
            .handPractise(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 个人中心-用户信息
     */
    fun queryUserInfo(): Observable<UserInfo> {
        return mClientFactory.createLabClient()
            .queryUserInfo()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 个人中心-违规信息
     */
    fun queryViolationList(param: ViolationReq): Observable<List<ViolationBean>> {
        return mClientFactory.createLabClient()
            .queryViolationList(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 是否有待处理的违规
     */
    fun existTodoViolation(): Observable<Boolean> {
        return mClientFactory.createLabClient()
            .existTodoViolation()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 开始考核-生成考核考卷
     * @param id 章节ID
     */
    fun classTestStart(id: String): Observable<ExamTopic> {
        return mClientFactory.createLabClient()
            .classTestStart(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 结束考核
     * @param id 考核ID(试卷ID)
     */
    fun handAssess(id: String): Observable<HandPractiseBean> {
        return mClientFactory.createLabClient()
            .handAssess(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 考核记录列表
     */
    fun classTestRecord(param: AssessRecordReq): Observable<CommonListResponse<AccessRecordBean>> {
        return mClientFactory.createLabClient()
            .classTestRecord(param)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

    /**
     * 考核详情
     * @param id 考核ID(试卷ID)
     */
    fun classTestDetail(id: String): Observable<ExamTopic> {
        return mClientFactory.createLabClient()
            .classTestDetail(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
    }

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

}