package com.zswc.creativity.vmodel

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import android.util.Log
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.zswc.creativity.BR
import com.zswc.creativity.R
import com.zswc.creativity.activity.ForgetPasswordActivity
import com.zswc.creativity.activity.MainActivity
import com.zswc.creativity.activity.RegisterActivity
import com.zswc.creativity.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.ysnows.base.utils.B
import com.ysnows.base.utils.UiSwitch
import com.zswc.creativity.activity.ArticleDetailActivity
import kotlinx.coroutines.launch

open class LoginVModel(application: Application) : BViewModel<BRepository>(application) {

    var one = MutableLiveData<Boolean>(true)
    var eyes = MutableLiveData<Int>(0)//0闭眼 1睁眼

    var isRead= MutableLiveData<Int>(0)//0未勾选 1已勾选


    @Bindable
    open var phone: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.phone)
        }


    @Bindable
    open var passWord: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.passWord)
        }

    fun toRegister() {

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

    }

    fun toForgetPassword() {
        UiSwitch.single(repository().context, ForgetPasswordActivity::class.java)
    }

    fun toAgree(a:Int){

        UiSwitch.bundle(repository().context, ArticleDetailActivity::class.java,
                B().putString("type","2")
                        .putString("chose",a.toString())
                        .ok())
    }

    fun login() {

        if (phone.isNullOrEmpty()) {
            toast(R.string.please_input_user_phone)
            return
        }
        if (passWord.isNullOrEmpty()) {
            toast(R.string.please_input_pwd)
            return
        }
        if(isRead.value==0){
            toast(R.string.please_red_policy)
            return
        }
        viewModelScope.launch {
            val it = repository().lreq { NetEngine.service.getLogin(phone, passWord) }
            if (it.ok(true)) {

                if(TextUtils.isEmpty(it.toString())){
                    toast(it.msg)
                    return@launch
                }
                Log.d("shy", it.data.toString())
                MMKV.defaultMMKV().putString("token", it.data.toString())//保存
               // MMKV.defaultMMKV().putString("token", "test")//保存
                //读取数据
                //MMKV.defaultMMKV().decodeString("token", "")

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

                //  RxBus.get().post(ConfigRx.CLOSE_LOGIN, "")
                (repository().context as Activity).finish()
            }
        }


    }


//    fun goRetrieveWord(){
//
//        UiSwitch.bundle(repository().context as Activity, ForgetPasswordActivity::class.java,
//                B().putString("adminType",adminType).ok())
//
//    }


}
