package com.yidianling.uikit.custom.http

import com.alibaba.fastjson.JSON
import com.ydl.ydlcommon.data.http.BaseAPIResponse
import com.ydl.ydlnet.YDLHttpUtils
import com.yidianling.im.http.ImRetrofitApi
import com.yidianling.medical.MedicalFileBeanRespose
import com.yidianling.medical.archives.bean.*
import com.yidianling.medical.im.bean.SystemListRespose
import com.yidianling.uikit.custom.http.response.*
import com.yidianling.uikit.custom.http.response.question.QuestionsInfoBean
import io.reactivex.Observable
import okhttp3.MediaType
import okhttp3.RequestBody
import java.util.*

/**
 * Created by xj on 2019/6/26.
 */
class ServiceImpl private constructor() {

    companion object {
        val instance by lazy { ServiceImpl() }
    }

    /**
     * 获取专家登录状态
     */
    fun getDoctorChatStatus(doctorUid: Long): Observable<BaseAPIResponse<ChatStatusBean>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).getDoctorChatStatus(doctorUid)
    }

    /**
     * 获取助理登录状态
     */
    fun getAssistantChatStatus(assistantUid: Long): Observable<BaseAPIResponse<Int>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).getAssistantChatStatus(assistantUid)
    }

    /**
     * 获取推荐专家列表
     */
    fun getRecommendExpertList(
        doctorUid: Long,
        catName: String,
        limit: Int
    ): Observable<BaseAPIResponse<ArrayList<RecommendExpertBean>>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java)
            .getRecommendExpertList(doctorUid, catName, limit)
    }

    /**
     * 获取常用语
     */
    fun getCommonQuestionList(): Observable<BaseAPIResponse<List<CommonQuestionBean>>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).getCommonQuestionList()
    }

    /**
     * 常用语的点击
     */
    fun sendCommonQuestionCount(id: String): Observable<BaseAPIResponse<Any>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).sendCommonQuestionCount(id)
    }

    /**
     * 信息采集的问题
     */
    fun userCollectList(): Observable<BaseAPIResponse<List<UserQuestInfoBean>>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).userCollectList()
    }

    /**
     * 信息采集的问题新版
     */
    fun preExamQuestionPaper(): Observable<BaseAPIResponse<QuestionsInfoBean>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).preExamQuestionPaper()
    }

    /**
     * 获取新用户收集的信息
     */
    fun getNewUserMes(bean: NewUserMesBean): Observable<BaseAPIResponse<List<UserQuestInfoBean>>> {
        val beanStr = JSON.toJSONString(bean)
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            beanStr
        ) as RequestBody
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).getNewUserMes(body)
    }

    /**
     * 获取用户来源
     */
    fun getUserSourceFrom(uid: String): Observable<BaseAPIResponse<String>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).getUserSource(uid)
    }

    /**
     * 上传采集的问题
     */
    fun submitUserCollect(params: String): Observable<BaseAPIResponse<Any>> {
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            params
        ) as RequestBody
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).submitUserCollect(body)
    }

    /**
     * 关注用户
     */
    fun focus(doctorId: String, status: String): Observable<BaseAPIResponse<Any>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).focus(doctorId, status)
    }

    /**
     * 服务列表
     */
    fun serviceList(doctorId: String): Observable<BaseAPIResponse<List<ServiceItemBean>>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).serviceList(doctorId)
    }

    /**
     * 获取输入框的hint内容
     */
    fun getChatViewConfig(): Observable<BaseAPIResponse<SystemInfoBean>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).getChatViewConfig()
    }


    /**
     * 客户端灵犀信息收集卡题库
     */
    fun getSourceDoctor(
        userUid: String,
        assistantUid: String
    ): Observable<BaseAPIResponse<SourceDoctorInfoBean>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java)
            .getSourceDoctor(1, userUid, assistantUid)
    }

    /**
     * 创建答题信息
     */
    fun createEexams(params: String): Observable<BaseAPIResponse<String>> {
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            params
        ) as RequestBody
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).createEexams(body)
    }

    /**
     * 回答问题
     */
    fun answerQuestion(id: String, params: String): Observable<BaseAPIResponse<String>> {
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            params
        ) as RequestBody
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).answerQuestion(id, body)
    }

    /**
     * 去聊天 回答问题
     */
    fun lingxiAnswerQuestion(params: String): Observable<BaseAPIResponse<Any>> {
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            params
        ) as RequestBody
        return YDLHttpUtils.obtainApi(ImRetrofitApi::class.java).lingxiAnswerQuestion(body)
    }

    /**
     * 去聊天批量发送 回答问题
     */
    fun batchSendCustomizeMessage(params: String): Observable<BaseAPIResponse<Any>> {
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            params
        ) as RequestBody
        return YDLHttpUtils.obtainApi(ImRetrofitApi::class.java).batchSendCustomizeMessage(body)
    }

    /***获取患者管理列表*/
    fun getPageList(
        pageNum: Int,
        pageSize: Int
    ): Observable<BaseAPIResponse<MedicalUserArchivesBean>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).getPageList(pageNum, pageSize)
    }

    /***获取问诊人列表*/
    fun getList(): Observable<BaseAPIResponse<List<MedicalArchivesListBean>>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java).getList()
    }

    /***
     * 创建问诊人
     * @param name 名字
     * @param idcardNo  身份证号
     * @param relation  与问诊人关系
     * @param merried 婚姻状态
     * */
    fun createArchives(
        name: String,
        idcardNo: String,
        relation: Int,
        merried: Int
    ): Observable<BaseAPIResponse<Any>> {
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            JSON.toJSONString(CreatePatientDtoBody(idcardNo, merried, name, relation))
        ) as RequestBody


        return YDLHttpUtils.obtainApi(ServiceApi::class.java)
            .createArchives(body)
    }

    /***
     * 修改问诊人
     * @param name 名字
     * @param idcardNo  身份证号
     * @param relation  与问诊人关系
     * @param merried 婚姻状态
     * */
    fun updateArchives(
        name: String,
        idcardNo: String,
        merried: Int,
        relation: Int,
        patientId: String
    ): Observable<BaseAPIResponse<Any>> {
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            JSON.toJSONString(UpdataPatientDtoBody(idcardNo, merried, name, relation,patientId))
        ) as RequestBody


        return YDLHttpUtils.obtainApi(ServiceApi::class.java)
            .updateArchives(body)
    }

    /***
     * 根据ID加载问诊人
     * @param patientId 问诊人id
     * */
    fun getArchives(patientId: String): Observable<BaseAPIResponse<GetArchivesResponse>> {
        return YDLHttpUtils.obtainApi(ServiceApi::class.java)
            .getArchives(patientId)
    }


    /***
     * 创建问诊订单
     * @param images 图片（检查报告或患处图片，逗号分隔）
     * @param isFirst  是否首次问诊(0不是、1是)
     * @param mainSuit  主诉，自己描述的病情
     * @param mainSuitDisease 主诉疾病（医生诊断的疾病名称）
     * @param patientId 患者ID
     * */
    fun createArchivesOrder(
        images: String,
        isFirst: Int,
        mainSuit: String,
        mainSuitDisease: String,
        patientId: Long,
        doctorId: String
    ): Observable<BaseAPIResponse<CreateArchivesResponse>> {
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            JSON.toJSONString(
                CreateImageInquiryDtoBody(
                    doctorId,
                    isFirst,
                    images,
                    1,
                    mainSuitDisease,
                    mainSuit,
                    patientId
                )
            )
        ) as RequestBody

        return YDLHttpUtils.obtainApi(ServiceApi::class.java)
            .createArchivesOrder(body)
    }

    /***
     * 删除问诊人
     * @param patientId 患者ID
     * */
    fun deleteArchives(
        patientId: Long
    ): Observable<BaseAPIResponse<Any>> {
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            JSON.toJSONString(PatientIdDtoBody(patientId))
        ) as RequestBody

        return YDLHttpUtils.obtainApi(ServiceApi::class.java).deleteArchives(body)
    }
    /**
     * 医疗 系统消息列表
     * @param read  是否阅读 0-未读 1-已读 空-全部
     * */
    fun getSystemPageList(pageNum: Int,pageSize: Int,read:String?):Observable<BaseAPIResponse<SystemListRespose>>{

        return YDLHttpUtils.obtainApi(ServiceApi::class.java).getSystemPageList(pageNum,pageSize,read)
    }
    /**
     * 医疗 系统消息 设为已读
     * */
    fun systemRead(msgId: Int):Observable<BaseAPIResponse<Any>>{

        return YDLHttpUtils.obtainApi(ServiceApi::class.java).systemRead(msgId)
    }

    fun uploadBatch( map :TreeMap<String, RequestBody>): Observable<BaseAPIResponse<List<MedicalFileBeanRespose>>> {

        return YDLHttpUtils.obtainApi(ServiceApi::class.java).uploadBatch(map)
    }

}