package com.zswc.creativity.vmodel

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.zswc.creativity.BR
import com.zswc.creativity.R
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.ysnows.base.utils.B
import com.ysnows.base.utils.MMKVManager
import com.ysnows.base.utils.UiSwitch
import com.zswc.creativity.activity.ArticleDetailActivity
import com.zswc.creativity.config.ConfigMMKV
import com.zswc.creativity.inte.ChoiceData
import com.zswc.creativity.utils.PopuTipsUtils
import com.zswc.creativity.utils.net.NetEngine
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit

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

    var codeTip: MutableLiveData<String> = MutableLiveData(ForgetPasswordVModel.getCodeStr)
    private var disposable: Disposable? = null
    var isRead= MutableLiveData<Int>(0)//0未勾选 1已勾选


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

        const val getShow = false


    }


    @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)
        }

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

    @Bindable
    var passWordTo: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.passWordTo)
        }
    @Bindable
    var passWordSeconnd: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.passWordSeconnd)
        }
    @Bindable
    var passWordSeconndTo: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.passWordSeconndTo)
        }
    @Bindable
    var shareCode: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.shareCode)
        }



    fun toAgree(a:Int){

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


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

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

        viewModelScope.launch {

            val it = repository().lreq { NetEngine.service.getPassword("register",phone) }
            if (it.ok(true)) {
                startTimer(start)
            }

        }
    }

    fun toSubmit() {

        //shareCode
        viewModelScope.launch {

            val it = repository().lreq { NetEngine.service.setRegister(phone, code,passWord,passWordTo,passWordSeconnd,passWordSeconndTo,shareCode) }
            if (it.ok(true)) {
                var con = repository().context as Activity
                con.finish()
            }
        }

    }
    fun toLogin(){
        var con = repository().context as Activity
        con.finish()
    }

    private fun startTimer(start: Long = 1) {
        disposeTimer()
        disposable = Observable.intervalRange(start, ForgetPasswordVModel.time - start + 1, 0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext {
                    ForgetPasswordVModel.timeStart = it
                    codeTip.value = if (ForgetPasswordVModel.time - it > 0) "${ForgetPasswordVModel.time - it}秒后重发" else {
                        ForgetPasswordVModel.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()
    }

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

}


