package com.adealink.weparty.im.util

import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.adealink.frame.log.Log
import com.adealink.frame.router.Router
import com.adealink.weparty.im.push.OfflineMessageDispatcher
import com.adealink.weparty.im.service.TIMAppService
import com.adealink.weparty.module.im.IM
import com.tencent.qcloud.tuicore.interfaces.TUILoginConfig
import com.tencent.qcloud.tuicore.push.OfflinePushExtInfo
import com.tencent.qcloud.tuikit.timcommon.BuildConfig

object TUIUtils {
    private val TAG: String = TUIUtils::class.java.getSimpleName()

    fun handleOfflinePush(intent: Intent?) {
        val context: Context = TIMAppService.getAppContext()
        //登录状态过期/失效
//        if (V2TIMManager.getInstance().loginStatus == V2TIMManager.V2TIM_STATUS_LOGOUT) {
//            if (TUIConfig.getTUIHostType() == TUIConfig.TUI_HOST_TYPE_RTCUBE) {
//                Log.e(TAG, "rtcube not logined")
//                val param: MutableMap<String?, Any?> = HashMap<String?, Any?>()
//                param.put(TUIConstants.TIMAppKit.OFFLINE_PUSH_INTENT_DATA, intent)
//                TUICore.notifyEvent(
//                    TUIConstants.TIMAppKit.NOTIFY_RTCUBE_EVENT_KEY,
//                    TUIConstants.TIMAppKit.NOTIFY_RTCUBE_LOGIN_SUB_KEY,
//                    param
//                )
//                return
//            }
//            Log.i(TAG, "handleOfflinePush, logout, goHome")
//            Router.build(context, AppModule.Main.PATH).apply {
//                addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
//                putExtra(AppModule.Main.EXTRA_MAIN_TAB, MainTab.MESSAGE.tab)
//                if (intent != null) {
//                    putExtras(intent)
//                }
//            }.start()
//            return
//        }

        val offlinePushExtInfo = OfflineMessageDispatcher.parseOfflineMessage(intent)
        if (offlinePushExtInfo != null) {
            val manager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
            manager?.cancelAll()

            val senderId = offlinePushExtInfo.businessInfo.senderId
            if (offlinePushExtInfo.businessInfo.chatAction == OfflinePushExtInfo.REDIRECT_ACTION_CHAT) {
                if (senderId.isNullOrEmpty()) {
                    return
                }
                Log.i(TAG, "handleOfflinePush, goSession($senderId)")
                val intent = Intent().apply {
                    setData(
                        Uri.parse(Router.getDeepLink(IM.Session.PATH)).buildUpon().apply {
                            appendQueryParameter(IM.Session.EXTRA_CHAT_ID, senderId)
                        }.build()
                    )
                    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                context.startActivity(intent)
            }
        }
    }

    fun handleOfflinePush(ext: String?) {
        Log.d(TAG, "handleOfflinePush ext = $ext")
        val offlinePushExtInfo: OfflinePushExtInfo? =
            OfflineMessageDispatcher.getOfflinePushExtInfo(ext)
        if (offlinePushExtInfo == null) {
            return
        }
        val context = TIMAppService.getAppContext()
        //登录状态过期/失效
//        if (V2TIMManager.getInstance().loginStatus == V2TIMManager.V2TIM_STATUS_LOGOUT) {
//            if (TUIConfig.getTUIHostType() == TUIConfig.TUI_HOST_TYPE_RTCUBE) {
//                TUICore.notifyEvent(
//                    TUIConstants.TIMAppKit.NOTIFY_RTCUBE_EVENT_KEY,
//                    TUIConstants.TIMAppKit.NOTIFY_RTCUBE_LOGIN_SUB_KEY,
//                    null
//                )
//                return
//            }
//            Log.i(TAG, "handleOfflinePush, logout, goHome")
//            Router.build(context, AppModule.Main.PATH).apply {
//                addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
//                putExtra(AppModule.Main.EXTRA_MAIN_TAB, MainTab.MESSAGE.tab)
//                putExtra(TUIConstants.TIMPush.NOTIFICATION_EXT_KEY, ext)
//            }.start()
//            return
//        }

        val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
        manager?.cancelAll()

        val senderId = offlinePushExtInfo.businessInfo.senderId
        if (offlinePushExtInfo.businessInfo.chatAction == OfflinePushExtInfo.REDIRECT_ACTION_CHAT) {
            if (senderId.isNullOrEmpty()) {
                return
            }
            Log.i(TAG, "handleOfflinePush, goSession($senderId)")
            val intent = Intent().apply {
                setData(
                    Uri.parse(Router.getDeepLink(IM.Session.PATH)).buildUpon().apply {
                        appendQueryParameter(IM.Session.EXTRA_CHAT_ID, senderId)
                    }.build()
                )
                addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }

    fun getLoginConfig(): TUILoginConfig {
        val config = TUILoginConfig()
        if (BuildConfig.DEBUG) {
            config.logLevel = TUILoginConfig.TUI_LOG_DEBUG
        }
        return config
    }
}
