package com.develop.auth.ui

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.develop.food.base.api.FoodRepository
import com.develop.food.base.common.API_TOKEN
import com.develop.food.base.common.CURRENT_USER_ID_TAG
import com.develop.food.base.common.LOGIN_TAG
import com.develop.food.base.http.observeStatus
import com.develop.food.base.repo.FoodDataProvider
import com.develop.food.base.repo.entity.UserInfo
import com.develop.food.base.utils.MmkvUtils
import com.develop.food.base.utils.ThreadUtils

class MemberViewModel(application: Application) : AndroidViewModel(application) {
    private var verifyCodeKey: String? = null
    val chooseAvatar = MutableLiveData<Int>()
    var enterFromHome: Boolean = false

    fun register(email: String, password: String): LiveData<Boolean> {
        val dispatchLiveData = MutableLiveData<Boolean>()
        FoodRepository.register(email, password).observeStatus(
            onSuccess = {
                login(email, password).observeForever {
                    dispatchLiveData.value = it
                }
            },
            onFailed = {
                it?.desc?.let { msg ->
                    ToastUtils.showShort(msg)
                }
                dispatchLiveData.value = false
            }
        )
        return dispatchLiveData
    }

    fun login(email: String, password: String): LiveData<Boolean> {
        val dispatchLiveData = MutableLiveData<Boolean>()
        FoodRepository.login(email, password).observeStatus(
            onSuccess = {
                MmkvUtils.save(API_TOKEN, it?.token)
                MmkvUtils.save(LOGIN_TAG, true)
                MmkvUtils.save(CURRENT_USER_ID_TAG, it?.user?.id?.toLong() ?: 100001)
                ThreadUtils.runOnWorkThread({
                    val userInfo = UserInfo(
                        it?.user?.id?.toLong() ?: 0L,
                        it?.user?.account,
                        it?.user?.portrait,
                        it?.user?.gender,
                        it?.user?.age ?: 0
                    )
                    FoodDataProvider.getUserDatabase().userInfoDao().updateUserInfo(userInfo)
                })
                dispatchLiveData.value = true
            },
            onFailed = {
                it?.desc?.let { msg ->
                    ToastUtils.showShort(msg)
                }
                dispatchLiveData.value = false
            }
        )
        return dispatchLiveData
    }

    fun sendEmail(email: String): LiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()
        FoodRepository.sendVerifyCode(email).observeStatus(
            onSuccess = {
                verifyCodeKey = it?.verifyCodeKey
                liveData.value = true
            },
            onFailed = {
                it?.desc?.let { msg ->
                    ToastUtils.showShort(msg)
                }
                liveData.value = false
            }
        )
        return liveData
    }

    fun resetPassword(
        email: String,
        captcha: String,
        password: String
    ): LiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()
        FoodRepository.updatePassword(
            email, password, verifyCodeKey ?: "", captcha
        ).observeStatus(
            onSuccess = {
                liveData.value = true
            },
            onFailed = {
                it?.desc?.let { msg ->
                    ToastUtils.showShort(msg)
                }
                liveData.value = false
            }
        )
        return liveData
    }
}