package org.timesafari.dailynotification;

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

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

/**
 * TestEventsPlugin - Receives test events from shell scripts via ADB broadcasts
 * 
 * This plugin allows test scripts (test-phase*.sh) to send structured events
 * to the test console UI in real-time via Android broadcast intents.
 * 
 * Usage from shell:
 *   adb shell am broadcast \
 *     -a org.timesafari.dailynotification.TEST_EVENT \
 *     --es payload '{"version":"testevent.v1","ts":"...","type":"step_start",...}'
 */
@CapacitorPlugin(name = "TestEvents")
public class TestEventsPlugin extends Plugin {

    private static final String TAG = "TestEventsPlugin";
    private static final String BROADCAST_ACTION = "org.timesafari.dailynotification.TEST_EVENT";
    private static final String EXTRA_PAYLOAD = "payload";

    private BroadcastReceiver testEventReceiver;

    @Override
    public void load() {
        super.load();

        // Register broadcast receiver
        testEventReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (BROADCAST_ACTION.equals(intent.getAction())) {
                    String payload = intent.getStringExtra(EXTRA_PAYLOAD);
                    if (payload != null) {
                        Log.d(TAG, "Received test event: " + payload);
                        notifyListeners("testEvent", new JSObject().put("payload", payload));
                    }
                }
            }
        };

        IntentFilter filter = new IntentFilter(BROADCAST_ACTION);
        getContext().registerReceiver(testEventReceiver, filter);

        Log.d(TAG, "TestEventsPlugin loaded, broadcast receiver registered");
    }

    @Override
    public void handleOnDestroy() {
        super.handleOnDestroy();

        // Unregister receiver
        if (testEventReceiver != null) {
            try {
                getContext().unregisterReceiver(testEventReceiver);
                Log.d(TAG, "TestEventsPlugin unregistered");
            } catch (IllegalArgumentException e) {
                // Receiver was not registered, ignore
                Log.w(TAG, "Receiver was not registered: " + e.getMessage());
            }
        }
    }

    /**
     * Plugin method to manually trigger an event (for testing)
     */
    @PluginMethod
    public void emitEvent(PluginCall call) {
        String payload = call.getString("payload");
        if (payload == null) {
            call.reject("Missing payload parameter");
            return;
        }

        Log.d(TAG, "Manually emitting test event: " + payload);
        notifyListeners("testEvent", new JSObject().put("payload", payload));
        call.resolve();
    }
}

