//
//  RequestHandler.h
//  EAMiPad
//
//  Created by temp on 1/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "WebService.h"
#import "WebQueryParameters.h"
#import "GridData.h"
#import "WebQueryResponse.h"

typedef enum {
    ConnectionModeConnected,
    ConnectionModeDisconnected,
    ConnectionModeMixed
} ConnectionMode;


#define REALTIME_WS_TIMEOUT 30.0f

@protocol WebQueryDelegate

- (void)webQuerySuccessWithModels:(NSArray *)models withType:(WebQueryRequestType)type withModel:(NSString *)modelName;
- (void)webQuerySuccessWithGridData:(GridData *)gridData withType:(WebQueryRequestType )type withGrid:(NSString *)gridName;
- (void)webQuerySuccesswithData:(NSArray *)data Type:(WebQueryRequestType)type withEntity:(NSString *)entityName;
- (void)webQuerySuccesswithData:(NSArray *)data fileBytes:(NSData *)fileBytes Type:(WebQueryRequestType)type withEntity:(NSString *)entityName;
- (void)webQuerySuccesswithData:(NSArray *)data file:(NSString *)file Type:(WebQueryRequestType)type withEntity:(NSString *)entityName;
- (void)webQueryFailed:(NSString *)fault withType:(WebQueryRequestType)type withEntity:(NSString *)entityName;
- (void)webQueryFailed:(NSString *)fault andFieldReference:(NSString *)fieldRef withType:(WebQueryRequestType)type withEntity:(NSString *)entityName;

@optional
- (void)webQuerySuccessWithGridData:(GridData *)gridData withType:(WebQueryRequestType )type withGrid:(NSString *)gridName withParameters:(WebQueryParameters *)parameters;
- (void)webQuerySession:(NSString *)sessionID;
- (void)webQueryConfirm:(NSString *)message withType:(WebQueryRequestType)type withEntity:(NSString *)entityName withException:(NSString *)exceptionName;
- (void)webQueryInvalidDevice:(NSString *)fault withDoesNotExist:(BOOL)doesNotExist;
- (void)webQueryFailed:(NSString *)fault withType:(WebQueryRequestType)type withEntity:(NSString *)entityName withSession:(NSString *)session;
- (void)webQueryFailed:(NSString *)fault withType:(WebQueryRequestType)type withEntity:(NSString *)entityName withSession:(NSString *)session withObject:(id)object withParameters:(WebQueryParameters *)parameters;
- (void)webQueryCompleteWithResponse:(WebQueryResponse *)response;

@end

@protocol LocalQueryDelegate

- (id)runRequest:(NSString *)request withParameters:(WebQueryParameters *)parameters withError:(NSError **)error;
- (id)runRequest:(NSString *)request withObject:(id)object withParameters:(WebQueryParameters *)parameters withError:(NSError **)error;

@end

@interface WebQuery : NSObject <WebServiceDelegate> 

@property (nonatomic, weak) id delegate;
@property (nonatomic, strong) NSString *sessionId;
@property (nonatomic) BOOL newSession;
@property (nonatomic) BOOL endSession;
@property (nonatomic) ConnectionMode connectionMode;
@property (nonatomic) BOOL canHaveNullValues;
@property (nonatomic) float webServiceTimeout;

- (void)getModel:(NSString *)modelName withParameters:(WebQueryParameters *)parameters;
- (void)addModel:(id)model;
- (void)updateModel:(id)model;
- (void)removeModel:(id)model;
- (void)rollbackModel:(id)model;
- (void)getGrid:(NSString *)grid 
   withFunction: (NSString *)function 
       withType: (GridQueryType)gridQueryType 
withNumberofRows:(long)rows
withCurrentPosition:(long)currentPosition
 withParameters:(WebQueryParameters *)parameters;
- (void)getGrid:(NSString *)grid 
   withFunction: (NSString *)function 
       withType: (GridQueryType)gridQueryType 
withNumberofRows:(long)rows
withCurrentPosition:(long)currentPosition
 withParameters:(WebQueryParameters *)parameters
  withDataSpyId:(NSString *)dataSpyId;
- (void)getGrid:(NSString *)grid
   withFunction: (NSString *)function
       withType: (GridQueryType)gridQueryType
withNumberofRows:(long)rows
withCurrentPosition:(long)currentPosition
 withParameters:(WebQueryParameters *)parameters
  withDataSpyId:(NSString *)dataSpyId
withDeviceLicense:(BOOL)deviceLicense;
- (void)runRequest:(NSString *)request withParameters:(WebQueryParameters *)parameters;
- (void)runRequest:(NSString *)request withFileBytes:(NSData *)fileBytes withParameters:(WebQueryParameters *)parameters;

@end


