package com.qimai.yttgrass.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.qimai.android.fetch.Fetch
import com.qimai.android.fetch.FetchWebApi
import com.qimai.android.fetch.extention.safeCall
import com.qimai.yttgrass.api.HomeService
import com.qimai.yttgrass.model.SingleLiveEvent
import com.qimai.yttgrass.model.goodsInfoBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import retrofit2.http.Field
import retrofit2.http.Query

class HomeVm : ViewModel() {
    val mCodeMutableList: SingleLiveEvent<String> by lazy {
        SingleLiveEvent<String>()
    }
    private var mPause = false
    val mBannerList = MutableLiveData<ArrayList<String>>()
    var mBackMutableLiveData: MutableLiveData<Boolean> = MutableLiveData<Boolean>()
    var mClick = false
    var mClickMutator: MutableLiveData<Boolean> = MutableLiveData<Boolean>()
    //商品详情
    var mGoodsInfo: goodsInfoBean? = null
    val webapi = FetchWebApi.instance.createApi(HomeService::class.java)

    fun getBannerUrl() = safeCall {
        webapi.getBannerUrl()
    }

    fun getGoodsInfo(
        product_no: String,
        store_multistores_id: String,
        store_id: String
    ) = safeCall {
        webapi.getGoodsInfo(product_no, store_multistores_id, store_id)
    }

    fun getMiniProgramCode(goods_id: String) = safeCall {
        webapi.getMiniProgramCode(goods_id)
    }

    fun countdown() {
        var index = 0
        viewModelScope.launch(Dispatchers.IO) {
            while (true) {
                if (mClick){
                    index = 0
                }
                delay(1000)
                index++
                if (index == 60) {
                    mBackMutableLiveData.postValue(true)
                }
            }

        }
    }

}