package com.quyunshuo.module.home.fragment.vm

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
import com.quyunshuo.androidbaseframemvvm.common.constant.MqName
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.fragment.repo.HomeFragmentRepo
import com.quyunshuo.module.home.model.UserPassword
import com.quyunshuo.module.home.model.livedata.LiveDataBean
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * HomeFragment 的 VM 层
 *
 * @author Qu Yunshuo
 * @since 2021/8/5 11:11 下午
 */
@HiltViewModel
class UserLoginFragmentVM @Inject constructor(private val mRepo: HomeFragmentRepo) :
    BaseViewModel() {

    private val _checkResult = MutableLiveData<LiveDataBean<String>>()
    val checkResult: LiveData<LiveDataBean<String>> = _checkResult

    private val TAG = "UserLoginFragmentVM"


    /**
     * 获取用户密码
     */
    fun getUserInfo() {
        viewModelScope.launch(Dispatchers.IO) {
            mRepo.getPwd()
                .catch {
                    Log.d(TAG, "getUserInfo catch: " + it.message)
                }
                .collect {
                    val gson: Gson = Gson()
                    val userPassword = gson.fromJson(it, UserPassword::class.java)
                    SpUtils.put(MqName.ADMIN_PWD, userPassword.adminPwd.trim())
                    SpUtils.put(MqName.GUEST_PWD, userPassword.guestPwd.trim())
                    Log.d(TAG, "getUserInfo collect: " + userPassword.adminPwd)
                    Log.d(TAG, "getUserInfo collect: " + userPassword.guestPwd)
                }
        }

    }

    /**
     * 密码检查是否正确
     */
    fun checkUser(userName: String, userPassword: String) {
        val adminUser = SpUtils.getString(MqName.ADMIN_PWD, "123456")
        val guestUser = SpUtils.getString(MqName.GUEST_PWD, "123456")
        if ("Admin" == userName) {
            if (adminUser != userPassword) {
                //提示用户
                _checkResult.value =
                    LiveDataBean<String>(UiUtil.getStringRes(R.string.base_home_login_tips_1), 0, "")
                return
            }
        } else {
            if (SpUtils.getBoolean(MMKVName.STAFF_LOGIN, false) == false) {
                //提示用户
                _checkResult.value =
                    LiveDataBean<String>(UiUtil.getStringRes(R.string.base_staff_login_tips), 0, "")
                return
            }
            if (!guestUser.equals(userPassword)) {
                //提示用户
                _checkResult.value =
                    LiveDataBean<String>(UiUtil.getStringRes(R.string.base_home_login_tips_1), 0, "")
                return
            }
        }
        //成功了 切换页面
        _checkResult.value =
            LiveDataBean<String>(UiUtil.getStringRes(R.string.base_home_login_tips_2), 1, "")


    }

    /**
     * 密码检查是否正确，正确则有返回值
     */
    fun checkUserReturn(userName: String, userPassword: String): Boolean {
        val adminUser = SpUtils.getString(MqName.ADMIN_PWD, "123456")
        val guestUser = SpUtils.getString(MqName.GUEST_PWD, "123456")
        if ("Admin" == userName) {
            if (adminUser != userPassword) {
                //提示用户
                return false
            }
        } else {

            if (!guestUser.equals(userPassword)) {
                //提示用户
                return false
            }
        }
        //成功了 切换页面
        return true

    }
}