package com.gh.gamecenter.message.view.message

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.viewModelProviderFromParent
import com.gh.gamecenter.message.R
import com.gh.gamecenter.message.entity.SortedMessageEntity
import com.gh.gamecenter.message.utils.NewFlatLogUtils

/**
 * 消息中心-分类消息列表
 */
class SortedMessageListFragment : ListFragment<SortedMessageEntity, SortedMessageListViewModel>() {
    private lateinit var mViewModel: SortedMessageListViewModel
    private var mAdapter: SortedMessageListAdapter? = null

    private var mIsLogEnter = false

    override fun provideListAdapter(): SortedMessageListAdapter {
        if (mAdapter == null) {
            mAdapter = SortedMessageListAdapter(requireContext(), mViewModel, mEntrance)
        }
        return mAdapter as SortedMessageListAdapter
    }

    override fun provideListViewModel(): SortedMessageListViewModel {
        mViewModel = viewModelProviderFromParent()
        return mViewModel
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mCachedView?.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
        mListRv?.overScrollMode = View.OVER_SCROLL_NEVER
    }

    override fun onStart() {
        super.onStart()
        if (isEverPause()) {
            mViewModel.loadMessageUnreadData()
            mViewModel.getSortedMessage()
        }
    }

    override fun onResume() {
        super.onResume()
        (requireActivity() as ToolBarActivity).clearMenu()
        initMenu(R.menu.menu_clean_unread)

        if (!mIsLogEnter) {
            NewFlatLogUtils.logMessageInformEnter(mEntrance, mViewModel.getUnreadCountLiveData()?.value?.message ?: 0)
            mIsLogEnter = true
        }
    }

    override fun onLoadRefresh() {
        super.onLoadRefresh()
        mViewModel.loadMessageUnreadData()
    }

    override fun onMenuItemClick(menuItem: MenuItem?) {
        if (menuItem?.itemId == R.id.menu_clean_unread) {
            DialogHelper.showDeleteDialog(
                requireContext(),
                title = "确定清除",
                msg = "确定清除消息的所有未读吗",
                confirmText = "确定",
                cancelText = "取消",
                confirmClickCallback = {
                    // 一键已读
                    mViewModel.markAllSortedMessageRead()

                    val sessionNum = mViewModel.listLiveData.value?.count { it.unreadCount != 0 } ?: 0
                    NewFlatLogUtils.logMessageInformClean(
                        sessionNum,
                        mViewModel.getUnreadCountLiveData()?.value?.message ?: 0
                    )
                },
                cancelClickCallback = null,
                isReverse = false
            )
        }
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration? = null

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mCachedView?.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
    }

}