package com.swago.login

import android.app.Application
import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.launcher.ARouter
import com.google.gson.Gson
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.http.SwagoException
import com.swago.baseswago.inter.ApiManager
import com.swago.baseswago.util.*
import com.tencent.imsdk.v2.V2TIMCallback
import com.tencent.qcloud.tim.uikit.TUIKit
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack

/**
 *@date 2021/8/27 14:14
 *description:
 */
class LoginVm(application: Application) : BaseViewModel(application) {

    fun login(source:String,token:String){
        requestData2 {
            requestData {
                val data = ApiManager.loginApi.login(source,token)
                UserInfo.setLoginInfo(data)
                SpUtil.putString("login_info", Gson().toJson(data))
                ARouter.getInstance().build(ARouteConstant.Home.home)
                    .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation()
            }

            requestError {
            }
        }
    }

    fun loginByAccount(account: String, pwd: String) {
        requestData2 {
            requestData {
                val data = ApiManager.loginApi.loginByAccount(account,pwd)
                UserInfo.setLoginInfo(data)
                SpUtil.putString("login_info", Gson().toJson(data))
                SwagoLoading.cancelLoadingDialog()
                ARouter.getInstance().build(ARouteConstant.Home.home)
                    .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation()
            }

            requestError {
                if (it is SwagoException){
                    SwagoLoading.cancelLoadingDialog()
                }
            }
        }
    }

}