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.How2ReportAdapter
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 PartJobDetailProcessViewModel @Inject constructor(
    private val savedStateHandle: SavedStateHandle
) : BaseViewModel() {

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

    /**
     * 获取工作流程数据
     */
    fun getHowReportData(): 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://i02piccdn.sogoucdn.com/4fad16afd3acd5b6",
                        "http://img.zcool.cn/community/019241565fa3df6ac7255d2e15560a.jpg@1280w_1l_2o_100sh.jpg",
                        "http://img.zcool.cn/community/0158a658a2b836a801219c77059a95.png@1280w_1l_2o_100sh.png"
                    )
                })

                //2
                list.add(HowToReportBean().apply {
                    title = "接送车信息"
                    img = listOf(
                        "http://i02piccdn.sogoucdn.com/254b0d3e6cb288f0",
                        "http://img.zcool.cn/community/0170755a65d091a80120a123ad53ba.jpg@1280w_1l_2o_100sh.jpg",
                    )
                })

                //3
                list.add(HowToReportBean().apply {
                    title = "汇报过程"
                    img = listOf(
                        "http://i02piccdn.sogoucdn.com/71390d1ade651f3a",
                        "http://img.zcool.cn/community/01066c5a97ad44a801206d965f0e52.jpg@1280w_1l_2o_100sh.jpg",
                        "http://img.zcool.cn/community/014f76565fa3da32f875ae349f0386.jpg@1280w_1l_2o_100sh.jpg",
                        "http://img.zcool.cn/community/0160855ac4e9c6a801212573507ad7.png@1280w_1l_2o_100sh.png",
                        "http://img.zcool.cn/community/01abe358a2b7c6a801219c779bb54a.png@1280w_1l_2o_100sh.png",
                        "http://i02piccdn.sogoucdn.com/69e667b2e752bdf7",
                    )
                    text = "一起来汇报吧！！"
                })

                //4
                list.add(HowToReportBean().apply {
                    title = "重要通知"
                    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()
        }
    }

}