package com.tcl.ah.tvcontrol.broadcast;

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

import com.tcl.ah.tvcontrol.MyApplication;
import com.tcl.ah.tvcontrol.service.MyMqttService;
import com.tcl.ah.tvcontrol.service.ServiceHelper;


public class BootCompletionReceiver extends BroadcastReceiver {
    private final static String TAG = "bootReceiver";
    public static final String ACTION_BOOT_P0 = "android.intent.action.BOOT_P0";

    public static final String HOTEL_INFO_CHANGED = "com.tcl.ah.HOTEL_INFO_CHANGED";
    public static final String HOTEL_URL_CHANGED = "com.tcl.ah.URL_CHANGED";

    public static volatile Boolean IsStartByBoot = false;

    private Context context;
    Intent mMqttIntent;
    private static Boolean isStartService = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        String action = intent.getAction();

//        if ("android.intent.action.BOOT_COMPLETED".equals(action)) {
//            Log.i(TAG, "receive boot completion action:" + action);
//            startForegroundService();
//        }

        if (ACTION_BOOT_P0.equals(action) || "android.intent.action.BOOT_COMPLETED".equals(action)) {
            Log.i(TAG, "receive boot completion action:" + action);
            if (!isStartService) {
                startForegroundService();
                isStartService = true;
            }

        }


    }


    private void startForegroundService() {
        try {
            BootCompletionReceiver.IsStartByBoot =  true;
            mMqttIntent = new Intent(MyApplication.getAppContext(), MyMqttService.class);
            ServiceHelper.startService(MyApplication.getAppContext(), mMqttIntent);
        } catch (Exception e) {
            Log.e(TAG, "start Mqtt fail");
        }
    }


}
