//
//  BESandboxFileAuthenticator.h
//  BEToolkit
//
//  Created by Philipp on 6/25/12.
//  Copyright (c) 2012 Philipp Mayerhofer. All rights reserved.
//

#import <Foundation/Foundation.h>

@class BESandboxFileAuthenticator;


@protocol BESandboxFileAuthenticatorDelegate <NSObject>

/**
 * Informs the delegate that the file authenticator will prompt the user
 * whether to authorize the given URL or all URLs.
 *
 * @param anAuthenticator The authenticator that sent the message
 * @param anURL The URL to be authorized
 * @param anAlert The alert dialog that will be shown to the user
 *
 * The delegate can customize the alert to the application's needs. Most
 * application's will want to (indeed should) customize the alert's message
 * text.
 */
-(void) fileAuthenticator:(BESandboxFileAuthenticator *)anAuthenticator willPromptWhetherToAuthorizeAllOrURL:(NSURL *)anURL alert:(NSAlert *)anAlert;

/**
 * Informs the delegate that the file authenticator will prompt the user
 * whether to authorize all files, or whether to authorize later.
 *
 * @param anAuthenticator The authenticator that sent the message
 * @param anAlert The alert dialog that will be shown to the user
 *
 * The delegate can customize the alert to the application's needs. Most
 * application's will want to (indeed should) customize the alert's message
 * text.
 */
-(void) fileAuthenticator:(BESandboxFileAuthenticator *)anAuthenticator willPromptWhetherToAuthorizeAllWithAlert:(NSAlert *)anAlert;

/**
 * Informs the delegate that the file authenticator will request authorization
 * of the given URL.
 *
 * @param anAuthenticator The authenticator that sent the message
 * @param anURL The URL to be authorized
 * @param anOpenPanel The open panel that will be shown to the user
 *
 * The delegate can customize the open panel to the application's needs. For
 * example, by setting a message text or adding an accessory view.
 */
-(void) fileAuthenticator:(BESandboxFileAuthenticator *)anAuthenticator willRequestAuthorizationOfURL:(NSURL *)anURL openPanel:(NSOpenPanel *)anOpenPanel;

@end


@interface BESandboxFileAuthenticator : NSObject <NSOpenSavePanelDelegate> {
    NSMutableArray *authorizedURLs;
    NSURL *URLToAuthorize;
    NSWindow *mainWindow;
    void (^ authorizedHandler)(BOOL success, NSArray *authenticatedURLs);
    id<BESandboxFileAuthenticatorDelegate> delegate;
    NSMutableArray *currentAuthorizedURLs;
}

#pragma mark Authorizing files
// TODO: remove unused message parameter
-(void) authorizeItemsAtURLs:(NSArray *)urls message:(NSString *)message window:(NSWindow *)window completionHandlerWithSingleURL:(void (^)(BOOL success, NSURL *authorizedURL))handler;
-(void) authorizeItemsAtURLs:(NSArray *)urls message:(NSString *)message window:(NSWindow *)window completionHandler:(void (^)(BOOL success, NSArray *authorizedURLs))handler;
-(void) authorizeAll:(NSWindow *)window;
-(void) startAccessingURL:(NSURL *)url;
-(void) stopAccessingURL:(NSURL *)url;
-(BOOL) hasAuthorizedURLs;
-(NSURL *)authorizedBookmarkURLForURL:(NSURL *)url;
-(void) permanentlyAuthorizeUserSelectedURL:(NSURL *)url;
-(NSURL *) openDialogToAuthorizeURL:(NSURL *)url withTitle:(NSString *) title;

-(void) addAccessoryViewForOpenPanel:(NSOpenPanel *)openPanel withMessage:(NSString *) message;

#pragma mark Getting and setting the delegate
@property (assign) id<BESandboxFileAuthenticatorDelegate> delegate;
@property (retain, atomic) NSMutableArray *currentAuthorizedURLs;
@property (retain, atomic) NSURL *URLToAuthorize;
@property (copy, atomic) void (^ authorizedHandler)(BOOL success, NSArray *authenticatedURLs);
@property (retain, nonatomic) NSWindow *mainWindow;

#pragma mark Querying the sandbox state
-(BOOL) inSandbox;

@end
