package io.kiotplugins.kioskplugin;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ComponentName;
import android.content.Intent;
import android.app.admin.DeviceAdminReceiver;
import android.util.Log;

public class KioskDeviceAdminReceiver extends DeviceAdminReceiver {
  private static final String TAG = "KioskMode:KioskDeviceAdminReceiver";

  public static ComponentName getComponentName(Context context) {
    return new ComponentName(context.getApplicationContext(), KioskDeviceAdminReceiver.class);
  }

  @Override
  public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
      Log.d(TAG, "BOOT_COMPLETED received: launching app in kiosk mode.");
      Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
      if (launchIntent != null) {
        launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(launchIntent);
      } else {
        Log.e(TAG, "Launch intent not found for package: " + context.getPackageName());
      }

      // 👉 Start the watchdog service
      try {
        Intent svc = new Intent(context, WatchdogService.class);
        context.startForegroundService(svc); // API 26+; use startService for lower API
        Log.d(TAG, "Kiosk WatchdogService started");
      } catch (Throwable t) {
        Log.e(TAG, "Could not start WatchdogService: " + t.getMessage());
      }
    }

  }

}
