// Copyright 2015-present 650 Industries. All rights reserved.

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
 * This is the versioned protocol for EXCachedResource, this also defines some
 * of the types used when interacting with EXCachedResource instances. This is
 * used with the EXCachedResourceManager service to be able to create EXCachedResource
 * instances in versioned code.
 *
 * **Avoid making breaking changes to this and if you do make sure to edit all
 * versions of this file.**
 */

@interface EXLoadingProgress : NSObject

@property (nonatomic, copy) NSString *status;
@property (nonatomic, strong) NSNumber *done;
@property (nonatomic, strong) NSNumber *total;

@end

typedef void (^EXCachedResourceSuccessBlock)(NSData *data);
typedef void (^EXCachedResourceErrorBlock)(NSError *error);
typedef void (^EXCachedResourceProgressBlock)(EXLoadingProgress *progress);

typedef enum EXCachedResourceBehavior {
  // load the resource without using any cache.
  EXCachedResourceNoCache,
  // return immediately with cached data if it exists, then try to download the resource and replace the cache in the background.
  EXCachedResourceUseCacheImmediately,
  // try to download the resource, but fall back to the cached version if the download fails.
  EXCachedResourceFallBackToCache,
  // use a cache if it exists, otherwise fail. (don't download anything)
  EXCachedResourceOnlyCache,
} EXCachedResourceBehavior;

@protocol EXResourceLoader

- (void)loadResourceWithBehavior:(EXCachedResourceBehavior)behavior
                   progressBlock:(__nullable EXCachedResourceProgressBlock)progressBlock
                    successBlock:(EXCachedResourceSuccessBlock)successBlock
                      errorBlock:(EXCachedResourceErrorBlock)errorBlock;

@end

NS_ASSUME_NONNULL_END
