package com.abbidot.tracker.ui.activity.account

import android.view.View
import androidx.activity.viewModels
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.NotificationV2Adapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.MessageBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityNotificationV2Binding
import com.abbidot.tracker.deprecated.ui.activity.vm.NotificationsViewModel
import com.abbidot.tracker.util.ViewUtil
import com.qmuiteam.qmui.widget.pullLayout.QMUIPullLayout

/**
 * 消息列表
 */
class NotificationV2Activity :
    BaseActivity<ActivityNotificationV2Binding>(ActivityNotificationV2Binding::inflate) {

    private val mNotificationsViewModel: NotificationsViewModel by viewModels()

    private lateinit var mMessageAdapter: NotificationV2Adapter

    private var mPage = 1
    private var mPullAction: QMUIPullLayout.PullAction? = null
    private var mReadMessageId = ""

    override fun getTopBar() = mViewBinding.notificationV2DetailBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_notifications)

        mMessageAdapter = NotificationV2Adapter(mContext, mutableListOf()).apply {
            setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
                override fun onItemClick(itemView: View?, pos: Int) {
                    val message = getItem(pos)
                    if (message.isRead == ConstantInt.Type0) {
                        mReadMessageId = message.messageId
                        mNotificationsViewModel.setMessageIsRead(mReadMessageId)
                    }
                }
            })
        }
        mViewBinding.apply {
            ViewUtil.instance.setRecyclerViewDecorationLinearLayoutManager(
                mContext, ilNotificationV2MessageLayout.pullRefreshAndLoadMoreRv, mMessageAdapter
            )
            ilNotificationV2MessageLayout.pullRefreshAndLoadMoreLayout.let {
                it.setActionListener { pullAction ->
                    mPullAction = pullAction
                    if (pullAction.pullEdge == QMUIPullLayout.PULL_EDGE_TOP) {
                        mPage = 1
                        mNotificationsViewModel.messageListByPage(mPage)
                    } else if (pullAction.pullEdge == QMUIPullLayout.PULL_EDGE_BOTTOM) {
                        mPage++
                        mNotificationsViewModel.messageListByPage(mPage)
                    }
                }
            }
            setOnClickListenerViews(btnNotificationV2ReadAll)
        }

        mNotificationsViewModel.messageListByPage(mPage)
    }

    override fun liveDataObserve() {
        mNotificationsViewModel.apply {
            mNotificationsLiveData.observe(this@NotificationV2Activity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        mPullAction?.let { p ->
                            mViewBinding.ilNotificationV2MessageLayout.pullRefreshAndLoadMoreLayout.finishActionRun(
                                p
                            )
                        }
                        it.getOrNull()?.apply {
                            setMessageData(this)
                        }
                    }

                    override fun onRequestError(exceptionCode: String?) {
                        mPullAction?.let { p ->
                            mViewBinding.ilNotificationV2MessageLayout.pullRefreshAndLoadMoreLayout.finishActionRun(
                                p
                            )
                            if (p.pullEdge == QMUIPullLayout.PULL_EDGE_BOTTOM) mPage--
                        }
                    }

                    override fun onInterceptCode() {
                        mPullAction?.let { p ->
                            mViewBinding.ilNotificationV2MessageLayout.pullRefreshAndLoadMoreLayout.finishActionRun(
                                p
                            )
                            if (p.pullEdge == QMUIPullLayout.PULL_EDGE_BOTTOM) mPage--
                        }
                    }
                }, isShowRequestErrorTip = false)
            }
            mReadMessageLiveData.observe(this@NotificationV2Activity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        val list = mMessageAdapter.getData()
                        for (i in 0 until list.size) {
                            val l = list[i]
                            if (mReadMessageId == l.messageId) {
                                l.isRead = ConstantInt.Type1
                                mMessageAdapter.notifyItemChanged(i)
                                break
                            }
                        }
                    }
                })
            }
            mReadAllMessageLiveData.observe(this@NotificationV2Activity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        val list = mMessageAdapter.getData()
                        var isRefresh = false
                        for (l in list) {
                            if (l.isRead == ConstantInt.Type0) {
                                l.isRead = ConstantInt.Type1
                                isRefresh = true
                            }
                        }
                        if (isRefresh) mMessageAdapter.notifyItemRangeChanged(0, list.size)
                    }
                })
            }
        }
    }

    private fun setMessageData(list: MutableList<MessageBean>) {
        val messageList = mMessageAdapter.getData()
        if (null == mPullAction) {
            messageList.addAll(list)
            mMessageAdapter.notifyItemRangeInserted(
                messageList.size - list.size, messageList.size
            )
        }
        mPullAction?.apply {
            if (pullEdge == QMUIPullLayout.PULL_EDGE_TOP) {
                mMessageAdapter.notifyItemRangeRemoved(0, messageList.size)
                messageList.clear()
                messageList.addAll(list)
                mMessageAdapter.notifyItemRangeInserted(0, messageList.size)
            } else {
                messageList.addAll(list)
                mMessageAdapter.notifyItemRangeInserted(
                    messageList.size - list.size, messageList.size
                )
            }
        }
    }

    override fun onClick(v: View?) {
        if (isLimitClick()) return
        mViewBinding.apply {
            when (v!!) {
                btnNotificationV2ReadAll -> mNotificationsViewModel.setMessageAllIsRead()
            }
        }
    }
}