package com.atmob.mobile_use_statistics.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class AppUsageUtil {
    private static final Set<String> SYSTEM_PACKAGES = new HashSet<>(10);
    private static final Map<String, ApplicationInfo> APPLICATION_INFO_CACHE = new java.util.concurrent.ConcurrentHashMap<>();

    public static boolean isSystemApp(Context context,String packageName) {
        if (packageName == null || packageName.isEmpty()) {
            return false;
        }
        if (SYSTEM_PACKAGES.contains(packageName)) {
            return true;
        }
        if (Objects.equals(context.getPackageName(), packageName)) {
            return false;
        }
        try {
            ApplicationInfo applicationInfo = getApplicationInfo(context,packageName);
            if (applicationInfo == null) {
                return false;
            }
            boolean isSystemApp = (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
            if (isSystemApp) {
                SYSTEM_PACKAGES.add(packageName);
                APPLICATION_INFO_CACHE.remove(packageName);
            } else {
                SYSTEM_PACKAGES.remove(packageName);
                APPLICATION_INFO_CACHE.put(packageName, applicationInfo);
            }
            return isSystemApp;
        } catch (Exception e) {
            // If the package is not found, consider it not a system app
            return false;
        }
    }

    public static String getAppName(Context context,String packageName) {
        if (packageName == null || packageName.isEmpty()) {
            return "";
        }
        PackageManager packageManager = context.getPackageManager();
        try {
            ApplicationInfo applicationInfo = getApplicationInfo(context,packageName);
            if (applicationInfo == null) {
                return packageName;
            }
            return packageManager.getApplicationLabel(applicationInfo).toString();
        } catch (Exception e) {
            // If the package is not found, return null
        }
        return packageName;
    }

    public static Drawable getAppIcon(Context context,String packageName) {
        if (packageName == null || packageName.isEmpty()) {
            return null;
        }
        PackageManager packageManager = context.getPackageManager();
        try {
            ApplicationInfo applicationInfo = getApplicationInfo(context,packageName);
            if (applicationInfo == null) {
                return null;
            }
            return packageManager.getApplicationIcon(applicationInfo);
        } catch (Exception e) {
            // If the package is not found, return null
            return null;
        }
    }

    public static ApplicationInfo getApplicationInfo(Context context,String packageName) {
        if (packageName == null || packageName.isEmpty()) {
            return null;
        }
        try {
            if (APPLICATION_INFO_CACHE.containsKey(packageName)) {
                return APPLICATION_INFO_CACHE.get(packageName);
            } else {
                ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
                APPLICATION_INFO_CACHE.put(packageName, applicationInfo);
                return applicationInfo;
            }
        } catch (Exception e) {
            // If the package is not found, return null
            return null;
        }
    }
}
