package com.swago.app

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.IMUtil
import com.swago.baseswago.util.SpUtil
import com.swago.baseswago.util.UserInfo
import com.tencent.imsdk.v2.V2TIMCallback
import com.tencent.qcloud.tim.uikit.TUIKit
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack

class SplashActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        if (UserInfo.getLoginModel() == null){
            ARouter.getInstance().build(ARouteConstant.Login.login).navigation()
            finish()
        }else{
            TUIKit.login(UserInfo.getLoginModel()!!.user_account,UserInfo.getLoginModel()!!.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?) {
                    Log.d("ddd","$errCode")
                }
            })
        }

    }


    var loginFun:(()->Unit) = {
        UserInfo.getLoginModel()?.let {
            TUIKit.login(it.user_account,it.user_sig,object:
                IUIKitCallBack {
                override fun onSuccess(data: Any?) {
                    IMUtil.joinGroup(null)
                }

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

            })
        }
    }
}