//
//  CSHostBase.h
//  CSNetwork
//
//  Created by Yaroslav Glushchenko on 2/16/12.
//  Copyright (c) 2012 Sladki Programi d.o.o. All rights reserved.
//

@class CSHostBase;
@class CSWebViewController;
@class CSAuthenticationToken;

//////////////////////////////////////////////////////////////////////
// WebHost object delegate
//////////////////////////////////////////////////////////////////////
@protocol CSHostDelegate

@required
- (void)tokenResult:(NSDictionary *)result;
- (void)requestResult:(NSDictionary *)result;

@optional
- (BOOL)needsAuthentication:(NSString *)authenticationURL forPermissions:(NSString *)permissions;
- (void)willShowUINotification:(CSHostBase *)sender;
- (void)didDismissUI:(CSHostBase *)sender;

@end

//////////////////////////////////////////////////////////////////////
// Base web host
//////////////////////////////////////////////////////////////////////
@interface CSHostBase : NSObject
{
@protected
	NSDictionary* appKeys;
    CSAuthenticationToken* authToken;
    NSString* permissions;
}

// Property
@property (assign)		id<CSHostDelegate>		delegate;
@property (copy)		NSDictionary*			appKeys;
@property (readonly)	NSString*				accessToken;

// Some read-only objects
@property (readonly)	CSWebViewController*	webViewController;
@property (readonly)	NSString*				loginSuccessURL;

- (id)initWithApplicationKeys:(NSDictionary *)appKeys delegate:(id)delegate;

// Access token
- (void)getAccessTokenForPermissions:(NSArray *)permissions cached:(BOOL)canCache rootWindow:(NSWindow *)rootWindow;
- (void)setAccessToken:(CSAuthenticationToken *)authenticationToken error:(NSString *)errorReason;
- (void)setAccessToken:(NSString *)accessToken expires:(NSTimeInterval)tokenExpires data:(NSDictionary *)data error:(NSString *)errorReason;
- (void)invalidateCachedToken;

// Requests
- (void)sendRequest:(NSString *)request;
- (void)sendRequest:(NSString *)request params:(NSDictionary *)params usePostRequest:(BOOL)postRequest;
- (void)sendRequest:(NSString *)request params:(NSDictionary *)params userData:(NSString *)user usePostRequest:(BOOL)postRequest;
- (void)postBinaryRequest:(NSString *)request params:(NSDictionary *)params;
- (void)processRequestResult:(NSDictionary *)params;

- (void)appendImageData:(NSData *)data withName:(NSString *)name toBody:(NSMutableData *)body;
- (NSData *)generatePostBody:(NSDictionary *)params;

// UI handlers
- (void)webViewWillShowUI;
- (void)didDismissUI;

@end