package com.hongyegroup.cpt_ewallet.mvvm

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import com.guadou.cs_cptservices.binding.BaseDataBindingAdapter
import com.guadou.lib_baselib.base.vm.BaseViewModel
import com.guadou.lib_baselib.ext.checkEmpty
import com.guadou.lib_baselib.utils.CommUtils
import com.hongyegroup.cpt_ewallet.BR
import com.hongyegroup.cpt_ewallet.R
import com.hongyegroup.cpt_ewallet.bean.EWalletHistoryBean
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import javax.inject.Inject

@HiltViewModel
class EWalletHistoryViewModel @Inject constructor(
    private val savedStateHandle: SavedStateHandle
) : BaseViewModel() {

    var mKeywordLiveData = MutableLiveData<String>()

    var mCurPage = 1
    private var isNeedPlaceHolder = true
    var isNeedCleanAllData = true
    var mHistoryDatas = mutableListOf<EWalletHistoryBean>()
    val mAdapter by lazy { BaseDataBindingAdapter(R.layout.item_ewallet_history, BR.item, mHistoryDatas) }


    fun getHistoryList(): LiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()

        launchOnUI {
            //开始Loading
            if (isNeedPlaceHolder) loadStartLoading()

            delay(1500)
            val list = mutableListOf<EWalletHistoryBean>()
            for (i in 1..10) {
                list.add(EWalletHistoryBean().apply {
                    groupDate = "2022-07-0$mCurPage"
                })
            }

            handleData(list)
            loadSuccess()
            liveData.postValue(true)

            //返回的数据是封装过的，检查是否成功
//            result.checkResult({
//                loadSuccess()
//                //成功
//                handleData(it?.list)
//                liveData.postValue(true)
//            }, {
//                //失败
//                if (isNeedPlaceHolder) {
//                    loadError(it)
//                } else {
//                    toast(it)
//                }
//                liveData.postValue(false)
//            })
        }

        return liveData
    }

    //处理数据-添加或刷新
    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    private fun handleData(list: List<EWalletHistoryBean>?) {
        if (!list.checkEmpty()) {
            //有数据，判断是刷新还是加载更多的数据
            if (isNeedCleanAllData) {
                //刷新的方式
                mHistoryDatas.clear()
                addAllHistoryBean(list!!)
                mAdapter.notifyDataSetChanged()
            } else {
                //加载更多
                addAllHistoryBean(list!!)
//                mAdapter.notifyItemRangeInserted((mHistoryDatas.size - list.size , list.size )
//                mAdapter.notifyItemRangeChanged(mHistoryDatas.size - list.size - 1, list.size + 1)
                mAdapter.notifyDataSetChanged()
                mAdapter.loadMoreModule.loadMoreComplete()
            }
        } else {
            //展示无数据
            if (isNeedCleanAllData && mCurPage == 1) {
                mHistoryDatas.clear()
                mAdapter.notifyDataSetChanged()
                //展示无数据的布局
                val emptyView: View = CommUtils.inflate(R.layout.item_empty_layout)
                mAdapter.setEmptyView(emptyView)
            } else {
                //如果是加载更多,展示加载完成，没有更多数据了
                mAdapter.loadMoreModule.loadMoreEnd()
            }
        }
        isNeedPlaceHolder = false
        isNeedCleanAllData = false
    }

    //赋值并判断月份的分割线字段
    private fun addAllHistoryBean(list: List<EWalletHistoryBean>) {
        mHistoryDatas.addAll(list)

        if (mHistoryDatas.size == 1) {
            mHistoryDatas[0].isShowDivider = false
        } else {
            mHistoryDatas.last().isShowDivider = false

            mHistoryDatas.forEachIndexed { index, bean ->
                if (index > 0) {
                    val preBean = mHistoryDatas[index - 1]
                    preBean.isShowDivider = bean.groupDate != preBean.groupDate
                }
            }

        }


    }

}