package com.abbidot.tracker.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.bean.CountryCodeBean
import com.abbidot.tracker.bean.UserBean
import com.abbidot.tracker.bean.UserInfoBean
import com.abbidot.tracker.retrofit2.NetworkApi
import com.abbidot.tracker.util.Util
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/2/14/014.
 * @link
 * @description:注册ViewModel
 */
class UserProfileViewModel : ViewModel() {

    val mGetEmailCodeLiveData = MutableLiveData<Result<UserBean>>()
    val mValidateCodeLiveData = MutableLiveData<Result<String>>()
    private val mRegisterLiveData = MutableLiveData<Result<String>>()
    val mCountryCodeLiveData = MutableLiveData<Result<MutableList<CountryCodeBean>>>()
    val mRegisterV2LiveData = MutableLiveData<Result<UserBean>>()
    val mUpdateUserInfoLiveData = MutableLiveData<Result<String>>()
    val mUploadImageLiveData = MutableLiveData<Result<String>>()
    val mUserInfoLiveData = MutableLiveData<Result<UserInfoBean>>()
    val mUserLogoffLiveData = MutableLiveData<Result<String>>()
    val mUpdateMeasureUnitLiveData = MutableLiveData<Result<String>>()

    /**
     * 获取邮箱验证码
     * @param type 验证码类型 1是注册 2是忘记密码 3修改邮箱
     */
    fun getEmailCode(activity: BaseActivity<*>, email: String, type: Int) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.requestVerificationCode(email, type)
            mGetEmailCodeLiveData.value = result
        }
    }

    /**
     * 验证码验证
     */
    fun validateCode(fragment: BaseFragment<*>, email: String, emailCode: String, type: Int) {
        fragment.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.validateCode(email, emailCode, type)
            mValidateCodeLiveData.value = result
        }
    }

    /**
     * 注册
     */
    fun register(fragment: BaseFragment<*>, email: String, password: String, userName: String) {
        fragment.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.register(email, password, userName)
            mRegisterLiveData.value = result
        }
    }

    /**
     * 获取国家代码号
     */
    fun getCountryCodes(activity: BaseActivity<*>) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getCountryCodes()
            mCountryCodeLiveData.value = result
        }
    }

    /**
     * 注册/创建用户
     */
    fun registerV2(
        activity: BaseActivity<*>,
        email: String,
        password: String,
        userName: String,
        countryCode: String
    ) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.registerV2(email, password, userName, countryCode)
            mRegisterV2LiveData.value = result
        }
    }

    /**
     * 更新用户信息
     */
    fun updateUserInfo(
        activity: BaseActivity<*>,
        email: String,
        imgurl: String,
        countryCode: String,
        username: String
    ) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.updateUser(email, userId, imgurl, countryCode, username)
            mUpdateUserInfoLiveData.value = result
        }
    }

    /**
     * 上传头像
     */
    fun uploadImage(activity: BaseActivity<*>, pathImg: String) {
        activity.runOnUiThread {
            activity.showLoading(true)
        }
        viewModelScope.launch {
            val result = NetworkApi.uploadImage(Util.uploadFile(pathImg))
            mUploadImageLiveData.value = result
        }
    }


    /**
     * 获取用户信息
     */
    fun getUserInfo(activity: BaseActivity<*>) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.getUserInfo(userId)
            mUserInfoLiveData.value = result
        }
    }


    /**
     * 用户注销
     */
    fun logoff(activity: BaseActivity<*>) {
        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.logoff(userId)
            mUserLogoffLiveData.value = result
        }
    }

    /**
     *修改单位
     */
    fun updateMeasureUnit(measurementUnit: Int) {
//        activity.showLoading(true)
        viewModelScope.launch {
            val userId = MMKVUtil.getString(MMKVKey.UserId)
            val result = NetworkApi.updateMeasureUnit(userId, measurementUnit)
            mUpdateMeasureUnitLiveData.value = result
        }
    }
}