package com.yidianling.consultant.modular.singlton

import android.app.Activity
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.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*

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

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

    var consultAssistantDialogFromHomePage: ConsultAssistantDialog? = null // 首页展示的dialog
    var consultAssistantDialog: ConsultAssistantDialog? = null //咨询师列表页面展示的dialog
    var expertSearchPageHasShown: Boolean = false // 专家咨询列表页面是否已经展示
    var timer: Timer? = null


    /**
     *判断是否符合代码展示逻辑
     * origin 展示请求来源    首页 home_index 咨询列表 doctor_list
     */
    fun fitRequest(activity: Activity, origin: String) {
        when (origin) {
            "home_index" -> {
                if (ConsultantIn.isLogin() &&
                    ConsultantIn.getUserImpl().getUserInfo()?.user_type == 1
                ) {
                    shouldShowDialog(activity, origin)
                }
            }
            "doctor_list" -> {
                shouldShowDialog(activity, origin)
            }
        }

    }

    /**
     * 判断是否符合接口展示逻辑
     */
    private fun shouldShowDialog(activity: Activity, origin: String, isDelay: Boolean = false) {
        // 请求接口判断导医咨询助理按钮是否展示
        SearchApi.getSearchApi().getConsultAssistantRequest(origin)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                if (it.data) {
                    if (origin == "home_index") {
                        showFromYdlHome(activity)
                    } else if (origin == "doctor_list") {
                        show(activity)
                    }
                }
            }
    }

    //////////////////////////////////////  首页的展示隐藏逻辑 代码块start  ///////////////////////////////////////////////////

    /**
     * 首页页展示
     */
    fun showFromYdlHome(activity: Activity) {
        if (!activity.isFinishing) {
            if (consultAssistantDialogFromHomePage == null) {
                consultAssistantDialogFromHomePage = ConsultAssistantDialog(
                        activity,
                        object : ConsultAssistantDialog.OnConsultAssistantClickListener {
                            override fun onClickAction() {
                                //获取用户uid
                                getConsultAssistantUid(activity)
                            }

                        })
            }
            consultAssistantDialogFromHomePage?.show()
        }
    }

    /**
     * 首页隐藏
     */
    fun hideFromHomePage() {
        consultAssistantDialogFromHomePage?.hide()
    }

    //////////////////////////////////////  首页的展示隐藏逻辑 代码块end  ///////////////////////////////////////////////////

    //////////////////////////////////////  咨询师列表页面的展示隐藏逻辑 代码块start  ///////////////////////////////////////////////////
    /**
     * 咨询师列表页展示
     */
    fun show(activity: Activity) {

        if (!expertSearchPageHasShown && timer == null) {
            timer = Timer()
            timer!!.schedule(object : TimerTask() {
                override fun run() {
                    Observable.just(1).observeOn(AndroidSchedulers.mainThread())
                        .subscribe {
                            expertSearchPageHasShown = true
                            showDialog(activity)
                        }
                }
            }, 5000)
        } else {
            showDialog(activity)
        }
    }

    /**
     * 咨询师列表页展示浮层
     */
    fun showDialog(activity: Activity) {
        if (!activity.isFinishing) {
            if (consultAssistantDialog == null) {
                consultAssistantDialog = ConsultAssistantDialog(
                        activity,
                        object : ConsultAssistantDialog.OnConsultAssistantClickListener {
                            override fun onClickAction() {
                                // 咨询师列表页面且未登录情况下，跳转登录页面
                                if (!ConsultantIn.isLogin()) {
                                    ConsultantIn.toLogin(activity)
                                } else {
                                    //获取用户uid
                                    getConsultAssistantUid(activity)
                                }
                            }

                        })
            }
            consultAssistantDialog?.show()
        }
    }


    /**
     * 咨询师列表页隐藏
     */
    fun hide() {
        consultAssistantDialog?.hide()
    }

    /**
     * 咨询师列表页面使用的重置本单例关于专家咨询列表页面得状态
     */
    fun resetStatus() {
        consultAssistantDialogFromHomePage?.dismiss()
        consultAssistantDialog?.dismiss()
        timer?.cancel()
        timer = null
        expertSearchPageHasShown = false
        consultAssistantDialogFromHomePage = null
        consultAssistantDialog = null
    }

    //////////////////////////////////////  咨询师列表页面的展示隐藏逻辑 代码块end  ///////////////////////////////////////////////////

    private fun getConsultAssistantUid(activity: Activity) {
        // 请求接口获取咨询助理的uid
        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("咨询助理忙碌中，请稍后再试")
                }
            }
    }
}