package com.zswc.creativity.vmodel

import android.app.Application
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.zswc.creativity.model.HomeBanner
import com.zswc.creativity.utils.net.NetEngine
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.ysnows.base.utils.UiSwitch
import com.zswc.creativity.activity.CertificationActivity
import com.zswc.creativity.activity.MsgActivity
import com.zswc.creativity.model.MessageListBean
import kotlinx.coroutines.launch

/**
 *Created by shihuiyun
 *on 2020/9/23
 */
open class HomeFirstVModel(application: Application) : BViewModel<BRepository>(application) {

    //默认登录状态
    var isLogin=MutableLiveData<Boolean>(true)

    var bannerList: MutableLiveData<List<HomeBanner>> = MutableLiveData()
    var message=MutableLiveData<String>()


    var is_auth=MutableLiveData<Boolean>(true)



    fun isShowAuth(is_auth:Boolean,isLogin:Boolean):Int{
        if(isLogin&&!is_auth){
            return  View.VISIBLE
        }
        return View.GONE
    }


    //多条件判断  android:visibility="@{vm.isShowPay(vm.bean.is_pay)
//    fun isShowPay(is_pay: String?): Int {
//        if (TextUtils.isEmpty(is_pay)) {
//            return View.GONE
//        }
//        if (is_pay.equals("0")) {
//            return View.VISIBLE
//        }
//        return View.GONE
//    }

    override fun firstReq() {
        super.firstReq()
        toGetBanner()
    }

    fun toCertification(){

        UiSwitch.single(repository().context, CertificationActivity::class.java)

    }

    fun toMsg(){
        UiSwitch.single(repository().context, MsgActivity::class.java)

    }

    fun  toGetBanner(){
        viewModelScope.launch {
            val it = repository().lreq { NetEngine.service.getBanner() }
            if (it.ok()) {
                bannerList.value=it.data
            }
        }
    }

    fun getUserInfo(){

        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.getUser() }
            if(it.ok()) {
                it.data()?.let { user ->
                    user.save()
                }
                is_auth.value = !it.data?.is_auth.equals("0")

            }
        }
    }

    fun getMessageList(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.getMessageList(10,1) }
            if(it.ok()){
                var data=it.data?.list as List<MessageListBean.MessageLis>
                if(data.isNotEmpty()) {
                    message.value = data[0].title
                }
            }

        }
    }


}
