//
//  WS1Intelligence.h
//  VMWare
//
//  Created by Kieran Crawford on 8/20/19.
//  Copyright © 2020 VMWare. All rights reserved.
//

#ifndef WS1Intelligence_h
#define WS1Intelligence_h

#import "WS1Filter.h"
#import "WS1Config.h"
#import "WS1Constants.h"

NS_ASSUME_NONNULL_BEGIN

@interface WS1Intelligence : NSObject

/*!
 * Enabling WS1Intelligence, this method must be called before any other WS1Intelligence functionality.
 * This method will use the app ID string value specified in the applications info.plist file with the
 * key 'WS1IntelligenceAppID'
 */
+ (void)enable;

/*!
 * Enabling WS1Intelligence, this method must be called before any other WS1Intelligence functionality.
 * This method will use the app ID string value specified in the applications info.plist file with the
 * key 'WS1IntelligenceAppID'
 * @param config WS1Config your custom WS1Config
 */
+ (void)enableWithConfig:(nullable WS1Config *)config;

/*!
 * Enabling WS1Intelligence, this method must be called before any other WS1Intelligence functionality
 * @param appId NSString is your iOS WS1Intelligence appId
 */
+ (void)enableWithAppID:(NSString *)appId;

/*!
 * Initializes WS1Intelligence with the given App ID (found on the WS1Intelligence web portal)
 * After this call completes, changes to the config object will have no affect on
 * the behavior of WS1Intelligence.
 * @param appId NSString your iOS appId
 * @param config WS1Config your custom WS1Config
 */
+ (void)enableWithAppID:(NSString *)appId config:(nullable WS1Config *)config;

/*!
 * Adds an additional filter for network instrumentation.
 * @see  WS1Filter header for additional details.
 * @param filter WS1Filter filter to add
 */
+ (void)addFilter:(WS1Filter *)filter;

/*!
 * Breadcrumbs provide the ability to track activity within your app.
 * A breadcrumb is a free form string you supply, which will be timestamped,
 * and stored in case a crash occurs. Crash reports will contain the breadcrumb
 * trail from the run of your app that crashed, as well as that of the prior run.
 *
 * Breadcrumbs are limited to 140 characters, and only the most recent 100 are
 * kept. WS1Intelligence will automatically insert a breadcrumb marked "session_start"
 * on each initial launch, or foreground of your app.
 * @param breadcrumb NSString, the custom string to leave a breadcrumb max 140 characters
 */
+ (void)leaveBreadcrumb:(NSString *)breadcrumb;

/*!
 * By default, breadcrumbs are flushed to disk immediately when written.
 * This is by design - in order to provide an accurate record of everything
 * that happened up until the point your app crashed. To improve performance you can
 * instruct the library to perform all breadcrumb writes on a background thread.
 * @param writeAsync BOOL, YES to write breadcrumbs in a background thread
 */
+ (void)setAsyncBreadcrumbMode:(BOOL)writeAsync;

/*!
 * Inform WS1Intelligence of the device's most recent location for use with
 * performance monitoring.
 * @param latitude double latitude value
 * @param longitude double longitude value
 */
+ (void)updateLocationToLatitude:(double)latitude longitude:(double)longitude;

/*!
 * Handled exceptions are a way of reporting exceptions your app intentionally
 * caught. If the passed in NSException object was thrown, the stack trace
 * of the thread that threw the exception will be displayed on the WS1Intelligence
 * web portal.
 *
 * Reporting of handled exceptions is throttled to once per minute. During
 * that minute period, up to 5 handled exceptions will be buffered.
 *
 * @param exception NSException exception to log
 */
+ (BOOL)logHandledException:(NSException *)exception;

/*!
 * Logging errors is a way of reporting errors your app has received.  If
 * the method is passed an NSError *error, the stack trace of the thread that
 * is logging the error will be displayed on the WS1Intelligence web portal.
 * @param error NSError error to log
 */
+ (BOOL)logError:(NSError *)error;

/*!
 * Logging errors is a way of reporting errors your app has received. This
 * method is the same as above, but allows a custom stack trace to be added
 * instead of collecting it automatically.
 * @param error NSError error to log
 * @param stacktrace NSArray an array of strings representing a stack trace
 */
+ (BOOL)logError:(NSError *)error
      stacktrace:(NSArray <NSString *> *)stacktrace;

/*!
 * Logging endpoints is a way of manually logging custom network library
 * network access to URL's which fall outside WS1Intelligence monitoring
 * of NSURLConnection and ASIHTTPRequest and NSURLSession method calls.
 * @param method NSString the connection method ex GET, POST
 * @param urlString NSString the url
 * @param latency NSTimeInterval, the time between data being sent and received
 * @param bytesRead NSUInteger the amout of data downloaded
 * @param bytesSent NSUInteger the amout of data uploaded
 * @param responseCode NSUInteger the response from the server
 * @param error NSError any error accociated with the network reqest
 * @return YES if the request was properly logged
 */
+ (BOOL)logNetworkRequest:(NSString *)method
                urlString:(NSString *)urlString
                  latency:(NSTimeInterval)latency
                bytesRead:(NSUInteger)bytesRead
                bytesSent:(NSUInteger)bytesSent
             responseCode:(NSInteger)responseCode
                    error:(nullable NSError *)error;

/*!
 * Logging endpoints is a way of manually logging custom network library
 * network access to URL's which fall outside WS1Intelligence's monitoring
 * of NSURLSession method calls.
 * @see + (BOOL)logNetworkRequest:(NSString *)method
 * urlString:(NSString *)urlString
 * latency:(NSTimeInterval)latency
 * bytesRead:(NSUInteger)bytesRead
 * bytesSent:(NSUInteger)bytesSent
 * responseCode:(NSInteger)responseCode
 * error:(NSError *)error;
 */
+ (BOOL)logNetworkRequest:(NSString *)method
                      url:(NSURL *)url
                  latency:(NSTimeInterval)latency
                bytesRead:(NSUInteger)bytesRead
                bytesSent:(NSUInteger)bytesSent
             responseCode:(NSInteger)responseCode
                    error:(nullable NSError *)error;

/*! If you wish to offer your users the ability to opt out of WS1Intelligence
 * crash reporting, you can set the OptOutStatus to YES. If you do so, any
 * pending crash reports will be purged.
 * @param status BOOL YES to disable WS1Intelligence
 */
+ (void)setOptOutStatus:(BOOL)status;

/*!
 * Retrieve current opt out status.
 */
+ (BOOL)getOptOutStatus;

/*!
 * Get the WS1Intelligence generated unique identifier for this device.
 * !! This is NOT the device's UDID.
 *
 * If called before enabling the library, this will return an empty string.
 *
 * All WS1Intelligence enabled apps on a device will share the UUID created by the
 * first installed WS1Intelligence enabled app.
 *
 * If all WS1Intelligence enabled applications are removed from a device, a new
 * UUID will be generated when the next one is installed.
 */
+ (NSString *)getUserUUID;

/*!
 * Associate a username string with the device's WS1Intelligence UUID. This will
 * send the association to WS1Intelligence's back end.
 * @param username NSString the new UUID
 */
+ (void)setUsername:(NSString *)username;

/*!
 *@return YES if the app crashed on the last load
 */
+ (BOOL)didCrashOnLastLoad;

/*!
 * Init and begin a user flow
 *@param name NSString the name of the user flow
 */
+ (void)beginUserFlow:(NSString *)name;

/*!
 * Init and begin a user flow with a specified timeout value
 *@param name NSString the name of the user flow
 *@param timeout NSTimeInterval timeout for this user flow in seconds
 */
+ (void)beginUserFlow:(NSString *)name timeout:(NSTimeInterval)timeout;

/*!
 * Cancel a user flow as if it never existed. The user flow will not be reported
 * @param name NSString the name of the user flow
 */
+ (void)cancelUserFlow:(NSString *)name;

/*!
 * End an already begun user flow successfully.
 * @param name NSString the name of the user flow
 */
+ (void)endUserFlow:(NSString *)name;

/*!
 * End an already begun user flow as a failure.
 * @param name NSString the name of the user flow
 */
+ (void)failUserFlow:(NSString *)name;

/*!
 * Set the logging level to tune the verbosity of WS1Intelligence log messages
 * @param loggingLevel WS1IntelligenceLoggingLevel the verbosity of logging
 */
+ (void)setLoggingLevel:(WS1IntelligenceLoggingLevel)loggingLevel;

/*!
 * The current level of logging
 *@return the current logging level
 */
+ (WS1IntelligenceLoggingLevel)loggingLevel;

/*!
 * Tell WS1Intelligence that your app is about to have abort() called
 */
+ (void)logAbort;

/*!
 * Tell WS1Intelligence that your app is about to have exit() called
 */
+ (void)logExit;

@end

NS_ASSUME_NONNULL_END

#endif /* WS1Intelligence_h */
