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.adapter.GroomingAdapter
import com.hongyegroup.cpt_parttime.bean.HowToReportBean
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import javax.inject.Inject

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

    var mDatas = mutableListOf<HowToReportBean>()
    val mAdapter by lazy { GroomingAdapter(mDatas) }

    /**
     * 获取工作流程数据
     */
    fun getGroomingData(): LiveData<Boolean> {

        val liveData = MutableLiveData<Boolean>()

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

                val list = mutableListOf<HowToReportBean>()

                //1
                list.add(HowToReportBean().apply {
                    title = "着装"
                    img = listOf(
                        "http://p1.itc.cn/images01/20210923/62fa580f42064121801a9fafa22e7a85.jpeg",
                        "http://5b0988e595225.cdn.sohucs.com/images/20190610/3dfb601e88a9400692ab81fbd27dc11a.jpeg",
                        "http://i01piccdn.sogoucdn.com/18bf7b5e086dc7ba",
                        "http://p0.itc.cn/q_70/images01/20211227/d7e9ac2a317843509a916e21df4b145c.png",
                    )
                })

                //2
                list.add(HowToReportBean().apply {
                    important_notice = "<h4 color='#333333'>请执行以下操作</h4>" +
                            "<h5 color='#000000'>收好你的制服</h5>" +
                            "<font color='#575757'>--从保安处进去左转然后右转然后在你左边的门的尽头就是制服间</font><br/>" +
                            "<h5 color='#000000'>收好你的制服</h5>" +
                            "<font color='#575757'>--男女更衣室位于制服间附近</font><br/>" +
                            "<h5 color='#000000'>在大厅报告</h5>" +
                            "<font color='#575757'>--向警卫室走去，你会看到一个婴儿从你身边抬起。乘电梯到1楼。有一扇棕色的门可以穿过。</font>"
                })

                return@withContext list
            }

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

        }

        return liveData
    }

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

}