package com.yidianling.consultant.modular.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.support.v7.app.AppCompatActivity
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
import com.yidianling.common.tools.ToastUtil
import com.yidianling.consultant.ConsultAssistantCenterActivity
import com.yidianling.consultant.constants.ConsultBIConstants
import com.yidianling.consultant.model.SearchApi
import com.yidianling.consultant.router.ConsultantIn
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

class ConsultAssistantEntryUtils {
    companion object {
        @SuppressLint("CheckResult")
        fun jumpConsultAssistant(activity: Activity, location: Int) {
            ActionCountUtils.count(
                ConsultBIConstants.POSITION_DAOYI_ADVERTISEMENT_CLICK,
                "$location"
            )

            if (!ConsultantIn.isLogin()) {
                ConsultantIn.toLogin(activity)
            } else {
                // 请求接口获取咨询助理的uid
                SearchApi.getSearchApi().getConsultAssistantUidRequest(location)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(Consumer {
                        if (it.code == "200" && it.data != 0.toLong() && null != activity) {
                            ConsultantIn.startP2PSession(
                                activity as AppCompatActivity,
                                it.data.toString()
                            )
                        } else {
                            ToastUtil.toastShort("咨询助理忙碌中，请稍后再试")
                        }
                    }, object : ThrowableConsumer() {
                        override fun accept(msg: String) {
                            if (null != activity && activity is ConsultAssistantCenterActivity && !activity.isFinishing) activity.finish()
                        }
                    }
                    )
            }

        }
    }
}