package com.quansu.heifengwuliu.vmodel

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.repository.RegisterRepository
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 RegisterVModel : BViewModel<RegisterRepository>() {
    var codeTip: MutableLiveData<String> = MutableLiveData(getCodeStr)
    private var disposable: Disposable? = null

    var showType: MutableLiveData<Boolean> = MutableLiveData(getShow)

    var open_id: String? = null


    override fun init() {
        super.init()
        timeStart = MMKVManager.instance().decodeLong(ConfigMMKV.REGISTER_TIME_START, 60)
        if (timeStart < time) {
            startTimer(timeStart)
        }

    }

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


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

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





    fun register() {

        //todo：

       // var ss=showType.value//choseType

        var type=if(showType.value!!)"2" else "1"

        repository()
                .register(userName,phone, code, type,open_id)
                ?.doOnNext() {
                    toast(it.msg())
                }
                ?.subscribe()


    }

    fun choseType(type:Int){
        showType.value = type==1

    }
    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.getRegisterSms(phone))
                .doOnNext() { res ->
                    if (res.ok(true)) {
                        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()
    }


    override fun onCleared() {
        super.onCleared()
        disposeTimer()
    }

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

        const val getShow = true



    }

    private fun disposeTimer() {
        disposable?.isDisposed ?: disposable?.dispose()
    }
}

