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

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cn.jpush.android.api.JPushInterface
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.bean.UserBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.retrofit2.NetworkApi
import com.abbidot.tracker.deprecated.ui.activity.LoginRegisterCNActivity
import com.abbidot.tracker.ui.activity.HomeV2Activity
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.launch
import java.util.*
import javax.inject.Inject

/**
 *Created  by .yzq on 2022/7/15/015.
 * @link
 * @description:
 */
@ActivityRetainedScoped
class LoginCNViewModel @Inject constructor() : ViewModel() {

    val mWXLoginResultLiveData = MutableLiveData<Result<UserBean>>()
    val mWXBindResultLiveData = MutableLiveData<Result<UserBean>>()
    val mPhoneLoginResultLiveData = MutableLiveData<Result<UserBean>>()
    val mAccountPwdLoginResultLiveData = MutableLiveData<Result<UserBean>>()

    /**
     * 微信登录
     */
    fun wxLogin(code: String) {
        viewModelScope.launch {
            val result = NetworkApi.wxLogin(code)
            mWXLoginResultLiveData.value = result
        }
    }

    /**
     * 处理微信、登录成功返回
     */
    fun loginSuccess(activity: LoginRegisterCNActivity,
                     userBean: UserBean,
                     tipRes: Int = R.string.txt_login_successful) {
        //判断有没有绑定手机号
        if (userBean.status == ConstantInt.Close) {
            activity.apply {
                mWXId = userBean.wxId
                switchFragment(LoginRegisterCNActivity.Fragment_Bind_Phone)
            }
        } else {
            //通知欢迎页面关闭
            XEventBus.post(EventName.FinishActivity)
            activity.showToast(tipRes)
            MMKVUtil.putString(MMKVKey.UserId, userBean.userId)
            MMKVUtil.putInt(MMKVKey.isBindDevice, userBean.isBindDevice)
            MMKVUtil.putString(MMKVKey.Token, userBean.token)
            val rand = Random()
            //极光推送设置别名
            JPushInterface.setAlias(activity, rand.nextInt(1000), userBean.userId)

            val intent = Intent()
            //判断有没有绑定设备
//            if (userBean.isBindDevice == ConstantInt.Close) {
//                intent.setClass(activity, AddTrackerDeviceActivity::class.java)
//            } else {
//                intent.setClass(activity, HomeActivity::class.java)
//            }
            intent.setClass(activity, HomeV2Activity::class.java)
            activity.startActivityFinish(intent)
        }
    }

    /**
     * 微信绑定手机号
     */
    fun wxBind(fragment: BaseFragment<*>, phone: String, verificationCode: String, wxId: String) {
        fragment.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.wxBind(phone, verificationCode, wxId)
            mWXBindResultLiveData.value = result
        }
    }


    /**
     * 验证码登录
     */
    fun phoneLogin(fragment: BaseFragment<*>, phone: String, code: String) {
        fragment.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.phoneLogin(phone, code)
            mPhoneLoginResultLiveData.value = result
        }
    }

    /**
     * 验证码登录
     */
    fun accountPasswordLogin(fragment: BaseFragment<*>, phone: String, password: String) {
        fragment.showLoading(true)
        viewModelScope.launch {
            val pwd = Utils.get32Md5Value(password)
            val result = NetworkApi.accountPasswordLogin(phone, pwd)
            mAccountPwdLoginResultLiveData.value = result
        }
    }
}