//
//  DONSMediaClient.h
//  DOCore
//
//  Created by Ben Dolman on 8/12/14.
//
//

#import <Foundation/Foundation.h>
#import <Mantle/Mantle.h>

@class DOWebClient;
@protocol DONSMediaClientDelegate;
@class DONSMediaClientRequest;
@class DONSMediaClientResponse;

/**
 * An abstract class for media transfers. Concrete implementations may be found in
 * DONSMediaUploadClient and DONSMediaDownloadClient.
 */
@interface DONSMediaClient : NSObject

@property (assign, readonly) NSInteger requestCount;
@property (copy, readonly) DOWebClient *webClient;
@property (copy, readonly) NSURLSessionConfiguration *sessionConfiguration;
@property (weak, readonly) id<DONSMediaClientDelegate> delegate;
@property (strong, readonly) NSOperationQueue *delegateQueue;
@property (assign, readonly) BOOL isSuspended;

- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration
                                   webClient:(DOWebClient *)webClient
                                    delegate:(id<DONSMediaClientDelegate>)delegate
                               delegateQueue:(NSOperationQueue *)delegateQueue;

- (void)addRequest:(DONSMediaClientRequest *)request;
- (DONSMediaClientRequest *)requestForHash:(NSString *)hash;
- (void)cancelRequestWithHash:(NSString *)hash;

- (void)invalidateAndCancel;

- (void)suspend;
- (void)resume;

@end


@protocol DONSMediaClientDelegate <NSObject>
@optional

- (void)mediaClient:(DONSMediaClient *)mediaClient
 didCompleteRequest:(DONSMediaClientRequest *)request
       withResponse:(DONSMediaClientResponse *)response;

- (void)mediaClientDidFinishReconnectingToSession:(DONSMediaClient *)mediaClient;
- (void)mediaClientDidFinishEventsForBackgroundURLSession:(DONSMediaClient *)mediaClient;

@end

@interface DONSMediaClientRequest : MTLModel

@property (copy, readonly) NSString *mediaHash;

- (instancetype)initWithHash:(NSString *)hash;

@end

@interface DONSMediaClientResponse : MTLModel

@property (copy, readonly) NSHTTPURLResponse *HTTPResponse;
@property (copy, readonly) NSError *error;

- (instancetype)initWithHTTPResponse:(NSHTTPURLResponse *)HTTPResponse error:(NSError *)error;

@end