package com.yidianling.consultant.modular.singlton

import android.app.Activity
import android.content.Context
import android.support.v7.app.AppCompatActivity
import com.yidianling.common.tools.ToastUtil
import com.yidianling.consultant.dialog.ConsultAssistantDialog
import com.yidianling.consultant.model.SearchApi
import com.yidianling.consultant.router.ConsultantIn
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * Created by xj on 2019/11/14.
 */
class ConsultAssistantDialogUtils private constructor(){

    companion object {
        val INSTANCE by lazy { ConsultAssistantDialogUtils() }
    }

    var consultAssistantDialog: ConsultAssistantDialog? = null

    fun showDialog(activity: Activity) {
        if (consultAssistantDialog == null) {
            consultAssistantDialog = ConsultAssistantDialog(activity, object : ConsultAssistantDialog.OnConsultAssistantClickListener {
                override fun onClickAction() {
                    //获取用户uid
                    getConsultAssistantUid(activity)
                }

            })
        }
        consultAssistantDialog!!.show()
    }

    fun hideDialog() {
        consultAssistantDialog?.dismiss()
        consultAssistantDialog = null
    }

    private fun getConsultAssistantUid(activity: Activity) {
        SearchApi.getSearchApi().getConsultAssistantUidRequest()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe{
                    if (it.code == "200" && it.data != 0.toLong()) {
                        ConsultantIn.startP2PSession(activity as AppCompatActivity, it.data.toString())
                    }else {
                        ToastUtil.toastShort("咨询助理忙碌中，请稍后再试")
                    }
                }
    }
}