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))
                TUIKit.login(data.user_account,data.user_sig,object:
                    IUIKitCallBack {
                    override fun onSuccess(data: Any?) {

                        ARouter.getInstance().build(ARouteConstant.Home.home)
                            .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation()
                        IMUtil.joinGroup(loginFun)
                    }

                    override fun onError(module: String?, errCode: Int, errMsg: String?) {
                        Toast.makeText(AppContext.getContext(), "IM Login error-$errCode", Toast.LENGTH_SHORT).show()
                    }
                })
            }

            requestError {
            }
        }
    }

    var loginFun:(()->Unit) = {
        UserInfo.getLoginModel()?.let {
            TUIKit.login(it.user_account,it.user_sig,object:
                IUIKitCallBack {
                override fun onSuccess(data: Any?) {
                    ARouter.getInstance().build(ARouteConstant.Home.home)
                        .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation()
                    IMUtil.joinGroup(null)
                }

                override fun onError(module: String?, errCode: Int, errMsg: String?) {
                    Toast.makeText(AppContext.getContext(), "IM Login error-$errCode", Toast.LENGTH_SHORT).show()
                }

            })
        }
    }


    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()
                TUIKit.login(data.user_account,data.user_sig,object:
                    IUIKitCallBack {
                    override fun onSuccess(data: Any?) {
                        ARouter.getInstance().build(ARouteConstant.Home.home).navigation()
                        IMUtil.joinGroup(loginFun)
                    }

                    override fun onError(module: String?, errCode: Int, errMsg: String?) {
                        Toast.makeText(AppContext.getContext(), "Login fail code=$errCode", Toast.LENGTH_SHORT).show()
                    }
                })
            }

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

}