package com.adealink.weparty.im

import android.app.Application
import com.adealink.frame.base.AppBase
import com.adealink.frame.log.Log
import com.adealink.frame.spi.RegisterService
import com.adealink.weparty.im.constant.OFFICIAL_IMAGE_TEXT_BUSINESS_ID
import com.adealink.weparty.im.constant.PLAYMATE_ORDER_BUSINESS_ID
import com.adealink.weparty.im.constant.PLAYMATE_SAY_WELCOME_TEXT_BUSINESS_ID
import com.adealink.weparty.im.constant.PLAYMATE_SAY_WELCOME_VOICE_BUSINESS_ID
import com.adealink.weparty.im.constant.TAG_IM_PUSH
import com.adealink.weparty.im.manager.login.imLoginManager
import com.adealink.weparty.im.manager.session.sessionManager
import com.adealink.weparty.im.service.TIMAppService
import com.adealink.weparty.im.session.mesasge.CustomMessageViewHolder
import com.adealink.weparty.im.session.mesasge.OfficialImageTextMessageBean
import com.adealink.weparty.im.session.mesasge.PlaymateOrderMessageBean
import com.adealink.weparty.im.session.mesasge.PlaymateSayWelcomeTextMessageBean
import com.adealink.weparty.im.session.mesasge.PlaymateSayWelcomeVoiceMessageBean
import com.adealink.weparty.im.util.TUIUtils
import com.adealink.weparty.module.im.IIMService
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.UserInfo
import com.tencent.qcloud.tim.push.TIMPushListener
import com.tencent.qcloud.tim.push.TIMPushManager
import com.tencent.qcloud.tim.push.TIMPushMessage
import com.tencent.qcloud.tuicore.TUIConfig
import com.tencent.qcloud.tuikit.tuichat.config.TUIChatConfigs
import com.tencent.qcloud.tuikit.tuiconversation.config.TUIConversationConfig

@RegisterService(value = IIMService::class)
class IMServiceImpl : IIMService {

    override fun appOnCreateMainTask(application: Application) {
        val myUserInfo = ProfileModule.getMyUserInfo()
        TUIConfig.setSelfFaceUrl(myUserInfo?.avatar)
        TUIConfig.setSelfNickName(myUserInfo?.nickName)

        //注册自定义消息类型
        TUIChatConfigs.registerCustomMessage(
            OFFICIAL_IMAGE_TEXT_BUSINESS_ID,
            OfficialImageTextMessageBean::class.java,
            CustomMessageViewHolder::class.java
        )
        TUIChatConfigs.registerCustomMessage(
            PLAYMATE_ORDER_BUSINESS_ID,
            PlaymateOrderMessageBean::class.java,
            CustomMessageViewHolder::class.java
        )
        TUIChatConfigs.registerCustomMessage(
            PLAYMATE_SAY_WELCOME_TEXT_BUSINESS_ID,
            PlaymateSayWelcomeTextMessageBean::class.java,
            CustomMessageViewHolder::class.java
        )
        TUIChatConfigs.registerCustomMessage(
            PLAYMATE_SAY_WELCOME_VOICE_BUSINESS_ID,
            PlaymateSayWelcomeVoiceMessageBean::class.java,
            CustomMessageViewHolder::class.java
        )

        //开启用户验收
        TUIConversationConfig.getInstance().isShowUserStatus = true

        //开启已读回执
        TUIChatConfigs.getGeneralConfig().isMsgNeedReadReceipt = true

        //离线Push设置
        TIMPushManager.getInstance().forceUseFCMPushChannel(true)
        TIMPushManager.getInstance().addPushListener(object : TIMPushListener() {

            override fun onRecvPushMessage(message: TIMPushMessage?) {
                super.onRecvPushMessage(message)
                if (!AppBase.isRelease) {
                    Log.e(TAG_IM_PUSH, "onRecvPushMessage:$message")
                }
            }

            override fun onNotificationClicked(ext: String?) {
                Log.i(TAG_IM_PUSH, "onNotificationClicked:$ext")
                TUIUtils.handleOfflinePush(ext)
            }
        })

        imLoginManager.init(application)
        sessionManager.init(application)
        TIMAppService().init(application)
    }

    override fun getService(): IMServiceImpl {
        return this
    }

    override fun onLogout() {

    }

    override fun isLogin(): Boolean {
        return imLoginManager.isLogin()
    }

    override fun updateIMSelfInfo(userInfo: UserInfo?) {
        imLoginManager.updateSelfUserInfo(userInfo)
    }

}