package com.metoyou.app;

import android.Manifest;
import android.app.Activity;
import android.app.PictureInPictureParams;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.util.Rational;

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

@CapacitorPlugin(
    name = "MetoyouMobile",
    permissions = {
        @Permission(
            strings = { Manifest.permission.RECORD_AUDIO, Manifest.permission.MODIFY_AUDIO_SETTINGS },
            alias = MetoyouMobilePlugin.MICROPHONE
        ),
        @Permission(strings = { Manifest.permission.CAMERA }, alias = MetoyouMobilePlugin.CAMERA)
    }
)
public class MetoyouMobilePlugin extends Plugin {
    static final String MICROPHONE = "microphone";
    static final String CAMERA = "camera";
    @PluginMethod
    public void requestVoiceCapturePermissions(PluginCall call) {
        if (getPermissionState(MICROPHONE) == PermissionState.GRANTED) {
            resolveCapturePermission(call, MICROPHONE);
            return;
        }

        requestPermissionForAlias(MICROPHONE, call, "voiceCapturePermissionsCallback");
    }

    @PluginMethod
    public void requestCameraCapturePermissions(PluginCall call) {
        if (getPermissionState(CAMERA) == PermissionState.GRANTED) {
            resolveCapturePermission(call, CAMERA);
            return;
        }

        requestPermissionForAlias(CAMERA, call, "cameraCapturePermissionsCallback");
    }

    @PermissionCallback
    private void voiceCapturePermissionsCallback(PluginCall call) {
        resolveCapturePermission(call, MICROPHONE);
    }

    @PermissionCallback
    private void cameraCapturePermissionsCallback(PluginCall call) {
        resolveCapturePermission(call, CAMERA);
    }

    @PluginMethod
    public void setSpeakerphoneEnabled(PluginCall call) {
        Boolean enabled = call.getBoolean("enabled", false);
        AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);

        if (audioManager != null) {
            audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            audioManager.setSpeakerphoneOn(enabled);
        }

        call.resolve();
    }

    @PluginMethod
    public void startVoiceForegroundService(PluginCall call) {
        Context context = getContext();
        Intent intent = new Intent(context, VoiceCallForegroundService.class);
        intent.setAction(VoiceCallForegroundService.ACTION_START);

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

        call.resolve();
    }

    @PluginMethod
    public void stopVoiceForegroundService(PluginCall call) {
        Context context = getContext();
        Intent intent = new Intent(context, VoiceCallForegroundService.class);
        intent.setAction(VoiceCallForegroundService.ACTION_STOP);
        context.startService(intent);
        call.resolve();
    }

    @PluginMethod
    public void enterNativePictureInPicture(PluginCall call) {
        Activity activity = getActivity();
        JSObject result = new JSObject();

        if (activity == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            result.put("supported", false);
            call.resolve(result);
            return;
        }

        PictureInPictureParams params = new PictureInPictureParams.Builder()
            .setAspectRatio(new Rational(16, 9))
            .build();

        boolean entered = activity.enterPictureInPictureMode(params);
        result.put("supported", entered);
        call.resolve(result);
    }

    @PluginMethod
    public void startCallKitSession(PluginCall call) {
        JSObject result = new JSObject();
        result.put("supported", false);
        call.resolve(result);
    }

    @PluginMethod
    public void endCallKitSession(PluginCall call) {
        call.resolve();
    }

    @PluginMethod
    public void exitNativePictureInPicture(PluginCall call) {
        Activity activity = getActivity();

        if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInPictureInPictureMode()) {
            activity.moveTaskToBack(false);
        }

        call.resolve();
    }

    @PluginMethod
    public void isRemotePushConfigured(PluginCall call) {
        JSObject result = new JSObject();
        boolean configured = false;

        try {
            Class<?> firebaseAppClass = Class.forName("com.google.firebase.FirebaseApp");
            Object app = firebaseAppClass.getMethod("getInstance").invoke(null);
            configured = app != null;
        } catch (Exception ignored) {
            configured = false;
        }

        result.put("configured", configured);
        call.resolve(result);
    }

    private void resolveCapturePermission(PluginCall call, String alias) {
        JSObject result = new JSObject();
        result.put(alias, getPermissionState(alias).toString());
        call.resolve(result);
    }
}
