package com.quansu.heifengwuliu.vmodel

import android.view.View
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import com.quansu.heifengwuliu.BR
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.config.ConfigMMKV
import com.quansu.heifengwuliu.model.SendSmsBean
import com.quansu.heifengwuliu.repository.LoginRepository
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.ysnows.base.base.BViewModel
import com.ysnows.base.utils.MMKVManager
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

open class LoginVModel : BViewModel<LoginRepository>() {


    var passwordVisiblity = MutableLiveData<Int>()
    var codeVisibility = MutableLiveData<Int>()
    var switchType= MutableLiveData<String>()

    var codeTip: MutableLiveData<String> = MutableLiveData(getCodeStr)
    private var disposable: Disposable? = null

    private var showType=true

    init {
        passwordVisiblity.value=View.GONE
        codeVisibility.value=View.VISIBLE
        switchType.value="使用密码登录"
    }

    companion object {
        const val time = 60
        var timeStart: Long = 1
        const val getCodeStr = "获取验证码"



    }



    fun getSmsCode(start: Long = 1) {
        if (codeTip.value != getCodeStr) {
            return
        }

        if (phone.isNullOrEmpty()) {
            toast(R.string.please_input_user_phone)
            return
        }

        repository().lreq(NetEngine.service.getRegisterCode(phone))
                .doOnNext() { res ->
                    if (res.ok(true)) {
//                        var bean: SendSmsBean = res.data() as SendSmsBean
//                        code=bean.code
                        startTimer(start)
                    }
                }
                .subscribe()
    }

    private fun startTimer(start: Long = 1) {
        disposeTimer()
        disposable = Observable.intervalRange(start, time - start + 1, 0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext {
                    timeStart = it
                    codeTip.value = if (time - it > 0) "${time - it}秒后重发" else {
                        getCodeStr
                    }
                }
                .doOnComplete() {
                    MMKVManager.instance().encode(ConfigMMKV.REGISTER_TIME_START, 60)
                }
                .doOnDispose() {
                    MMKVManager.instance().encode(ConfigMMKV.REGISTER_TIME_START, 60)
                }
                .subscribe()
    }


    private fun disposeTimer() {
        disposable?.isDisposed ?: disposable?.dispose()
    }
    @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)
        }

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



    fun login() {
        //todo:
        if(showType){//验证码登录

            repository().loginCode(phone, code)

        }else{//密码登录
            repository().login(phone, passWord)

        }

    }
    fun setShowType(){

        if(showType){//验证码显示-密码显示
            passwordVisiblity.value=View.VISIBLE
            codeVisibility.value=View.GONE
            switchType.value="使用验证码登录"

        }else{//密码显示-验证码
            passwordVisiblity.value=View.GONE
            codeVisibility.value=View.VISIBLE
            switchType.value="使用密码登录"
        }
        showType=!showType
    }


}
