package com.adealink.weparty.im.session

import android.view.inputmethod.InputMethodManager
import androidx.activity.viewModels
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.statusBarHeight
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.im.databinding.ActivitySessionBinding
import com.adealink.weparty.im.session.comp.SessionTopComp
import com.adealink.weparty.im.session.viewmodel.SessionViewModel
import com.adealink.weparty.im.viewmodel.IMViewModelFactory
import com.adealink.weparty.module.im.IM
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.Session.PATH], desc = "会话详情")
class SessionActivity : BaseActivity() {

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

    @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 var chatInfo: ChatInfo? = null

    private val binding by viewBinding(ActivitySessionBinding::inflate)
    private val sessionViewModel by viewModels<SessionViewModel> { IMViewModelFactory() }
    private val sessionFragment: SessionFragment by fastLazy { SessionFragment() }

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

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        binding.topBar.root.setPadding(
            0,
            this@SessionActivity.statusBarHeight() + 5.dp(),
            0,
            5.dp()
        )
        inflateSessionFragment()
    }

    override fun initComponents() {
        super.initComponents()
        SessionTopComp(this, binding.topBar, chatInfo).attach()
    }

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

    override fun loadData() {
        super.loadData()
        sessionViewModel.loadChatInfo(chatInfo)
    }

    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
            }
        }

    }


    override fun finish() {
        hideSoftInput()
        super.finish()
    }

    private fun hideSoftInput() {
        val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        val window = getWindow()
        if (window != null) {
            imm.hideSoftInputFromWindow(window.decorView.windowToken, 0)
        }
    }
}