package com.hh.arome.broadcastreceiver;

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

import com.hh.arome.activity.MainActivity;
import com.hh.lib_base.Constants;
import com.hh.lib_base.log.AppLogUtils;
import com.hh.lib_base.log.LogType;
import com.hh.lib_base.manager.ActivityManager;
import com.hh.lib_base.utils.device.DeviceUtils;
import com.hh.lib_common.local.AppLocalConfigHandler;
import com.hh.lib_common.local.IAppLocalConfig;


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) {

        // 不存在，则延迟启动
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    Intent mainIntent = new Intent(context, MainActivity.class);
                    /**
                     * Intent.FLAG_ACTIVITY_NEW_TASK
                     * Intent.FLAG_ACTIVITY_CLEAR_TOP
                     */
                    mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    context.startActivity(mainIntent);
                    String logLine= (new Throwable().getStackTrace())[0].toString();
                    // 开始保存本地日志
                    AppLogUtils.saveLog(
                            LogType.Logcat, new  StringBuilder()
                                    .append(Constants.LogcatValue.LINE)
                                    .append(logLine).append(Constants.LogcatValue.WARP)
                                    .append(Constants.LogcatValue.NODE)
                                    .append("HHArome  开机广播  启动MainActivity ")
                                    .toString());
                }catch (Exception e){

                }

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


    private boolean isMainPageExistence(Context context){
        String deviceModel = DeviceUtils.getDeviceModel();
        if (!Constants.DeviceModel.T1YB01.contains(deviceModel)
                && !Constants.DeviceModel.T1YB01A.contains(deviceModel)) {
            Intent intent = new Intent();
            intent.setClassName("com.hh.arome", MainActivity.class.getName());
            if (context != null &&
                    context.getPackageManager() != null &&
                    context.getPackageManager().resolveActivity(intent, 0) != null) {
                AppLogUtils.e("MainActivity存在,return");
                return true;
            }
        }
        return false;
    }

    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--非启动");
            }
            String logLine= (new Throwable().getStackTrace())[0].toString();
            // 开始保存本地日志
            AppLogUtils.saveLog(
                    LogType.Logcat, new  StringBuilder()
                            .append(Constants.LogcatValue.LINE)
                            .append(logLine).append(Constants.LogcatValue.WARP)
                            .append(Constants.LogcatValue.NODE)
                            .append("HHArome  开机广播  autoApp "+(autoApp?"--自启动":"--非启动"))
                            .toString());
        }
    }

}