/***
 * Excerpted from "Developing for Apple Watch",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/jkwatch for more book information.
***/
//
//  AppDelegate.m
//  ActionableNotifications
//
//  Created by Jeff Kelley on 3/30/15.
//  Copyright (c) 2015 Pragmatic Bookshelf. All rights reserved.
//


#import "AppDelegate.h"


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self registerNotificationCategories];
    return YES;
}

- (void)registerNotificationCategories
{
    UIMutableUserNotificationAction *acceptAction =
    [[UIMutableUserNotificationAction alloc] init];
    
    acceptAction.activationMode = UIUserNotificationActivationModeBackground;
    acceptAction.authenticationRequired = YES;
    acceptAction.identifier = @"Accept";
    acceptAction.title = @"Accept";

    UIMutableUserNotificationAction *declineAction =
    [[UIMutableUserNotificationAction alloc] init];
    
    declineAction.activationMode = UIUserNotificationActivationModeBackground;
    declineAction.authenticationRequired = YES;
    declineAction.identifier = @"Decline";
    declineAction.title = @"Decline";
    
    UIMutableUserNotificationAction *maybeAction =
    [[UIMutableUserNotificationAction alloc] init];
    
    maybeAction.activationMode = UIUserNotificationActivationModeBackground;
    maybeAction.authenticationRequired = YES;
    maybeAction.identifier = @"Maybe";
    maybeAction.title = @"Maybe";
    
    UIMutableUserNotificationAction *deleteAction =
    [[UIMutableUserNotificationAction alloc] init];
    
    deleteAction.activationMode = UIUserNotificationActivationModeBackground;
    deleteAction.authenticationRequired = YES;
    deleteAction.destructive = YES;
    deleteAction.identifier = @"Delete";
    deleteAction.title = @"Delete";
    
    UIMutableUserNotificationCategory *category =
    [[UIMutableUserNotificationCategory alloc] init];
    
    category.identifier = @"Invitation";
    
    [category setActions:@[acceptAction, declineAction]
              forContext:UIUserNotificationActionContextMinimal];
    
    [category setActions:@[acceptAction, maybeAction, declineAction, deleteAction]
              forContext:UIUserNotificationActionContextDefault];
    
    NSSet *categories = [NSSet setWithObject:category];
    
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert
                                      categories:categories];
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

- (void)       application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
      forLocalNotification:(UILocalNotification *)notification
         completionHandler:(void (^)())completionHandler
{
    if ([notification.category isEqualToString:@"Invitation"]) {
        if ([identifier isEqualToString:@"Accept"]) {
            // Handle accepted invitation
        }
        else if ([identifier isEqualToString:@"Maybe"]) {
            // Handle tentative response
        }
        else if ([identifier isEqualToString:@"Decline"]) {
            // Handle declined invitation
        }
        else if ([identifier isEqualToString:@"Delete"]) {
            // Delete invitation
        }
    }
    
    completionHandler();
}

@end
