//
//  MVMCoreLoadHandler.h
//  myverizon
//
//  Created by ScottMVMCoreErrorObject.h Pfeil on 11/12/15.
//  Copyright © 2015 Verizon Wireless. All rights reserved.
//
//  Handles loading data

#import <Foundation/Foundation.h>
#import <MVMCore/MVMCoreLoadDelegateProtocol.h>
#import <MVMCore/MVMCorePresentationDelegateProtocol.h>
#import <MVMCore/MVMCoreActionDelegateProtocol.h>
#import <MVMCore/MVMCoreViewControllerProtocol.h>

@class MVMCoreRequestParameters;
@class MVMCoreErrorObject;
@class MVMCoreLoadObject;
@class MVMCoreLoadRequestOperation;
@class DelegateObject;

@interface MVMCoreLoadHandler : NSObject

// Returns the shared instance of this singleton
+ (nullable instancetype)sharedGlobal;

// Adds an additional externally managed queue to the list of all loading queues. This allows for cross queue dependency management.
- (void)registerLoadQueue:(NSOperationQueue *_Nonnull)queue;

// Returns the error location for the given load object. Important for proper logging.
- (nonnull NSString *)errorLocationForRequest:(nonnull MVMCoreLoadObject *)loadObject;

// Returns the error location for the given requesting object and page type and modules. Important for proper logging.
- (nonnull NSString *)errorLocationForRequest:(nonnull id)requestingObject pageType:(nonnull NSString *)pageType modules:(nonnull NSString *)modules;

#pragma mark - Request Functions.

// Creates a request object with the given parameters.
- (nullable NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters error:(MVMCoreErrorObject *_Nonnull *_Nonnull)error;

// Sends a given request to the server. When it is finished, it calls request finished, passing along the json object or nil if there is an error.
- (nullable NSURLSessionTask *)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject * _Nullable error))requestFinished;

#pragma mark - Loading Functions

// Loads a blocking request with the passed in parameters and data for the next page. Pass in the data to handle certain functions.
- (nonnull MVMCoreLoadRequestOperation *)loadRequest:(nonnull MVMCoreRequestParameters *)requestParameters dataForPage:(nullable NSDictionary *)dataForPage delegateObject:(nullable DelegateObject *)delegateObject;

// Loads a background request with the passed in parameters and data for the next page. Pass in the data to handle certain functions.
- (nonnull MVMCoreLoadRequestOperation *)loadBackgroundRequest:(nonnull MVMCoreRequestParameters *)requestParameters dataForPage:(nullable NSDictionary *)dataForPage delegateObject:(nullable DelegateObject *)delegateObject;

// Loads a blocking request with the passed in load object and data for the next page.
- (nonnull MVMCoreLoadRequestOperation *)loadObject:(nonnull MVMCoreLoadObject *)loadObject;

// Cancels all of the current loads.
- (void)cancelAllLoads;

#pragma mark - Standard Delegate Functions

// By default, if error code is not zero, returns not to continue.
+ (BOOL)defaultCheckForSpecificErrors:(nullable MVMCoreErrorObject *)errorObject loadObject:(nonnull MVMCoreLoadObject *)loadObject;

// By default, returns continue loading and decides not to throw an error.
+ (BOOL)defaultHandleModuleError:(nonnull NSString *)module loadObject:(nonnull MVMCoreLoadObject *)loadObject error:(nonnull MVMCoreErrorObject *)error;

#pragma mark - Deprecated

// Loads a blocking request with the passed in parameters and data for the next page. Pass in the data to handle certain functions.
- (nonnull MVMCoreLoadRequestOperation *)loadRequest:(nonnull MVMCoreRequestParameters *)requestParameters dataForPage:(nullable NSDictionary *)dataForPage delegate:(nullable NSObject<MVMCoreLoadDelegateProtocol,MVMCorePresentationDelegateProtocol,MVMCoreActionDelegateProtocol>*)delegate __deprecated;

// Loads a background request with the passed in parameters and data for the next page. Pass in the data to handle certain functions.
- (nonnull MVMCoreLoadRequestOperation *)loadBackgroundRequest:(nonnull MVMCoreRequestParameters *)requestParameters dataForPage:(nullable NSDictionary *)dataForPage delegate:(nullable NSObject<MVMCoreLoadDelegateProtocol,MVMCorePresentationDelegateProtocol,MVMCoreActionDelegateProtocol>*)delegate __deprecated;

@end
