package com.gh.gamecenter.receiver;

import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_TO;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import com.gh.common.util.DownloadNotificationHelper;
import com.gh.common.util.PackageInstaller;
import com.gh.common.util.PackageUtils;
import com.gh.gamecenter.DownloadManagerActivity;
import com.gh.gamecenter.SplashScreenActivity;
import com.gh.gamecenter.common.base.fragment.BaseFragment_ViewPager;
import com.gh.gamecenter.common.constant.Constants;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.common.eventbus.EBMiPush;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.download.DownloadFragment;
import com.lightgame.download.DownloadDao;
import com.gh.gamecenter.core.utils.CurrentActivityHolder;
import com.halo.assistant.HaloApp;
import com.gh.gamecenter.download.DownloadFragment;
import com.lightgame.download.DownloadDao;
import com.lightgame.download.DownloadEntity;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by LGT on 2016/10/10.
 * notification安装跳转
 */
public class InstallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ExtensionsKt.doOnMainProcessOnly(context, () -> {
            String path = intent.getStringExtra(EntranceConsts.KEY_PATH);
            DownloadEntity downloadEntity;
            String downloadUrl = intent.getStringExtra(EntranceConsts.KEY_URL);
            downloadEntity = DownloadDao.getInstance(context).getSnapshot(downloadUrl);
            updateNotification(downloadEntity);

            if (PackageUtils.isCanLaunchSetup(context, path)) {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || PackageUtils.isAppOnForeground(context)) {
                    if (downloadEntity != null) {
                        PackageInstaller.install(context, downloadEntity);
                    } else {
                        PackageInstaller.install(context, false, path);
                    }
                } else {
                    Intent intent2 = new Intent();
                    intent2.setAction(Intent.ACTION_MAIN);
                    intent2.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent2.setComponent(new ComponentName(context, DownloadManagerActivity.class));
                    intent2.putExtra(DownloadFragment.KEY_PATH_OF_PACKAGE_TO_INSTALL, path);
                    intent2.putExtra(EntranceConsts.KEY_ENTRANCE, "(安装跳转)");
                    intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    context.startActivity(intent2);
                }
            } else {
                if (HaloApp.getInstance().isRunningForeground) {
                    if (CurrentActivityHolder.getCurrentActivity() instanceof DownloadManagerActivity) {
                        // 这里是指从后台返回到前台 前两个的是关键
                        Intent intent2 = new Intent();
                        intent2.setAction(Intent.ACTION_MAIN);
                        intent2.addCategory(Intent.CATEGORY_LAUNCHER);
                        intent2.setComponent(new ComponentName(context, DownloadManagerActivity.class));
                        intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        context.startActivity(intent2);

                        EventBus.getDefault().post(new EBMiPush("plugin_install", path));
                    } else {
                        Intent intent2 = new Intent(context, DownloadManagerActivity.class);
                        intent2.putExtra(BaseFragment_ViewPager.ARGS_INDEX, 0);
                        intent2.putExtra(EntranceConsts.KEY_PATH, path);
                        intent2.putExtra(EntranceConsts.KEY_ENTRANCE, "(安装跳转)");
                        intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent2);
                    }
                } else {
                    // 应用未在运行
                    Bundle bundle = new Bundle();
                    bundle.putString(KEY_TO, DownloadManagerActivity.TAG);
                    bundle.putInt(BaseFragment_ViewPager.ARGS_INDEX, 0);
                    bundle.putString(EntranceConsts.KEY_PATH, path);
                    bundle.putString(EntranceConsts.KEY_ENTRANCE, "(安装跳转)");
                    context.startActivity(SplashScreenActivity.getSplashScreenIntent(context, bundle));
                }
            }
        });
    }

    private void updateNotification(DownloadEntity downloadEntity) {
        try {
            if (downloadEntity != null) {
                downloadEntity.getMeta().put(Constants.MARK_ALREADY_TRIGGERED_INSTALLATION, "YES");
                DownloadNotificationHelper.addOrUpdateDownloadNotification(downloadEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
