//
//  PTPrinter.h
//
//  Created by midmirror on 15/12/25.
//  Copyright © 2015年 midmirror. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "SDKDefine.h"
#import "PTPrinter.h"

/** 当前通讯方式 */
typedef NS_ENUM(NSInteger, PTDispatchMode) {
    
    PTDispatchModeUnconnect  = 0,
    PTDispatchModeBLE        = 1,
    PTDispatchModeWiFi       = 2,
};

typedef NS_ENUM(NSInteger, PTPrintState) {
    
    PTPrintStateSuccess             = 0xcc00, // 打印成功
    PTPrintStateFailurePaperEmpty   = 0xcc01, // 打印失败（缺纸）
    PTPrintStateFailureLidOpen      = 0xcc02, // 打印失败（开盖）
};

@class PTPrinter;

/** 发现新蓝牙 */
typedef void(^ScanPrinterBlock)(PTPrinter *printer);
typedef void(^ScanPrintersBlock)(NSDictionary *printerDict);

/** 连接成功、失败 */
typedef void(^ConnectSuccessBlock)();
typedef void(^ConnectFailureBlock)();

/** 可以发送、发送成功、发送失败、接收到返回值 */
typedef void(^SendSuccessBlock)(NSNumber *useTime);
typedef void(^SendFailureBlock)();
typedef void(^SendProgressBlock)(NSNumber *progress);
typedef void(^ReceiveDataBlock)(NSData *data);
typedef void(^RSSIBlock)(NSNumber *RSSI);

/** 打印状态 */
typedef void(^PrintStateBlock)(PTPrintState state);

/** 断开 */
typedef void(^UnconnectBlock)(NSNumber *useTime);

/**
 *  主要功能：实现和打印机的 BLE 和 WiFi通讯
 */
@interface PTDispatcher : NSObject

singletonH(PTDispatcher)

@property(strong,nonatomic,readwrite) PTPrinter *printerConnected;
@property(assign,nonatomic,readwrite) PTDispatchMode mode;

@property(copy,nonatomic,readwrite) SendSuccessBlock     sendSuccessBlock;        //发送结束时的 Block
@property(copy,nonatomic,readwrite) SendFailureBlock     sendFailureBlock;
@property(copy,nonatomic,readwrite) SendProgressBlock    sendProgressBlock;
@property(copy,nonatomic,readwrite) ReceiveDataBlock     receiveDataBlock;
@property(copy,nonatomic,readwrite) PrintStateBlock      printStateBlock;

@property(copy,nonatomic,readwrite) ScanPrinterBlock     findBluetoothBlock;
@property(copy,nonatomic,readwrite) ScanPrintersBlock    findBluetoothAllBlock;
@property(copy,nonatomic,readwrite) ScanPrintersBlock    findWiFiAllBlock;

@property(copy,nonatomic,readwrite) ConnectSuccessBlock  connectSuccessBlock;
@property(copy,nonatomic,readwrite) ConnectFailureBlock  connectFailureBlock;
@property(copy,nonatomic,readwrite) UnconnectBlock       unconnectBlock;
@property(copy,nonatomic,readwrite) RSSIBlock            readRSSIBlock;

/** 发送数据 */
- (void)sendData:(NSData *)data;

#pragma mark PTBluetooth

/** 蓝牙 */
- (void)scanBluetooth;
- (void)stopScanBluetooth;

/** WiFi */

- (void)scanWiFi:(ScanPrintersBlock)wifiAllBlock;

/** 连接和断开 */
- (void)connectPrinter:(PTPrinter *)printer;
- (void)unconnectPrinter:(PTPrinter *)printer;

/* 发现新蓝牙（同一个蓝牙如果状态更新会被重复发现），仅用于快速连接 */
- (void)whenFindBluetooth:(ScanPrinterBlock)bluetoothBlock;
/* 发现所有的蓝牙（每3秒更新一次所有蓝牙状态），用于扫描连接 */
- (void)whenFindBluetoothAll:(ScanPrintersBlock)bluetoothAllBlock;

/** 更新打印机状态（当已连接）*/
- (void)whenReadRSSI:(RSSIBlock)readRSSIBlock;

/** 当连接成功时 */
- (void)whenConnectSuccess:(ConnectSuccessBlock)connectSuccessBlock;

/** 当连接失败时 */
- (void)whenConnectFailure:(ConnectFailureBlock)connectFailureBlock;

/** 当断开连接时 */
- (void)whenUnconnect:(UnconnectBlock)unconnectBlock;

/** 发送数据 */
- (void)whenSendSuccess:(SendSuccessBlock)sendSuccessBlock; // 成功
- (void)whenSendFailure:(SendFailureBlock)sendFailureBlock; // 失败
- (void)whenSendProgressUpdate:(SendProgressBlock)sendProgressBlock;    // 发送进度
- (void)whenReceiveData:(ReceiveDataBlock)receiveDataBlock;
- (void)whenUpdatePrintState:(PrintStateBlock)printStateBlock;

- (NSString *)SDKVersion;
- (NSString *)SDKBuildTime;
- (NSString *)SDKDescription;
@end
