package com.swago.baseswago.util

import android.content.Intent
import android.util.Log
import android.widget.Toast
import com.alibaba.android.arouter.launcher.ARouter
import com.swago.baseswago.constant.ARouteConstant
import com.tencent.imsdk.common.IMCallback
import com.tencent.imsdk.v2.*

/**
 *@date 2021/8/28 13:55
 *description:
 */
object IMUtil {

    fun addToBlackList(list:List<String>,call:()->Unit){
        V2TIMManager.getFriendshipManager().addToBlackList(list,object:V2TIMValueCallback<List<V2TIMFriendOperationResult>>{
            override fun onSuccess(t: List<V2TIMFriendOperationResult>?) {
                Toast.makeText(
                    AppContext.getContext(),
                    "Add to black list success",
                    Toast.LENGTH_SHORT
                ).show()
                call.invoke()
            }

            override fun onError(code: Int, desc: String?) {
                Toast.makeText(AppContext.getContext(), "black fail code-$code", Toast.LENGTH_SHORT).show()
            }

        })
    }


    fun deleteBlackList(list:List<String>,call:()->Unit){
        V2TIMManager.getFriendshipManager().deleteFromBlackList(list,object:V2TIMValueCallback<List<V2TIMFriendOperationResult>>{
            override fun onSuccess(t: List<V2TIMFriendOperationResult>?) {
                Toast.makeText(
                    AppContext.getContext(),
                    "delete the user from black list success",
                    Toast.LENGTH_SHORT
                ).show()
                call.invoke()
            }

            override fun onError(code: Int, desc: String?) {
                Log.d("hhq","$code")
            }

        })
    }



    fun checkIsLogin():Boolean{
        if (V2TIMManager.getInstance().loginUser.isNullOrEmpty()){
            return false
        }
        return true
    }

    fun joinGroup(loginCallback:(()->Unit)?){
        if (checkIsLogin()){
            //已登录 注释掉是因为下一次登录的时候收不到群消息
//            V2TIMManager.getInstance().quitGroup(UserInfo.getLoginModel()!!.group_id,null)
            V2TIMManager.getInstance().joinGroup(UserInfo.getLoginModel()!!.group_id, "",object:V2TIMCallback{
                override fun onSuccess() {
                    LogUtil.d("加入大群成功---${UserInfo.getLoginModel()!!.group_id}")
                }

                override fun onError(code: Int, desc: String?) {
                    Toast.makeText(AppContext.getContext(), "IM join group error-$code", Toast.LENGTH_SHORT).show()
                }

            })
        }else{
            //未登录成功
            loginCallback?.invoke()
        }
    }
}