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.cs_cptservices.binding.BaseDataBindingAdapter
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.R
import com.hongyegroup.cpt_parttime.bean.PartJobResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import javax.inject.Inject

@SuppressLint("NotifyDataSetChanged")
@HiltViewModel
class PartTimeHomeViewModel @Inject constructor(
    private val savedStateHandle: SavedStateHandle
) : BaseViewModel() {

    var mFilterIndustryIds: ArrayList<String>? = null   //筛选的行业Ids

    val mKeywordLiveData = MutableLiveData<String>()  //搜索Key值
    var mCurPage = 1
    var isNeedPlaceHolder = true
    var isNeedCleanAllData = true

    var mNewsJobDataes = mutableListOf<String>()
    val mNewJobAdapter by lazy { BaseDataBindingAdapter(R.layout.item_part_time_new_job, BR.item, mNewsJobDataes) }

    var mAllJobDataes = mutableListOf<PartJobResult>()
    val mAllJobAdapter by lazy { BaseDataBindingAdapter(R.layout.item_part_time_jobs, BR.item, mAllJobDataes) }

    /**
     * 获取兼职工作列表
     */

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

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


            async {
                if (mCurPage == 1) {
                    mNewsJobDataes.clear()
                    for (i in 1..5) {
                        mNewsJobDataes.add(i.toString())
                    }
                }
            }
            mNewJobAdapter.notifyDataSetChanged()

            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
            }


            handleData(result)
            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
    }

    //处理数据-添加或刷新
    private fun handleData(list: List<PartJobResult>?) {
        if (!list.checkEmpty()) {
            //有数据，判断是刷新还是加载更多的数据
            if (isNeedCleanAllData) {
                //刷新的方式
                mAllJobDataes.clear()
                mAllJobDataes.addAll(list!!)
                mAllJobAdapter.notifyDataSetChanged()
            } else {
                //加载更多
                mAllJobDataes.addAll(list!!)
                mAllJobAdapter.notifyItemRangeInserted(mAllJobDataes.size - list.size, list.size)
                mAllJobAdapter.loadMoreModule.loadMoreComplete()
            }
        } else {
            //展示无数据
            if (isNeedCleanAllData && mCurPage == 1) {
                mAllJobDataes.clear()
                mAllJobDataes.add(PartJobResult())  //空视图
                mAllJobAdapter.notifyDataSetChanged()
                mAllJobAdapter.loadMoreModule.loadMoreEnd()
            } else {
                //如果是加载更多,展示加载完成，没有更多数据了
                mAllJobAdapter.loadMoreModule.loadMoreEnd()
            }
        }
        isNeedPlaceHolder = false
        isNeedCleanAllData = false
    }


}