/**
 * VCDataReceiver.h: Vacuum
 * Created by Jiří Urbášek on 9/29/12.
 *
 * Copyright (c) 2012, Arat. All rights reserved.
 *
 * This source code can be used only for purposes specified
 * by the given license contract signed by the rightful deputy of Arat.
 * This source code can be used only by the owner of the license.
 *
 * Any disputes arising in respect of this agreement (license) shall be brought
 * before the Municipal Court of Prague.
 */

#import <Foundation/Foundation.h>
#import "VCTransferableItem.h"
#import "VCService.h"


@protocol VCDataReceiverDelegate;
@class VCDevicePermission;

@interface VCDataReceiver : NSObject

- (instancetype) initWithPort:(int)port;
@property (nonatomic, assign, readonly) int port;

@property (nonatomic, weak) id<VCDataReceiverDelegate> delegate;
@property (nonatomic, strong) NSString *downloadDirectoryPath; // Path to directory in which received files will be stored, default is Documents dir

/** Start listening for transfers */
@property (nonatomic, readonly) BOOL becomeAvailableForReceiving;
@property (nonatomic, assign, readonly) BOOL isAvailableForReceiving;

@property (nonatomic, assign, readonly) BOOL isReceivingData;

/** Stop all transfers and stop listening for new transfers */
- (void)stop;

/**
 * Allows/Denies to begin receiving files being sent to receiver.
 * This method should be issued only when delegate method receiver:allowTransferOfFilesAutomaticaly: was called and returned YES/NO.
 * In every other situation the method does nothing.
 */
- (void)allowFileTransfer;
- (void)denyFileTransfer;

/** Tells that receiwer is waiting for 'allowFileTransfer' or 'denyFileTransfer' messages to be send to decide next step of file transfer */
@property (nonatomic, assign, readonly) BOOL isWaitingForTransferApprovalOrDenial;

/**
 * Allows/Denies to send notification token
 */
- (void)allowSentNotificationToken;
- (void)denySentNotificationToken;

/** Tells that receiwer is waiting for 'allowNoticationToken' or 'denyNotificationToken' messages to be send to decide next step of transfer */
@property (nonatomic, assign, readonly) BOOL isWaitingForNotificationTokenApprovalOrDenial;

@end


@protocol VCDataReceiverDelegate <NSObject>

@required
/**
 * Sent when receiver finds out which files are being sent by other side.
 * Return YES to begin downloading files automaticaly. If you return NO receiver will waing until 'allowFileTransfer' or 'denyFileTransfer'
 * messages are sent. You can use this method to notify user that there some incoming files and as for permission.
 * You can use 'files' array to learn which files are about to be transfered. They are objects conforming to VCTransferableFile protocol and
 * should have prefiled basic information such as fileName or fileSize but not the content.
 * When not implemented, YES is provided as a return value.
 */
- (BOOL)receiver:(VCDataReceiver *)receiver allowTransferOfFilesAutomaticaly:(NSArray<id<VCTransferableItem>> *)files devicePermission:(VCDevicePermission *)devicePermision;

- (void)receiver:(VCDataReceiver *)receiver didReceiveAllFiles:(NSArray<id<VCTransferableItem>> *)files;
- (void)receiver:(VCDataReceiver *)receiver didFailWithError:(NSError *)error;

@optional
- (void)receiverHasIncomingFileTransfer:(VCDataReceiver *)receiver;

- (BOOL)receiver:(VCDataReceiver *)receiver allowNotificationTokenAutomaticalyDevicePermission:(VCDevicePermission *)devicePermision;

- (void)receiver:(VCDataReceiver *)receiver willReceiveFiles:(NSArray<id<VCTransferableItem>> *)files;
- (void)receiver:(VCDataReceiver *)receiver didStartReceivingFile:(id<VCTransferableItem>)file;
- (void)receiver:(VCDataReceiver *)receiver didReceivePartOfFile:(id<VCTransferableItem>)file currentLength:(unsigned long long)currentLength;
- (void)receiver:(VCDataReceiver *)receiver didFinishReceivingFile:(id<VCTransferableItem>)file;

@end


#define kVCErrorCodeCouldNotWriteFile       416
#define kVCErrorCodeCouldChangedDestation   417
#define kVCErrorCodeUpgradeRequired         426
#define kVCErrorCodeToManyRequests          429
