package io.kiotplugins.haptic;

import android.content.Context;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.os.VibratorManager;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

public class Haptic extends CordovaPlugin {

    private Vibrator vibrator;

    @Override
    protected void pluginInitialize() {
        super.pluginInitialize();
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            VibratorManager vibratorManager = (VibratorManager) cordova.getActivity()
                    .getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
            vibrator = vibratorManager.getDefaultVibrator();
        } else {
            vibrator = (Vibrator) cordova.getActivity()
                    .getSystemService(Context.VIBRATOR_SERVICE);
        }
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        try {
            switch (action) {
                case "impact":
                    impact(args.getString(0), callbackContext);
                    return true;
                case "notification":
                    notification(args.getString(0), callbackContext);
                    return true;
                case "selection":
                    selection(callbackContext);
                    return true;
                case "vibrate":
                    vibrate(args.getInt(0), callbackContext);
                    return true;
                default:
                    return false;
            }
        } catch (JSONException e) {
            callbackContext.error("Invalid arguments: " + e.getMessage());
            return false;
        }
    }

    private void impact(String intensity, CallbackContext callbackContext) {
        if (vibrator == null || !vibrator.hasVibrator()) {
            callbackContext.error("Vibrator not available");
            return;
        }

        // Emulate iOS haptic impact patterns with Android vibration
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            VibrationEffect effect;
            
            switch (intensity) {
                case "light":
                    // Light: Quick, gentle tap
                    effect = VibrationEffect.createOneShot(40, 150);
                    break;
                case "heavy":
                    // Heavy: Strong, pronounced feedback
                    effect = VibrationEffect.createOneShot(60, 255);
                    break;
                default: // medium
                    // Medium: Balanced feedback
                    effect = VibrationEffect.createOneShot(40, 150);
                    break;
            }
            
            vibrator.vibrate(effect);
        } else {
            // Fallback for older Android versions
            switch (intensity) {
                case "light":
                    vibrator.vibrate(20);
                    break;
                case "heavy":
                    vibrator.vibrate(60);
                    break;
                default: // medium
                    vibrator.vibrate(40);
                    break;
            }
        }
        
        callbackContext.success();
    }

    private void notification(String type, CallbackContext callbackContext) {
        if (vibrator == null || !vibrator.hasVibrator()) {
            callbackContext.error("Vibrator not available");
            return;
        }

        // Emulate iOS notification patterns with distinctive Android vibration patterns
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            VibrationEffect effect;
            
            switch (type) {
                case "success":
                    // Success: Double tap pattern (positive feeling)
                    effect = VibrationEffect.createWaveform(new long[]{0, 30, 20, 30}, new int[]{0, 120, 0, 120}, -1);
                    break;
                case "warning":
                    // Warning: Triple tap pattern (attention grabbing)
                    effect = VibrationEffect.createWaveform(new long[]{0, 25, 15, 25, 15, 25}, new int[]{0, 150, 0, 150, 0, 150}, -1);
                    break;
                case "error":
                    // Error: Strong-weak-strong pattern (urgent/negative)
                    effect = VibrationEffect.createWaveform(new long[]{0, 50, 30, 20, 30, 50}, new int[]{0, 200, 0, 100, 0, 200}, -1);
                    break;
                default:
                    effect = VibrationEffect.createOneShot(40, VibrationEffect.DEFAULT_AMPLITUDE);
                    break;
            }
            
            vibrator.vibrate(effect);
        } else {
            // Fallback for older Android versions
            switch (type) {
                case "success":
                    vibrator.vibrate(new long[]{0, 30, 20, 30}, -1);
                    break;
                case "warning":
                    vibrator.vibrate(new long[]{0, 25, 15, 25, 15, 25}, -1);
                    break;
                case "error":
                    vibrator.vibrate(new long[]{0, 50, 30, 20, 30, 50}, -1);
                    break;
                default:
                    vibrator.vibrate(40);
                    break;
            }
        }
        
        callbackContext.success();
    }

    private void selection(CallbackContext callbackContext) {
        if (vibrator == null || !vibrator.hasVibrator()) {
            callbackContext.error("Vibrator not available");
            return;
        }

        // Emulate iOS selection feedback: very subtle, quick tap
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            VibrationEffect effect = VibrationEffect.createOneShot(10, 50);
            vibrator.vibrate(effect);
        } else {
            vibrator.vibrate(10);
        }
        
        callbackContext.success();
    }

    private void vibrate(int duration, CallbackContext callbackContext) {
        if (vibrator == null || !vibrator.hasVibrator()) {
            callbackContext.error("Vibrator not available");
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            VibrationEffect effect = VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE);
            vibrator.vibrate(effect);
        } else {
            vibrator.vibrate(duration);
        }
        
        callbackContext.success();
    }
}