//
//  AutoConnectBleController.m
//  iOSPrinterDemo
//
//  Created by ios on 2018/4/13.
//  Copyright © 2018年 Mellow. All rights reserved.
//

#import "AutoConnectBleController.h"
#import <PrinterSDK/PrinterSDK.h>
#import "PTScaleImage.h"
#import "ProgressHUD.h"

@interface AutoConnectBleController ()<UITextFieldDelegate>

@property (nonatomic, strong) NSArray *printers;

@property (nonatomic, strong) NSString *printName;

@end

@implementation AutoConnectBleController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.printName = @"HM-T220";
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.printName = textField.text;
}

- (IBAction)autoConnectBLE:(id)sender {
    
    [self PQ_printer:self.printName printImage:[UIImage imageNamed:@"test.png"]];
    
}

- (void)PQ_printer:(NSString *)printerName printImage:(UIImage *)image {
    
    UIImage *logoImage = [PTScaleImage scaleSourceImage:image maxWidth:180.0f];
    
    //开始扫描蓝牙
    [[PTDispatcher share] scanBluetooth];
    // 获取已发现的所有打印机，每新发现新的打印机或隔三秒调用一次
    [[PTDispatcher share] whenFindBluetoothAll:^(NSDictionary<NSString *,PTPrinter *> *printerDic) {
        self.printers = [NSArray arrayWithArray:printerDic.allValues];
        for (PTPrinter *printer in self.printers) {
            
            if ([printer.name isEqualToString:printerName]) {
                [ProgressHUD show:@"Printing"];
                //关闭蓝牙搜索
                [[PTDispatcher share] stopScanBluetooth];
                //连接打印机
                [[PTDispatcher share] connectPrinter:printer];
            }
        }
    }];
    
    //连接蓝牙成功后做的事情
    [[PTDispatcher share] whenConnectSuccess:^{
        PTCommandESC *cmd = [[PTCommandESC alloc] init];
        //初始化打印机
        [cmd initializePrinter];
        //设置对齐方式
        [cmd setJustification:1];
        [cmd appendRasterImage:logoImage.CGImage mode:PTBitmapModeBinary compress:PTBitmapCompressModeNone];
        [cmd printAndReturnStandardMode];
        //发送data
        [[PTDispatcher share] sendData:[cmd getCommandData]];
    }];
    
    //数据发送成功
    [[PTDispatcher share] whenSendSuccess:^(NSNumber *number) {
        NSLog(@"send data success == %@", number);
        [[PTDispatcher share] unconnectPrinter:[[PTDispatcher share] printerConnected]];
        
    }];
    
    //数据发送失败
    [[PTDispatcher share] whenSendFailure:^{
        NSLog(@"send data fail");
        [ProgressHUD dismiss];
    }];
    
    //连接失败的回调
    [[PTDispatcher share] whenConnectFailureWithErrorBlock:^(PTBleConnectError error) {
        [ProgressHUD showError:[NSString stringWithFormat:@"connectFail--%li",(long)error]];
    }];
    
    //这边断开蓝牙后的回调
    [[PTDispatcher share] whenUnconnect:^(NSNumber *number) {
        [ProgressHUD dismiss];
        [PTDispatcher share].sendSuccessBlock = nil;
        [PTDispatcher share].sendFailureBlock = nil;
        [PTDispatcher share].sendProgressBlock = nil;
        [PTDispatcher share].connectFailBlock = nil;
        [PTDispatcher share].connectSuccessBlock = nil;
        [PTDispatcher share].readRSSIBlock = nil;
    }];
    
    //打印进度
    [[PTDispatcher share] whenSendProgressUpdate:^(NSNumber *number) {
        NSLog(@"send progress:%@", number);
    }];
    
    
}

@end
