package com.adealink.weparty.im.session

import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.im.databinding.ActivityOfficialSessionBinding
import com.adealink.weparty.module.im.IM
import com.adealink.weparty.util.applyImmersive
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuikit.tuichat.bean.C2CChatInfo
import com.tencent.qcloud.tuikit.tuichat.bean.ChatInfo
import com.tencent.qcloud.tuikit.tuichat.bean.DraftInfo
import com.tencent.qcloud.tuikit.tuichat.bean.GroupChatInfo

@RouterUri(path = [IM.OfficialSession.PATH], desc = "会话详情")
class OfficialSessionActivity : BaseActivity() {

    companion object {
        private const val TAG = "OfficialSessionActivity"
    }

    @BindExtra(IM.Session.EXTRA_CHAT_TYPE)
    var chatType: Int? = null

    @BindExtra(IM.Session.EXTRA_CHAT_ID)
    var chatID: String? = null

    @BindExtra(IM.Session.EXTRA_CHAT_NAME)
    var chatName: String? = null

    @BindExtra(IM.Session.EXTRA_CHAT_DRAFT_TEXT)
    var chatDraftText: String? = null

    @BindExtra(IM.Session.EXTRA_CHAT_DRAFT_TIME)
    var chatDraftTime: Long? = null

    private val binding by viewBinding(ActivityOfficialSessionBinding::inflate)

    private val sessionFragment: OfficialSessionFragment by fastLazy { OfficialSessionFragment() }

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        this@OfficialSessionActivity.applyImmersive { top ->
            binding.topBar.root.setPadding(
                0,
                top + 5.dp(),
                0,
                5.dp()
            )
        }
        binding.topBar.ivBack.onClick {
            finish()
        }
        inflateSessionFragment()
    }

    private fun inflateSessionFragment() {
        if (sessionFragment.isAdded) {
            return
        }
        sessionFragment.setChatInfo(getChatInfo())
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, sessionFragment, IM.OfficialSession.PATH)
            .commitAllowingStateLoss()
    }

    private fun getChatInfo(): ChatInfo? {
        if (chatID.isNullOrEmpty()) {
            return null
        }

        when (chatType) {
            V2TIMConversation.V2TIM_C2C -> {
                val chatInfo = C2CChatInfo()
                chatInfo.type = ChatInfo.TYPE_C2C
                chatInfo.id = chatID
                chatInfo.setChatName(chatName)

                if (!chatDraftText.isNullOrEmpty()) {
                    chatInfo.draft = DraftInfo().apply {
                        draftText = chatDraftText
                        draftTime = chatDraftTime ?: 0
                    }
                }
                return chatInfo
            }

            V2TIMConversation.V2TIM_GROUP -> {
                val chatInfo = GroupChatInfo()
                chatInfo.type = ChatInfo.TYPE_GROUP
                chatInfo.id = chatID
                chatInfo.setChatName(chatName)

                if (!chatDraftText.isNullOrEmpty()) {
                    chatInfo.draft = DraftInfo().apply {
                        draftText = chatDraftText
                        draftTime = chatDraftTime ?: 0
                    }
                }
                return chatInfo
            }

            else -> {
                return null
            }
        }

    }


}