package com.yourorg.discordclone;

import android.Manifest;
import android.content.Intent;
import android.os.Build;

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

@CapacitorPlugin(
    name = "VoiceService",
    permissions = {
        @Permission(
            strings = { Manifest.permission.POST_NOTIFICATIONS },
            alias = "notifications"
        )
    }
)
public class VoiceServicePlugin extends Plugin {

    private static VoiceServicePlugin instance;

    @Override
    public void load() {
        instance = this;
    }

    public static VoiceServicePlugin getInstance() {
        return instance;
    }

    @PluginMethod
    public void startService(PluginCall call) {
        // Request POST_NOTIFICATIONS permission on Android 13+
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            requestAllPermissions(call, "handleStartAfterPermission");
            return;
        }
        doStartService(call);
    }

    @com.getcapacitor.annotation.PermissionCallback
    private void handleStartAfterPermission(PluginCall call) {
        doStartService(call);
    }

    private void doStartService(PluginCall call) {
        String channelName = call.getString("channelName", "Voice");
        boolean isMuted = call.getBoolean("isMuted", false);
        boolean isDeafened = call.getBoolean("isDeafened", false);

        Intent intent = new Intent(getContext(), VoiceConnectionService.class);
        intent.setAction(VoiceConnectionService.ACTION_START);
        intent.putExtra(VoiceConnectionService.EXTRA_CHANNEL_NAME, channelName);
        intent.putExtra(VoiceConnectionService.EXTRA_IS_MUTED, isMuted);
        intent.putExtra(VoiceConnectionService.EXTRA_IS_DEAFENED, isDeafened);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getContext().startForegroundService(intent);
        } else {
            getContext().startService(intent);
        }

        call.resolve();
    }

    @PluginMethod
    public void stopService(PluginCall call) {
        Intent intent = new Intent(getContext(), VoiceConnectionService.class);
        intent.setAction(VoiceConnectionService.ACTION_STOP);
        getContext().startService(intent);
        call.resolve();
    }

    @PluginMethod
    public void updateNotification(PluginCall call) {
        Intent intent = new Intent(getContext(), VoiceConnectionService.class);
        intent.setAction(VoiceConnectionService.ACTION_UPDATE);

        String channelName = call.getString("channelName");
        if (channelName != null) {
            intent.putExtra(VoiceConnectionService.EXTRA_CHANNEL_NAME, channelName);
        }
        if (call.hasOption("isMuted")) {
            intent.putExtra(VoiceConnectionService.EXTRA_IS_MUTED, call.getBoolean("isMuted", false));
        }
        if (call.hasOption("isDeafened")) {
            intent.putExtra(VoiceConnectionService.EXTRA_IS_DEAFENED, call.getBoolean("isDeafened", false));
        }

        getContext().startService(intent);
        call.resolve();
    }

    public void fireNotificationAction(String action) {
        JSObject data = new JSObject();
        data.put("action", action);
        notifyListeners("voiceNotificationAction", data);
    }
}
