package com.tencent.cloud.tuikit.roomkit.common.utils;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import java.lang.reflect.InvocationTargetException;

public class RTCubeUtils {
    private static final String RTCUBE_PACKAGE_NAME = "com.tencent.trtc";
    private static final String PACKAGE_TENCENT_RTC = "com.tencent.rtc.app";

    public static String getApplicationName(Context context) {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo;
        try {
            packageManager = context.getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
        return TextUtils.isEmpty(applicationName) ? "" : applicationName;
    }

    public static boolean isRTCubeApp(Context context) {
        return RTCUBE_PACKAGE_NAME.equals(context.getPackageName());
    }

    public static boolean isTencentRTC(Context context) {
        return TextUtils.equals(context.getPackageName(), PACKAGE_TENCENT_RTC);
    }

    public static String getPackageName() {
        return getApplicationByReflect().getPackageName();
    }

    public static Application getApplicationByReflect() {
        try {
            @SuppressLint("PrivateApi") Class<?> activityThread = Class.forName("android.app.ActivityThread");
            Object thread = activityThread.getMethod("currentActivityThread").invoke(null);
            Object app = activityThread.getMethod("getApplication").invoke(thread);
            if (app == null) {
                throw new NullPointerException("You should init first.");
            }
            return (Application) app;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        throw new NullPointerException("You should init first.");
    }
}
