/*
 
 Owner: Stefan Neumärker <stefan@boinx.com>
 Company: Boinx Software Ltd., Germany
 
 Abstract: Abstract super class for session used in assistants. Holds data over some panes and handles communication with FileMaker Server over lasso files. Override 
 
 Related:	BXAssistantController.h
 
			subclasses:
			BXFeedbackSession.h
			BXSeedAssistantInvitationSession.h
			BXSeedAssistantInviteFriendsSesion.h
			BXSeedAssistantTakepartSession.h
			BXExtendedDemoSession.h
 
 Documentation : Documentation/assistant_session.html
 
 */

#import "BXSubmissionWindowController.h"


// form data constants

// TODO: Move to inline function
#define FORM_RESULT_UNKNOWN_ERROR_TITLE NSLocalizedStringWithDefaultValue(@"FORM_RESULT_UNKNOWN_ERROR_TITLE", @"BXServices", SERVICE_BUNDLE, @"An unknown error occured", @"alert title for unknown error in Boinx assistant form.")
#define FORM_RESULT_UNKNOWN_ERROR_MESSAGE NSLocalizedStringWithDefaultValue(@"FORM_RESULT_UNKNOWN_ERROR_MESSAGE", @"BXServices", SERVICE_BUNDLE, @"Please try again later or contact Boinx Software Support directly if problems persist.", @"alert text for unknown error in Boinx assistant form")


// personal data
extern NSString * const BXFeedbackAssistantFormParameterEmail;
extern NSString * const BXFeedbackAssistantFormParameterFirstName;
extern NSString * const BXFeedbackAssistantFormParameterLastName;
extern NSString * const BXFeedbackAssistantFormParameterCompany;

// user given feedback
extern NSString * const BXFeedbackAssistantFormParameterFeedbackType;
extern NSString * const BXFeedbackAssistantFormParameterAnswer2;
extern NSString * const BXFeedbackAssistantFormParameterAnswer3;
extern NSString * const BXFeedbackAssistantFormParameterAnswer4;
extern NSString * const BXFeedbackAssistantFormParameterAnswer5;
extern NSString * const BXFeedbackAssistantFormParameterAnswer6;
extern NSString * const BXFeedbackAssistantFormParameterAnswer7;
extern NSString * const BXFeedbackAssistantFormParameterAnswer8;
extern NSString * const BXFeedbackAssistantFormParameterAnswer9;
extern NSString * const BXFeedbackAssistantFormParameterAnswer10;

// automatic given feedback
extern NSString * const BXFeedbackAssistantFormParameterLicenseKey;
extern NSString * const BXFeedbackAssistantFormParameterCorrespondenceNumber;
extern NSString * const BXFeedbackAssistantFormParameterZipFilePath;
extern NSString * const BXFeedbackAssistantFormParameterApplication;
// extern NSString * const BXFeedbackAssistantFormParameterVersion;		// this key is not longer used and does not get parsed in lasso
extern NSString * const BXFeedbackAssistantFormParameterSystemProfiler;
extern NSString * const BXFeedbackAssistantFormParameterQuickTime;
extern NSString * const BXFeedbackAssistantFormParameterBXLog;
extern NSString * const BXFeedbackAssistantFormParameterConsoleLog;
extern NSString * const BXFeedbackAssistantFormParameterCrashLog;
extern NSString * const BXFeedbackAssistantFormParameterOSVersion;

extern NSString * const BXFeedbackAssistantFormParameterNewsletter;

extern NSString * const BXFeedbackFormBadDataErrorDomain;
extern NSString * const BXFeedbackFormServerErrorErrorDomain;
extern NSString * const BXFeedbackFormBadResponseErrorDomain;
extern NSString * const BXFeedbackFormNoConnectionErrorDomain;

extern NSString * const BXFeedbackFormResultDatabaseError;
extern NSString * const BXFeedbackFormResultSuccessful;
extern NSString * const BXFeedbackFormResultInvitationCheckSuccessful;
extern NSString * const BXFeedbackFormResultNotInvited;
extern NSString * const BXFeedbackFormResultNotSeeded;
extern NSString * const BXFeedbackFormResultFriendsInvited;
extern NSString * const BXFeedbackFormResultInvitationNotAllowed;
extern NSString * const BXFeedbackFormResultRequestSuccessful;
extern NSString * const BXFeedbackFormResultInvalidProduct;
extern NSString * const BXFeedbackFormResultInvalidCode;
extern NSString * const BXFeedbackFormResultSeedcodeRequestSuccessful;


extern NSString * const BXFeedbackAssistantFormURL;

extern NSString * const BXSessionVisualUpdateNeededNotification;
extern NSString * const BXSessionVisualUpdateFetchingDataKey;
extern NSString * const BXSessionVisualUpdateUploadingDataKey;
extern NSString * const BXSessionVisualUpdateSendingKey;
extern NSString * const BXSessionVisualUpdateSuccessKey;
extern NSString * const BXSessionVisualUpdateErrorKey;


#define MAIL_PREFIX @"mailto:"
#define FEEDBACK_MAIL_FALLBACK @"support@boinx.com"


typedef enum
	{
		BXValidationValid,
		BXValidationEmpty,
		BXVAlidationNotValid,
	} BXSessionDataValidationReturnType;


@interface BXAssistantSession : NSObject
{
	NSMutableDictionary *_sessionData;
	
	NSDictionary *_formDataDict;
	NSString *_formResult;
	
	BOOL _forceFormSend;
	BOOL _finishAfterError;
	BOOL _networkDiagnosisAlreadyRun;
	BOOL _personalAlerts;
}

+ (id)session;

#pragma mark Communication With Server

- (NSURL *)formURL;		// override only if not sending to feedback lasso file

- (BOOL)sendFormWithError:(NSError **)ioError;
- (BOOL)sendFormWithError:(NSError **)ioError forced:(BOOL)inForced;

- (NSDictionary *)generateFormDataDictionary;	// override to handle your data

#pragma mark Preparation of data

- (void)prepareData;
- (void)addBoinxToAddressBook;

#pragma mark Validation of data

- (BOOL)validateFormDataDictionary:(NSDictionary *)inFormDataDict error:(NSError **)ioError;  // override to fit your data

- (int)validateName:(NSString *)inName;
- (int)validateMailAddress:(NSString *)inMailAddress withError:(NSError **)ioError;

- (BOOL)validateFirstName:(NSString *)inFirstName needsIdentification:(BOOL)needsIdentification withError:(NSError **)ioError;
- (BOOL)validateLastName:(NSString *)inLastName needsIdentification:(BOOL)needsIdentification withError:(NSError **)ioError;
- (BOOL)validateEMailAddress:(NSString *)inEMailAddress needsIdentification:(BOOL)needsIdentification withError:(NSError **)ioError;

#pragma mark validation errros

- (NSError *)hostNotReachableErrorForURL:(NSURL *)formURL;
- (NSError *)errorForFeedbackFormResult:(NSString *)inFormResultString;

#pragma mark Notifying UI Changes

- (void)notifyVisualUpdateWithKey:(NSString *)inKey;
- (void)notifyVisualUpdateWithKey:(NSString *)inKey forError:(NSError *)inError;

- (NSString *)progressWindowTitle;
- (NSDictionary *)submissionWindowConfigurationFetchingData;
- (NSDictionary *)submissionWindowConfigurationUploadingData;
- (NSDictionary *)submissionWindowConfigurationSending;
- (NSDictionary *)submissionWindowConfigurationSuccess;
- (NSDictionary *)submissionWindowConfigurationGeneralError;
- (NSDictionary *)submissionWindowConfigurationErrorHandlingWithoutErrorOnlyClose;
- (NSDictionary *)submissionWindowConfigurationErrorHandlingCloseOrTryAgainOrTestConection:(NSError *)inError;
- (NSDictionary *)submissionWindowConfigurationErrorHandlingCloseOrSendViaMail:(NSError *)inError;

#pragma mark Actions

- (void)sendViaMail:(id)inSender;
- (void)checkNetworkConnection:(id)inSender;

#pragma mark Accessors

- (NSMutableDictionary *)sessionData;

- (NSDictionary *)formDataDict;
- (void)setFormDataDict:(NSDictionary *)inFormDataDict;

- (NSString *)formResult;
- (void)setFormResult:(NSString *)inFormResult;

- (BOOL)forceFormSend;
- (void)setForceFormSend:(BOOL)flag;

- (BOOL)finishAfterError;
- (void)setFinishAfterError:(BOOL)flag;

- (BOOL)networkDiagnosisAlreadyRun;
- (void)setNetworkDiagnosisAlreadyRun:(BOOL)flag;

- (BOOL)personalAlerts;
- (void)setPersonalAlerts:(BOOL)flag;

@end


