package com.hongyegroup.cpt_parttime.mvvm

import android.annotation.SuppressLint
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import com.guadou.lib_baselib.base.vm.BaseViewModel
import com.guadou.lib_baselib.ext.checkEmpty
import com.hongyegroup.cpt_parttime.BR
import com.hongyegroup.cpt_parttime.adapter.PartTimeJobAdapter
import com.hongyegroup.cpt_parttime.bean.PartJobResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import javax.inject.Inject

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

    val mKeywordLiveData = MutableLiveData<String>()  //搜索Key值
    val mSortTypeLD = MutableLiveData(1)  //排序类型值 1.推荐 2.最新 3.附近
    val mPopupPositionTypeLD = MutableLiveData(0)  //下拉选变色记录索引

    var mCurPage = 1
    private var isNeedPlaceHolder = true
    var isNeedCleanAllData = true
    var mDatas = mutableListOf<PartJobResult>()
    val mAdapter by lazy { PartTimeJobAdapter(BR.item, mDatas) }

    /**
     * 获取兼职工作列表
     */
    fun requestJobList(): LiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()

        launchOnUI {
            //开始Loading
            if (isNeedPlaceHolder) loadStartLoading()
            val result = withContext(Dispatchers.IO) {
                delay(1000)

                val list = mutableListOf<PartJobResult>()
                for (i in 1..10) {
                    if (isNeedCleanAllData) {
                        if (i == 4) list.add(PartJobResult().apply {
                            type = 2
                            groupDate = "2022-03-$mCurPage 星期二"
                        })
                    }
                    list.add(PartJobResult().apply {
                        type = 1
                        groupDate = "2022-03-$mCurPage 星期二"
                    })
                }
                return@withContext list
            }

            loadSuccess()
            handleData(result)
            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("NotifyDataSetChanged")
    private fun handleData(list: List<PartJobResult>?) {
        if (!list.checkEmpty()) {
            //有数据，判断是刷新还是加载更多的数据
            if (isNeedCleanAllData) {
                //刷新的方式
                mDatas.clear()
                mDatas.addAll(list!!)
                mAdapter.notifyDataSetChanged()
            } else {
                //加载更多
                mDatas.addAll(list!!)
                mAdapter.notifyItemRangeInserted(mDatas.size - list.size, list.size)
                mAdapter.loadMoreModule.loadMoreComplete()
            }
        } else {
            //展示无数据
            if (isNeedCleanAllData && mCurPage == 1) {
                mDatas.clear()
                mDatas.add(PartJobResult())  //空视图
                mAdapter.notifyDataSetChanged()
                mAdapter.loadMoreModule.loadMoreEnd()
            } else {
                //如果是加载更多,展示加载完成，没有更多数据了
                mAdapter.loadMoreModule.loadMoreEnd()
            }
        }
        isNeedPlaceHolder = false
        isNeedCleanAllData = false
    }


}