package com.gh.gamecenter.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.gh.common.util.InstallUtils;
import com.gh.common.util.PackageHelper;
import com.gh.common.util.PackageUtils;
import com.gh.download.PackageObserver;
import com.gh.gamecenter.common.constant.Constants;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.core.AppExecutor;
import com.gh.gamecenter.core.utils.SPUtils;
import com.gh.gamecenter.eventbus.EBPackage;
import com.halo.assistant.HaloApp;
import com.lightgame.utils.Utils;

import org.greenrobot.eventbus.EventBus;


/**
 * 监听应用安装和卸载的广播
 *
 * @author LGT 黄壮华
 */
public class InstallAndUninstallReceiver extends BroadcastReceiver {

    private static final String webviewPackageName = "com.google.android.webview";

    @Override
    public void onReceive(Context context, Intent intent) {
        PackageUtils.dumpInstalledListCache();
        ExtensionsKt.doOnMainProcessOnly(context, () -> {
            Utils.log("InstallAndUninstallReceiver:: onReceive->" + intent.getAction() + "==" + intent.getDataString());
            // 接收安装广播
            if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
                String packageName = intent.getDataString();
                packageName = packageName.substring(packageName.indexOf(":") + 1);
                Utils.log("安装了:" + packageName + "包名的程序");
                InstallUtils.getInstance().removeInstall(packageName);
                PackageHelper.refreshLocalPackageList();

                String versionName = PackageUtils.getVersionNameByPackageName(packageName);
                EBPackage installEb = new EBPackage(EBPackage.TYPE_INSTALLED, packageName, versionName);
                if (PackageUtils.isAppOnForeground(context)) {
                    PackageObserver.onPackageChanged(installEb);
                    EventBus.getDefault().post(installEb);
                } else {
                    // 处于后台运行的时候尝试延迟 100 ms 再触发 onPackageChanged (猜测是引起 ANR 的原因)
                    AppExecutor.getUiExecutor().executeWithDelay(() -> {
                        PackageObserver.onPackageChanged(installEb);
                        EventBus.getDefault().post(installEb);
                    }, 100);
                }
                if (webviewPackageName.equals(packageName)) {
                    HaloApp.getInstance().getWebviewAbiList();
                }
            }

            // 接收卸载广播
            if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
                String packageName = intent.getDataString();
                packageName = packageName.substring(packageName.indexOf(":") + 1);
                Utils.log("卸载了:" + packageName + "包名的程序");
                InstallUtils.getInstance().removeUninstall(packageName);
                PackageHelper.refreshLocalPackageList();

                EBPackage uninstallEb = new EBPackage(EBPackage.TYPE_UNINSTALLED, packageName, "");
                PackageObserver.onPackageChanged(uninstallEb);
                EventBus.getDefault().post(uninstallEb);
                if (webviewPackageName.equals(packageName)) {
                    SPUtils.remove(Constants.SP_WEBVIEW_ABI_LIST);
                    SPUtils.remove(Constants.SP_WEBVIEW_VERSION_CODE);
                }
            }

            // 接收替换广播
            if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
                String packageName = intent.getData().getSchemeSpecificPart();
                packageName = packageName.substring(packageName.indexOf(":") + 1);
                Utils.log("替换了:" + packageName + "包名的程序");
                String versionName = PackageUtils.getVersionNameByPackageName(packageName);

                EBPackage updateEb = new EBPackage(EBPackage.TYPE_REPLACED, packageName, versionName);
                EventBus.getDefault().post(updateEb);
                PackageObserver.onPackageChanged(updateEb);
                if (webviewPackageName.equals(packageName)) {
                    HaloApp.getInstance().getWebviewAbiList();
                }
            }
        });
    }

}
