#import "CDVHaptic.h"
#import <AudioToolbox/AudioToolbox.h>

@implementation CDVHaptic

- (void)impact:(CDVInvokedUrlCommand*)command {
    NSString* intensity = [command.arguments objectAtIndex:0];
    
    if (@available(iOS 10.0, *)) {
        UIImpactFeedbackStyle style;
        
        if ([intensity isEqualToString:@"light"]) {
            style = UIImpactFeedbackStyleLight;
        } else if ([intensity isEqualToString:@"heavy"]) {
            style = UIImpactFeedbackStyleHeavy;
        } else {
            style = UIImpactFeedbackStyleMedium;
        }
        
        UIImpactFeedbackGenerator* generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:style];
        [generator impactOccurred];
        
        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    } else {
        // Fallback for iOS < 10
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    }
}

- (void)notification:(CDVInvokedUrlCommand*)command {
    NSString* type = [command.arguments objectAtIndex:0];
    
    if (@available(iOS 10.0, *)) {
        UINotificationFeedbackType feedbackType;
        
        if ([type isEqualToString:@"success"]) {
            feedbackType = UINotificationFeedbackTypeSuccess;
        } else if ([type isEqualToString:@"warning"]) {
            feedbackType = UINotificationFeedbackTypeWarning;
        } else if ([type isEqualToString:@"error"]) {
            feedbackType = UINotificationFeedbackTypeError;
        } else {
            feedbackType = UINotificationFeedbackTypeSuccess;
        }
        
        UINotificationFeedbackGenerator* generator = [[UINotificationFeedbackGenerator alloc] init];
        [generator notificationOccurred:feedbackType];
        
        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    } else {
        // Fallback for iOS < 10
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    }
}

- (void)selection:(CDVInvokedUrlCommand*)command {
    if (@available(iOS 10.0, *)) {
        UISelectionFeedbackGenerator* generator = [[UISelectionFeedbackGenerator alloc] init];
        [generator selectionChanged];
        
        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    } else {
        // Fallback for iOS < 10
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    }
}

- (void)vibrate:(CDVInvokedUrlCommand*)command {
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

@end