package com.bc_chat.im.messages.vibration

import com.bc_chat.bc_base.entity.dao.GroupMemberDao
import com.bc_chat.bc_base.utils.Const
import io.rong.imkit.IExtensionModule
import io.rong.imkit.RongExtension
import io.rong.imkit.RongIM
import io.rong.imkit.emoticon.IEmoticonTab
import io.rong.imkit.plugin.IPluginModule
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message

/**
 * author：Created by zhaohaoting on 2019/3/15
 * email：526309416@qq.com
 * desc：
 */
class VibrationExtensioModule : IExtensionModule {
    private var mTargetId: String? = null
    override fun onInit(appKey: String) {
        //自定义震动消息
        RongIM.registerMessageType(RCVibrationMessage::class.java)
        RongIM.registerMessageTemplate(RCVibrationMessageProvider())
    }

    override fun onConnect(token: String) {}

    override fun onAttachedToExtension(extension: RongExtension) {
        mTargetId = extension.targetId
    }

    override fun onDetachedFromExtension() {}

    override fun onReceivedMessage(message: Message) {}

    override fun getPluginModules(conversationType: Conversation.ConversationType): List<IPluginModule> {
        val pluginModules = arrayListOf<IPluginModule>()
        if (conversationType == Conversation.ConversationType.GROUP) {
            val tGroupMember = GroupMemberDao.instance.findGroupMemberById(mTargetId!!, RongIMClient.getInstance().currentUserId)
            if (1 == tGroupMember?.role) { //我为群主
                val vibrationPlugin = VibrationPlugin()
                pluginModules.add(vibrationPlugin)
            }
        } else if (conversationType == Conversation.ConversationType.PRIVATE) {
            if (!Const.SERVICE_ID.contains(mTargetId!!)) {
                val vibrationPlugin = VibrationPlugin()
                pluginModules.add(vibrationPlugin)
            }
        }
        return pluginModules
    }

    override fun getEmoticonTabs(): List<IEmoticonTab>? {
        return null
    }

    override fun onDisconnect() {}

}