package com.hh.arome.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;

import com.common.lib_base.common.local.AppLocalConfigHandler;
import com.hh.arome.common.IAppLocalConfig;
import com.hh.arome.ui.MainActivity;
import com.hh.arome.utlis.log.AppLogUtils;

public class AutoStartBroadReceiver extends BroadcastReceiver {

    IAppLocalConfig localConfig;

    private static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        AppLogUtils.e("onReceive: ");
        AppLogUtils.e("onReceive: " + intent.getAction());

        localConfig = AppLocalConfigHandler.create(context.getApplicationContext(), IAppLocalConfig.class);


        boolean firstStart = localConfig.isFirstStart();
        AppLogUtils.e("firstStart--" + firstStart);
        if (!firstStart) {
            localConfig.setFirstStart(true);

            delayStartActivity(context);

        } else {
            startApp(context,intent);
        }

//        localConfig.setStartActivity(false);


    }

    private void delayStartActivity(Context context) {

        // 会收到多次开机广播

        // 判断MainActivity是否已存在
        Intent intent = new Intent(context, MainActivity.class);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            // Activity存在
            AppLogUtils.e("MainActivity存在,return");
            return;
        } else {
            // Activity不存在
            AppLogUtils.e("MainActivity不存在");
        }
//        PackageManager packageManager = context.getPackageManager();
//        try {
//            packageManager.getActivityInfo(new ComponentName(context, MainActivity.class), PackageManager.GET_META_DATA);
//            // Activity存在
//            AppLogUtils.e("MainActivity存在,return");
//            return;
//        } catch (PackageManager.NameNotFoundException e) {
//            // Activity不存在
//            AppLogUtils.e("MainActivity不存在");
//        }


        // 不存在，则延迟启动
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AppLogUtils.e("MainActivity延迟启动");
                Intent mainIntent = new Intent(context, MainActivity.class);
                /**
                 * Intent.FLAG_ACTIVITY_NEW_TASK
                 * Intent.FLAG_ACTIVITY_CLEAR_TOP
                 */
                mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(mainIntent);

            }
        }, 5 * 1000); //延时10s执行run内代码
    }



    private void startApp(Context context, Intent intent) {
        //开机启动
        if (ACTION.equals(intent.getAction())) {
            AppLogUtils.e("startApp--onReceive: 启动了。。。");

            boolean autoApp = localConfig.isAutoApp();
            AppLogUtils.e("autoApp--" + autoApp);
            if (autoApp) {
                AppLogUtils.e("autoApp--自启动");
                //第二种方式：指定class类，跳转到相应的Acitivity

                delayStartActivity(context);


            } else {
                AppLogUtils.e("autoApp--非启动");
            }

        }
    }

}