package com.yidianling.user

import com.tencent.bugly.crashreport.CrashReport
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.ydl.ydlcommon.utils.BuryPointUtils
import com.ydl.ydlcommon.utils.JPushUtils
import com.ydl.ydlcommon.utils.log.LogHelper
import com.yidianling.common.tools.LogUtil
import com.yidianling.router.im.IMLoginInfo
import com.yidianling.router.im.IMRequestCallback
import com.yidianling.router.user.UserResponse
import com.yidianling.user.api.event.UserLoginEvent
import com.yidianling.user.http.UserHttpImpl
import com.yidianling.user.http.request.ChannelIdParam
import com.yidianling.user.http.request.Logout
import com.yidianling.user.route.UserIn
import de.greenrobot.event.EventBus
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer


/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/05/09
 */
object LoginUtils {

    @JvmStatic
    fun saveData(userInfo: UserResponse?) {
        UserHelper.setUserinfo(userInfo)
    }

    @JvmStatic
    fun onLogin(userInfo: UserResponse?) {
        //设置异常用户ID
        CrashReport.setUserId(userInfo?.uid)
        //登录IM聊天
//        loginIm(userInfo)
        //设置极光注册id
        LoginHelper.setChannelId()
        val loginEvent = UserLoginEvent("login", userInfo?.uid!!)
        EventBus.getDefault().post(loginEvent)
        BuryPointUtils.bindUid(userInfo?.uid ?: "")
        val channelId = JPushUtils.getRegistrationID()
        UserHttpImpl.getInstance().channelId(ChannelIdParam(channelId))
                .compose(RxUtils.resultData())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Consumer {
                    if (it["upload_log"] == "1") {
                        LogHelper.getInstance().uploadLog(false)
                    }
                }, object : ThrowableConsumer() {
                    override fun accept(msg: String) {
                        //  ToastHelper.show(msg)
                    }
                })
    }

    private fun loginIm(userInfo: UserResponse?) {
        val info = IMLoginInfo(userInfo?.uid ?: "", userInfo?.hxpwd ?: "")
        val callback = object : IMRequestCallback<IMLoginInfo> {
            override fun onSuccess(t: IMLoginInfo?) {
                UserIn.imSetAccount(userInfo?.uid ?: "")
            }

            override fun onFailed(i: Int) {
                LogUtil.e("IM登录失败：$i")
            }

            override fun onException(throwable: Throwable?) {
                LogUtil.e("IM登录onException：${throwable?.message}")
            }
        }

        UserIn.imSetAccount(info.account)
        UserIn.imLogin(info, callback)
    }


    @JvmStatic
    fun logout() {
        UserHttpImpl.getInstance().logout(Logout())
                .compose(RxUtils.resultData())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    logoutClearLocal()
                }, {
                    logoutClearLocal()
                })
    }

    @JvmStatic
    fun logoutClearLocal() {
        UserIn.imLogout()
        UserIn.closePlayer()
        UserIn.clearImData()
        UserHelper.setUserinfo(null)
    }


}