package com.quyunshuo.module.home.activity.main

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.quyunshuo.androidbaseframemvvm.base.ktx.launchIO
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.bean.ProductDataBean
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
import com.quyunshuo.androidbaseframemvvm.common.util.ProductAbout
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import javax.inject.Inject


/**
 * 首页的VM层
 *
 * @property mRepository HomeRepository 仓库层 通过Hilt注入
 *
 * @author Qu Yunshuo
 * @since 5/25/21 5:41 PM
 */
private const val TAG = "MainViewModel"
@HiltViewModel
class MainViewModel @Inject constructor(private val mRepository: MainRepository) : BaseViewModel() {

    val data = MutableLiveData<String>()

    /**
     * 模拟获取数据
     */
    fun getData() {
        launchIO {
            mRepository.getData()
                .catch { Log.d("qqq", "getData: $it") }
                .collect { data.postValue(it) }
        }
    }
    //初始化商品数据

    fun initProductData() {
        /*val productDataContains = SpUtils.contains(MMKVName.PRODUCT_DATA)
        //如果不存在 创建默认的
        if (!productDataContains!!) {
            val goodsBeans: ArrayList<ProductDataBean> = ArrayList<ProductDataBean>()
            for (i in 0 until ProductAbout.namesS.size) {

                val productDataBean: ProductDataBean = ProductDataBean(
                    i,
                    ProductAbout.[i],
                    0.0,
                    ProductAbout.namesS[i],
                    ProductAbout.imgIdsS[i],
                    ProductAbout.chineseNameS[i],
                    true,
                    "",
                    i,
                    ""
                )
                goodsBeans.add(productDataBean)
            }
            Log.d("backinfo","商品："+Gson().toJson(goodsBeans))
            SpUtils.put(MMKVName.PRODUCT_DATA,Gson().toJson(goodsBeans))
        }*/
    }
}