package com.gh.gamecenter.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.gh.gamecenter.core.utils.ClassUtils;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.SplashScreenActivity;
import com.halo.assistant.HaloApp;

/**
 * Created by khy on 2016/9/1.
 * 光环插件跳转助手
 */
public class ActivitySkipReceiver extends BroadcastReceiver {

    public static final String ACTION_ACTIVITY_SKIP = "com.gh.gamecenter.ACTIVITYSKIP";

    @Override
    public void onReceive(Context context, Intent intent) {
        ExtensionsKt.doOnMainProcessOnly(context, () -> {
            if (ACTION_ACTIVITY_SKIP.equals(intent.getAction())) {
                Bundle bundle = intent.getExtras();
                if (HaloApp.getInstance().isRunningForeground && bundle != null) {
                    // 应用正在运行，前台或后台
                    String to = bundle.getString("to");
                    if (!TextUtils.isEmpty(to)) {
                        Class<?> clazz = ClassUtils.forName(to);
                        if (clazz != null) {
                            Intent intent1 = new Intent(context, clazz);
                            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent1.putExtras(bundle);
                            context.startActivity(intent1);
                        }
                    }
                } else {
                    // 应用未在运行
                    context.startActivity(SplashScreenActivity.getSplashScreenIntent(context, bundle));
                }
            }
        });
    }

}
