package com.dieam.reactnativepushnotification.helpers;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import com.facebook.common.logging.FLog;

import me.leolin.shortcutbadger.Badger;
import me.leolin.shortcutbadger.ShortcutBadger;
import me.leolin.shortcutbadger.impl.SamsungHomeBadger;

/**
 * Helper for setting application launcher icon badge counts.
 * <p>
 * This is a wrapper around {@link ShortcutBadger}, with a couple enhancements:
 * <p>
 * - If the first attempt fails, don't retry. This keeps logs clean, as failed attempts are noisy.
 * - Test and apply a separate method for older Samsung devices, which ShortcutBadger has
 * (perhaps over-aggressively) deprecated. ref: https://github.com/leolin310148/ShortcutBadger/issues/40
 */
public class ApplicationBadgeHelper {

    public static final ApplicationBadgeHelper INSTANCE = new ApplicationBadgeHelper();

    private static final String LOG_TAG = "ApplicationBadgeHelper";
    private static final Badger LEGACY_SAMSUNG_BADGER = new SamsungHomeBadger();

    private Boolean applyAutomaticBadger;
    private Boolean applySamsungBadger;
    private ComponentName componentName;

    private ApplicationBadgeHelper() {
    }

    public void setApplicationIconBadgeNumber(Context context, int number) {
        if (null == componentName) {
            componentName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent();
        }
        tryAutomaticBadge(context, number);
        tryLegacySamsungBadge(context, number);
    }

    private void tryAutomaticBadge(Context context, int number) {
        if (null == applyAutomaticBadger) {
            applyAutomaticBadger = ShortcutBadger.applyCount(context, number);
            if (applyAutomaticBadger) {
                FLog.i(LOG_TAG, "First attempt to use automatic badger succeeded; permanently enabling method.");
            } else {
                FLog.i(LOG_TAG, "First attempt to use automatic badger failed; permanently disabling method.");
            }
            return;
        } else if (!applyAutomaticBadger) {
            return;
        }
        ShortcutBadger.applyCount(context, number);
    }

    private void tryLegacySamsungBadge(Context context, int number) {
        // First attempt to apply legacy samsung badge. Check if eligible, then attempt it.
        if (null == applySamsungBadger) {
            applySamsungBadger = isLegacySamsungLauncher(context) && applyLegacySamsungBadge(context, number);
            if (applySamsungBadger) {
                FLog.i(LOG_TAG, "First attempt to use legacy Samsung badger succeeded; permanently enabling method.");
            } else {
                FLog.w(LOG_TAG, "First attempt to use legacy Samsung badger failed; permanently disabling method.");
            }
            return;
        } else if (!applySamsungBadger) {
            return;
        }
        applyLegacySamsungBadge(context, number);
    }

    private boolean isLegacySamsungLauncher(Context context) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

        if (resolveInfo == null || resolveInfo.activityInfo.name.toLowerCase().contains("resolver")) {
            return false;
        }

        String currentHomePackage = resolveInfo.activityInfo.packageName;
        return LEGACY_SAMSUNG_BADGER.getSupportLaunchers().contains(currentHomePackage);
    }

    private boolean applyLegacySamsungBadge(Context context, int number) {
        try {
            LEGACY_SAMSUNG_BADGER.executeBadge(context, componentName, number);
            return true;
        } catch (Exception e) {
            FLog.w(LOG_TAG, "Legacy Samsung badger failed", e);
            return false;
        }
    }
}

@@ -365,64 +365,8 @@
 ger;
-%0Aimport me.leolin.shortcutbadger.impl.SamsungHomeBadger;
 %0A%0A/*
@@ -429,23 +429,16 @@
 counts.%0A
- * %3Cp%3E%0A
  * This 
@@ -483,338 +483,9 @@
 ger%7D
-, with a couple enhancements:%0A * %3Cp%3E%0A * - If the first attempt fails, don't retry. This keeps logs clean, as failed attempts are noisy.%0A * - Test and apply a separate method for older Samsung devices, which ShortcutBadger has%0A * (perhaps over-aggressively) deprecated. ref: https://github.com/leolin310148/ShortcutBadger/issues/40
+:
 %0A */
@@ -684,89 +684,8 @@
 er%22;
-%0A    private static final Badger LEGACY_SAMSUNG_BADGER = new SamsungHomeBadger();
 %0A%0A  
@@ -728,48 +728,8 @@
 er;%0A
-    private Boolean applySamsungBadger;%0A
     
@@ -1108,56 +1108,8 @@
 r);%0A
-        tryLegacySamsungBadge(context, number);%0A
     
@@ -1687,32 +1687,32 @@
 turn;%0A        %7D%0A
+
         Shortcut
@@ -1756,1715 +1756,7 @@
    %7D
-%0A%0A    private void tryLegacySamsungBadge(Context context, int number) %7B%0A        // First attempt to apply legacy samsung badge. Check if eligible, then attempt it.%0A        if (null == applySamsungBadger) %7B%0A            applySamsungBadger = isLegacySamsungLauncher(context) && applyLegacySamsungBadge(context, number);%0A            if (applySamsungBadger) %7B%0A                FLog.i(LOG_TAG, %22First attempt to use legacy Samsung badger succeeded; permanently enabling method.%22);%0A            %7D else %7B%0A                FLog.w(LOG_TAG, %22First attempt to use legacy Samsung badger failed; permanently disabling method.%22);%0A            %7D%0A            return;%0A        %7D else if (!applySamsungBadger) %7B%0A            return;%0A        %7D%0A        applyLegacySamsungBadge(context, number);%0A    %7D%0A%0A    private boolean isLegacySamsungLauncher(Context context) %7B%0A        Intent intent = new Intent(Intent.ACTION_MAIN);%0A        intent.addCategory(Intent.CATEGORY_HOME);%0A        ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);%0A%0A        if (resolveInfo == null %7C%7C resolveInfo.activityInfo.name.toLowerCase().contains(%22resolver%22)) %7B%0A            return false;%0A        %7D%0A%0A        String currentHomePackage = resolveInfo.activityInfo.packageName;%0A        return LEGACY_SAMSUNG_BADGER.getSupportLaunchers().contains(currentHomePackage);%0A    %7D%0A%0A    private boolean applyLegacySamsungBadge(Context context, int number) %7B%0A        try %7B%0A            LEGACY_SAMSUNG_BADGER.executeBadge(context, componentName, number);%0A            return true;%0A        %7D catch (Exception e) %7B%0A            FLog.w(LOG_TAG, %22Legacy Samsung badger failed%22, e);%0A            return false;%0A        %7D%0A    %7D
 %0A%7D%0A

Remove try Samsung Badger code due to update in ShortcutBadger