//
//  DONSEntryUploadClient.h
//  DOCore
//
//  Created by Ben Dolman on 9/18/14.
//
//

#import <Mantle/Mantle.h>

@protocol DONSEntryUploadClientDelegate;
@class DOWebClient;
@class DONSEntryUploadRequest;
@class DOWebRevision;

/**
 * Uploads entries to the sync endpoint.
 */
@interface DONSEntryUploadClient : NSObject

@property (assign, readonly) NSInteger uploadCount;
@property (weak, readonly) id<DONSEntryUploadClientDelegate> delegate;
@property (assign, readonly) BOOL isSuspended;

- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration
                            workingDirectory:(NSURL *)directory
                                   webClient:(DOWebClient *)client
                                    delegate:(id<DONSEntryUploadClientDelegate>)delegate
                               delegateQueue:(NSOperationQueue *)delegateQueue;

- (void)processBackgroundEventsWithCompletionHandler:(void (^)(void))completionHandler;

- (void)addRequest:(DONSEntryUploadRequest *)request;
- (DONSEntryUploadRequest *)requestForEntryID:(NSString *)entryID journalID:(NSString *)journalID;
- (void)cancelRequestForEntryID:(NSString *)entryID journalID:(NSString *)journalID;

- (void)invalidateAndCancel;

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

@end


@interface DONSEntryUploadRequest : MTLModel

@property (copy, readonly) NSString *entryID;
@property (copy, readonly) NSString *journalID;
@property (copy, readonly) NSNumber *parentRevisionID;
@property (copy, readonly) NSDictionary *userInfo;

+ (DONSEntryUploadRequest *)uploadRequestForEntryID:(NSString *)entryID
                                          journalID:(NSString *)journalID
                                   proposedRevision:(DOWebRevision *)proposedRevision
                                     parentRevision:(NSNumber *)parentRevisionID
                                           userInfo:(NSDictionary *)userInfo;

@end

@interface DONSEntryUploadResponse : MTLModel

@property (strong, readonly) DOWebRevision *revision;
@property (copy, readonly) NSString *outcome;

@end

@protocol DONSEntryUploadClientDelegate <NSObject>
@optional

- (void)entryUploadClient:(DONSEntryUploadClient *)uploadClient
                 request:(DONSEntryUploadRequest *)request
        didFailWithError:(NSError *)error;

- (void)entryUploadClient:(DONSEntryUploadClient *)uploadClient
                 request:(DONSEntryUploadRequest *)request
  didSucceedWithResponse:(DONSEntryUploadResponse *)response;

- (void)entryUploadClientDidFinishReconnectingToSession:(DONSEntryUploadClient *)uploadClient;

@end
