package com.adealink.weparty.im.util;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import com.adealink.frame.log.Log;
import com.adealink.weparty.im.push.HandleOfflinePushCallBack;
import com.adealink.weparty.im.push.OfflineMessageDispatcher;
import com.adealink.weparty.im.service.TIMAppService;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.qcloud.tuicore.TUIConfig;
import com.tencent.qcloud.tuicore.TUIConstants;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuicore.interfaces.TUILoginConfig;
import com.tencent.qcloud.tuicore.push.OfflinePushExtInfo;
import com.tencent.qcloud.tuicore.util.TUIBuild;
import com.tencent.qcloud.tuikit.timcommon.BuildConfig;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class TUIUtils {
    private static final String TAG = TUIUtils.class.getSimpleName();

    public static String offlineData = null;

    private static boolean hasTryToLogin = false;

    public static void startActivity(String activityName, Bundle param) {
        TUICore.startActivity(activityName, param);
    }

    public static void startChat(String chatId, String chatName, int chatType) {
        Bundle bundle = new Bundle();
        bundle.putString(TUIConstants.TUIChat.CHAT_ID, chatId);
        bundle.putString(TUIConstants.TUIChat.CHAT_NAME, chatName);
        bundle.putInt(TUIConstants.TUIChat.CHAT_TYPE, chatType);
        if (chatType == V2TIMConversation.V2TIM_C2C) {
            TUICore.startActivity(TUIConstants.TUIChat.C2C_CHAT_ACTIVITY_NAME, bundle);
        } else if (chatType == V2TIMConversation.V2TIM_GROUP) {
            TUICore.startActivity(TUIConstants.TUIChat.GROUP_CHAT_ACTIVITY_NAME, bundle);
        }
        // TODO: 2025/11/16  
//        if (AppConfig.DEMO_UI_STYLE == AppConfig.DEMO_UI_STYLE_CLASSIC) {
//            if (chatType == V2TIMConversation.V2TIM_C2C) {
//                TUICore.startActivity(TUIConstants.TUIChat.C2C_CHAT_ACTIVITY_NAME, bundle);
//            } else if (chatType == V2TIMConversation.V2TIM_GROUP) {
//                TUICore.startActivity(TUIConstants.TUIChat.GROUP_CHAT_ACTIVITY_NAME, bundle);
//            }
//        } else {
//            if (chatType == V2TIMConversation.V2TIM_C2C) {
//                TUICore.startActivity("TUIC2CChatMinimalistActivity", bundle);
//            } else if (chatType == V2TIMConversation.V2TIM_GROUP) {
//                TUICore.startActivity("TUIGroupChatMinimalistActivity", bundle);
//            }
//        }
    }
    public static int getCurrentVersionCode(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException ignored) {
            Log.e(TAG, "getCurrentVersionCode exception= " + ignored);
        }
        return 0;
    }

    public static void handleOfflinePush(Intent intent, HandleOfflinePushCallBack callBack) {
        Context context = TIMAppService.getAppContext();
        if (V2TIMManager.getInstance().getLoginStatus() == V2TIMManager.V2TIM_STATUS_LOGOUT) {
            if (TUIConfig.getTUIHostType() == TUIConfig.TUI_HOST_TYPE_RTCUBE) {
                Log.e(TAG, "rtcube not logined");
                Map<String, Object> param = new HashMap<>();
                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;
            }
            Intent intentAction = new Intent();
            intentAction.setAction("com.tencent.qloud.splash");
            intentAction.addCategory("android.intent.category.LAUNCHER");
            intentAction.addCategory("android.intent.category.DEFAULT");
            if (intent != null) {
                intentAction.putExtras(intent);
            }
            intentAction.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intentAction);
            if (callBack != null) {
                callBack.onHandleOfflinePush(false);
            }
            return;
        }

        final OfflinePushExtInfo offlinePushExtInfo = OfflineMessageDispatcher.parseOfflineMessage(intent);
        if (offlinePushExtInfo != null) {
            if (callBack != null) {
                callBack.onHandleOfflinePush(true);
            }
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                manager.cancelAll();
            }

            String senderId = offlinePushExtInfo.getBusinessInfo().getSenderId();
            if (offlinePushExtInfo.getBusinessInfo().getChatAction() == OfflinePushExtInfo.REDIRECT_ACTION_CHAT) {
                if (TextUtils.isEmpty(senderId)) {
                    return;
                }
                TUIUtils.startChat(senderId, offlinePushExtInfo.getBusinessInfo().getSenderNickName(), offlinePushExtInfo.getBusinessInfo().getChatType());
            }
        }
    }

    public static void handleOfflinePush(String ext, HandleOfflinePushCallBack callBack) {
        Log.d(TAG, "handleOfflinePush ext = " + ext);
        final OfflinePushExtInfo offlinePushExtInfo = OfflineMessageDispatcher.getOfflinePushExtInfo(ext);
        if (offlinePushExtInfo == null) {
            return;
        }
        Context context = TIMAppService.getAppContext();
        if (V2TIMManager.getInstance().getLoginStatus() == V2TIMManager.V2TIM_STATUS_LOGOUT && !hasTryToLogin) {
            hasTryToLogin = true;
            if (TUIConfig.getTUIHostType() == TUIConfig.TUI_HOST_TYPE_RTCUBE) {
                offlineData = ext;
                TUICore.notifyEvent(TUIConstants.TIMAppKit.NOTIFY_RTCUBE_EVENT_KEY, TUIConstants.TIMAppKit.NOTIFY_RTCUBE_LOGIN_SUB_KEY, null);
                return;
            }
            Intent intentAction = new Intent();
            intentAction.setAction("com.tencent.qloud.splash");
            intentAction.addCategory("android.intent.category.LAUNCHER");
            intentAction.addCategory("android.intent.category.DEFAULT");
            if (!TextUtils.isEmpty(ext)) {
                intentAction.putExtra(TUIConstants.TIMPush.NOTIFICATION_EXT_KEY, ext);
            }
            intentAction.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intentAction);
            if (callBack != null) {
                callBack.onHandleOfflinePush(false);
            }
            return;
        }

        if (callBack != null) {
            callBack.onHandleOfflinePush(true);
        }
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (manager != null) {
            manager.cancelAll();
        }

        String senderId = offlinePushExtInfo.getBusinessInfo().getSenderId();
        if (offlinePushExtInfo.getBusinessInfo().getChatAction() == OfflinePushExtInfo.REDIRECT_ACTION_CHAT) {
            if (TextUtils.isEmpty(senderId)) {
                return;
            }
            TUIUtils.startChat(senderId, offlinePushExtInfo.getBusinessInfo().getSenderNickName(), offlinePushExtInfo.getBusinessInfo().getChatType());
        }
    }

    public static TUILoginConfig getLoginConfig() {
        TUILoginConfig config = new TUILoginConfig();
        if (BuildConfig.DEBUG) {
            config.setLogLevel(TUILoginConfig.TUI_LOG_DEBUG);
        }
        return config;
    }
}
