package com.hongyegroup.cpt_parttime.mvvm

import android.annotation.SuppressLint
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import com.chad.library.adapter.base.entity.node.BaseNode
import com.guadou.lib_baselib.base.vm.BaseViewModel
import com.guadou.lib_baselib.ext.checkEmpty
import com.hongyegroup.cpt_parttime.adapter.PartDetailQAAdapter
import com.hongyegroup.cpt_parttime.bean.PartDetailAnswer
import com.hongyegroup.cpt_parttime.bean.PartDetailQuestion
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import javax.inject.Inject

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

    var mDatas = mutableListOf<BaseNode>()
    val mAdapter by lazy { PartDetailQAAdapter() }

    /**
     * 请求FQA的数据
     */
    fun requestJobFaqs(): LiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()

        launchOnUI {
            loadStartLoading()
            val result = withContext(Dispatchers.IO) {
                delay(1000)

                val list = mutableListOf<BaseNode>()
                //1
                list.add(PartDetailQuestion().apply {
                    question = "工作地点在哪里？"
                    childNode = listOf(PartDetailAnswer().apply {
                        answer = "新加坡鱼尾狮公园富勒顿一号富勒顿路1号， 邮编：049213"
                    })
                })

                //2
                list.add(PartDetailQuestion().apply {
                    question = "工作服是什么样的？"
                    childNode = listOf(PartDetailAnswer().apply {
                        answer = "工作服是酒店提供的哦"
                    })
                })

                //3
                list.add(PartDetailQuestion().apply {
                    question = "工作地点在哪里？"
                    childNode = listOf(PartDetailAnswer().apply {
                        answer = "新加坡鱼尾狮公园富勒顿一号富勒顿路1号， 邮编：049213"
                    })
                })

                return@withContext list
            }

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

        }

        return liveData
    }

    //处理数据-添加或刷新
    @SuppressLint("NotifyDataSetChanged")
    private fun handleData(list: List<BaseNode>?) {
        if (!list.checkEmpty()) {
            mDatas.clear()
            mDatas.addAll(list!!)
            mAdapter.setList(mDatas)
        } else {
            //展示无数据
            mDatas.clear()
            mAdapter.setList(emptyList())
        }
    }

}