package com.hongyegroup.cpt_main.ui

import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.guadou.cs_cptservices.base.fragment.YYBaseVDBLoadingFragment
import com.guadou.cs_cptservices.interfaces.IFragmentRefresh
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.toast
import com.guadou.lib_baselib.ext.vertical
import com.guadou.lib_baselib.view.gloading.GLoadingTitleStatus
import com.guadou.lib_baselib.view.gloading.Gloading
import com.guadou.lib_baselib.view.gloading.GloadingGlobalAdapter
import com.hongyegroup.cpt_main.BR
import com.hongyegroup.cpt_main.R
import com.hongyegroup.cpt_main.databinding.FragmentNotificationMainBinding
import com.hongyegroup.cpt_main.mvvm.NotificationMainViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * 通知列表
 */
class NotificationFragment : YYBaseVDBLoadingFragment<NotificationMainViewModel, FragmentNotificationMainBinding>(), IFragmentRefresh,
    OnLoadMoreListener, OnRefreshListener {

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_notification_main, BR.viewModel, mViewModel)
            .addBindingParams(BR.click, ClickProxy())
    }

    //重新生成GLoading对象-跳动动画
    override fun generateGLoading(view: View): Gloading.Holder {
        return Gloading.from(GloadingGlobalAdapter())
            .wrap(view, GLoadingTitleStatus(true, true, true))
            .withRetry { onGoadingRetry() }
    }

    override fun startObserve() {
    }

    override fun init(savedInstanceState: Bundle?) {

        initRV()
        initData()
        initListener()
    }

    private fun initData() {
        mViewModel.getNotificationList().observe(this) {
            mBinding.refreshLayout.finishRefresh()
            mViewModel.mAdapter.loadMoreModule.isEnableLoadMore = true
        }
    }

    override fun onGoadingRetry() {
        mViewModel.isNeedPlaceHolder = true
        onRefresh(mBinding.refreshLayout)
    }

    private fun initListener() {
        //Adapter的滑动监听，监听加载更多
        mViewModel.mAdapter.loadMoreModule.isEnableLoadMore = false
        mViewModel.mAdapter.loadMoreModule.preLoadNumber = 4
        mViewModel.mAdapter.loadMoreModule.setOnLoadMoreListener(this)

        //刷新控件初始化
        mBinding.refreshLayout.setEnableNestedScroll(true)
        mBinding.refreshLayout.setEnableLoadMore(false)
        mBinding.refreshLayout.setEnableRefresh(true)
        mBinding.refreshLayout.setOnRefreshListener(this)

        //Item点击
        mViewModel.mAdapter.addChildClickViewIds(R.id.ll_content_root)
        mViewModel.mAdapter.setOnItemChildClickListener { _, _, position ->
            val item = mViewModel.mDatas[position]
            //调用接口已读消息
            if (!item.isRead) {
                //切换状态
                item.isRead = true
                mViewModel.mAdapter.notifyItemChanged(position, "read")
            }
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mViewModel.isNeedCleanAllData = true
        mViewModel.mAdapter.loadMoreModule.loadMoreComplete()
        mViewModel.mAdapter.loadMoreModule.isEnableLoadMore = false
        //直接调用，参数从成员变量中获取
        mViewModel.mCurPage = 1
        initData()
    }

    override fun onLoadMore() {
        mViewModel.isNeedCleanAllData = false
        mViewModel.mCurPage++
        initData()
    }

    private fun initRV() {
        mBinding.recyclerView.vertical().apply {
            adapter = mViewModel.mAdapter
        }
    }

    override fun scrollTopRefresh() {
        if (mViewModel.mDatas.size > 1) {
            mBinding.recyclerView.scrollToPosition(0)
        }
        mBinding.refreshLayout.autoRefresh()
    }

    /**
     * DataBinding事件处理
     */
    inner class ClickProxy {

        //阅读全部
        fun readAll() {
            toast("阅读全部")
        }

    }


}