package com.lukouguoji.module_base.model

import android.content.Context
import android.content.DialogInterface
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.R
import com.lukouguoji.module_base.adapter.setCommonAdapter
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.bean.SocketEventBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.databinding.DialogNoticeMessageBinding
import com.lukouguoji.module_base.databinding.ItemNoticeMessageBinding
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.interfaces.IOnItemClickListener
import com.lukouguoji.module_base.ktx.addOnItemClickListener
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.loge
import com.lxj.xpopup.XPopup
import dev.DevUtils
import dev.utils.app.HandlerUtils
import dev.utils.app.ScreenUtils
import dev.utils.app.share.SPUtils
import kotlin.math.roundToInt

class NoticeMessageDialogModel : BaseDialogModel<DialogNoticeMessageBinding>(DIALOG_TYPE_FULL) {

    val count = MutableLiveData("1")

    val folded = MutableLiveData(false)

    val viewHolder = NoticeMessageViewHolder::class.java
    val itemId = R.layout.item_notice_message

    private val closeMessage = {
        dismiss()
    }

    override fun onBuild(builder: XPopup.Builder) {
        loge("获取屏幕宽高 : ${ScreenUtils.getScreenWidth()} - ${ScreenUtils.getScreenHeight()}")
        builder.maxHeight((ScreenUtils.getScreenHeight() * 0.8f).roundToInt())
    }

    override fun layoutId() = R.layout.dialog_notice_message

    override fun onDialogCreated(context: Context) {
        binding.model = this
        setCommonAdapter(binding.rv, viewHolder, itemId)
        binding.rv.addOnItemClickListener(object : IOnItemClickListener {
            override fun onItemClick(position: Int, type: Int) {
                val bean = binding.rv.commonAdapter()!!.getItem(position) as SocketEventBean
                // TODO: 点击事件
                FlowBus.with<SocketEventBean>(ConstantEvent.SOCKET_EVENT_CLICK)
                    .tryEmit(bean)
                dismiss()
            }
        })

        HandlerUtils.postRunnable(closeMessage, 15000)
    }

    fun foldClick() {
        folded.value = !folded.value!!
    }

    /**
     * 添加消息
     */
    fun addMessage(bean: SocketEventBean) {
        val adapter = binding.rv.commonAdapter()!!
        val oldCount = adapter.itemCount
        adapter.addItem(bean)
        count.value = (oldCount + 1).toString()
        // 移出之前的计时
        HandlerUtils.removeRunnable(closeMessage)
        // 重新计时
        HandlerUtils.postRunnable(closeMessage, 15000)
    }

    override fun onDismiss(d: DialogInterface?) {
        super.onDismiss(d)
        HandlerUtils.removeRunnable(closeMessage)
        currentDialog = null
    }

    companion object {

        private var currentDialog: NoticeMessageDialogModel? = null

        fun show(bean: SocketEventBean) {
            HandlerUtils.postRunnable {
                if (currentDialog != null && currentDialog!!.isShowing()) {
                    currentDialog!!.addMessage(bean)
                } else {
                    currentDialog = NoticeMessageDialogModel()
                    currentDialog!!.show(DevUtils.getTopActivity())
                    currentDialog!!.addMessage(bean)
                }
            }
        }

        class NoticeMessageViewHolder(view: View) :
            BaseViewHolder<SocketEventBean, ItemNoticeMessageBinding>(view) {
            override fun onBind(item: Any?, position: Int) {
                val bean = getItemBean(item)!!
                binding.tv.text = bean.content
                notifyItemClick(position, binding.ll)
            }
        }
    }

}