package com.abbidot.tracker.deprecated.ui.fragment.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/7/18/018.
 * @link
 * @description:
 */
class RegisterCNViewModel : ViewModel() {
    val mSendCodeResultLiveData = MutableLiveData<Result<String>>()
    val mValidateCodeResultLiveData = MutableLiveData<Result<String>>()
    val mPhoneRegisterResultLiveData = MutableLiveData<Result<String>>()
    val mSetNewPasswordResultLiveData = MutableLiveData<Result<String>>()

    /**
     * 发送验证码
     * @param type 发送类型 1是验证码登录 2微信绑定 3注册 4忘记密码
     */
    fun sendCode(fragment: BaseFragment<*>, phone: String, type: Int) {
        fragment.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.sendCode(phone, type)
            mSendCodeResultLiveData.value = result
        }
    }

    /**
     * 手机号验证码验证
     */
    fun validatePhoneCode(fragment: BaseFragment<*>, phone: String, code: String, type: Int) {
        fragment.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.validatePhoneCode(phone, code, type)
            mValidateCodeResultLiveData.value = result
        }
    }

    /**
     * 注册
     */
    fun register(fragment: BaseFragment<*>, phone: String, password: String) {
        fragment.showLoading(true)
        viewModelScope.launch {
            val pwd = Utils.get32Md5Value(password)
            val result = NetworkApi.register(phone, pwd)
            mPhoneRegisterResultLiveData.value = result
        }
    }

    /**
     * 重置密码
     */
    fun resetNewPassword(fragment: BaseFragment<*>, phone: String, password: String) {
        fragment.showLoading(true)
        viewModelScope.launch {
            val pwd = Utils.get32Md5Value(password)
            val result = NetworkApi.resetNewPassword(phone, pwd)
            mSetNewPasswordResultLiveData.value = result
        }
    }
}