package com.adealink.weparty.im

import android.app.Activity
import android.app.Application
import com.adealink.frame.router.Router
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.manager.login.imLoginManager
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.module.im.IIMService
import com.adealink.weparty.module.im.IM
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuikit.tuichat.config.TUIChatConfigs
import com.tencent.qcloud.tuikit.tuisearch.bean.ChatInfo

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

    override fun activityOnCreateMainTask() {
    }

    override fun activityOnCreateSubTask() {
    }

    override fun appOnCreateMainTask(application: Application) {
        //注册自定义消息类型
        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.getGeneralConfig().isMsgNeedReadReceipt = true

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

    override fun appOnCreateSubTask(application: Application) {
    }

    override fun getService(): IMServiceImpl {
        return this
    }

    override fun logout() {

    }

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

    override fun goSession(activity: Activity, uid: String) {
        val chatInfo = ChatInfo()
        chatInfo.type = ChatInfo.TYPE_C2C
        chatInfo.id = uid
        Router.build(activity, IM.Session.PATH)
            .putExtra(IM.Session.EXTRA_CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
            .putExtra(IM.Session.EXTRA_CHAT_ID, uid)
//            .putExtra(IM.Session.EXTRA_CHAT_NAME, conversationInfo.title)
//            .putExtra(IM.Session.EXTRA_CHAT_DRAFT_TEXT, conversationInfo.draft?.draftText)
//            .putExtra(IM.Session.EXTRA_CHAT_DRAFT_TIME, conversationInfo.draft?.draftTime)
            .start()
    }
}