//
//  PTTestPrint.m
//
//  Created by midmirror on 15/12/18.
//  Copyright © 2015年 midmirror. All rights reserved.
//

#import "PTTestESC.h"
#import <PrinterSDK/PrinterSDK.h>
#import "iOSPrinterDemo-Swift.h"

@interface PTTestESC ()

@end

@implementation PTTestESC

- (void)testPrintMaxByte:(int)number    //测试 BLE 最大传输字节数
{
    char data[number];
    for (int i=0; i<number; i++) {
        int remainder = i%26;
        data[i] = (char)('A' + remainder);
    }
    NSString *letterString = [[NSString alloc] initWithBytes:data length:number encoding:NSUTF8StringEncoding];
    
    NSData *textData = [letterString dataUsingEncoding:NSUTF8StringEncoding];
    Byte byte[] = {0x0a};
    NSMutableData *textMutableData = [[NSMutableData alloc] initWithData:textData];
    [textMutableData appendBytes:byte length:1];
    
    NSLog(@"sendData.length:%lu",(unsigned long)textMutableData.length);
    
    [PrinterPort sendWithData:textMutableData];
}

- (NSString *)getCurrentTime:(NSString *)title
{
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSString *currentTime = [dateFormatter stringFromDate:nowDate];
    NSString *timeString = [NSString stringWithFormat:@"%@:%@",title,currentTime];
 
    return timeString;
}

- (void)printText
{
    PTCommandESC *cmd = [[PTCommandESC alloc] init];
    
    [cmd appendText:@"    Xcode(Bold)" mode:ESCTextBold];
    [cmd appendText:@"7.3 beta 3 adds interactive iOS and OS X playgrounds that allow you to click, drag, type, and otherwise interact with the user interfaces you code into your playground. These interfaces react just as they would within a full application. Interactive playgrounds help you to quickly prototype and build your applications, and simply provide another great way to interact with your code.\n\n" mode:ESCTextNormal];
    [cmd appendText:@"    Any view or view controller that is assigned to the" mode:ESCTextNormal];
    [cmd appendText:@" liveView(Underline) " mode:ESCTextUnderline];
    [cmd appendText:@" property of the " mode:ESCTextNormal];
    [cmd appendText:@" XCPlaygroundPage(Double Width) " mode:ESCTextDoubleWidth];
    [cmd appendText:@"is automatically made interactive, and since it runs within a playground you get all the usual playground results. You can experiment with gesture recognizers, see how" mode:ESCTextNormal];
    [cmd appendText:@" UITableView(Double Height) " mode:ESCTextDoubleHeight];
    [cmd appendText:@"creates and dequeues cells as you scroll, or interact with a complex 3D scene in SceneKit.\n" mode:ESCTextNormal];
    
    [PrinterPort sendWithData:[cmd getCommandData]];
}

- (void)printBarcode2
{
    PTCommandESC *cmd = [[PTCommandESC alloc] init];
    
    [cmd appendBarcode:ESCBarcodeB_UPCA data:@"075678164125" justification:0 width:2 height:40 hri:2];
    [cmd appendBarcode:ESCBarcodeB_UPCE data:@"01227000009" justification:0 width:2 height:40 hri:2];
    [cmd appendBarcode:ESCBarcodeB_EAN13 data:@"6901028075831" justification:0 width:2 height:40 hri:2];
    [cmd appendBarcode:ESCBarcodeB_EAN8 data:@"04210009" justification:0 width:2 height:40 hri:2];
    [cmd appendBarcode:ESCBarcodeB_CODE39 data:@"123456789" justification:0 width:2 height:40 hri:2];
    [cmd appendBarcode:ESCBarcodeB_ITF data:@"123456789012" justification:0 width:2 height:40 hri:2];
    [cmd appendBarcode:ESCBarcodeB_CODEBAR data:@"A40156B" justification:0 width:2 height:40 hri:2];
    [cmd appendBarcode:ESCBarcodeB_CODE93 data:@"TEST93" justification:0 width:2 height:40 hri:2];
    [cmd appendBarcode:ESCBarcodeB_CODE128 data:@"{BS/N:{C14427016A" justification:0 width:2 height:40 hri:2];
    
    // 进入页模式，打印两条并排二维码
    [cmd transmitPaperSonsorStatus];
    [cmd initializePrinter];
    [cmd setPageMode];
    [cmd pageSetPrintAreaWithXPos:0 yPos:0 width:300 height:300];
    
    [cmd pageSetPrintDirection:0];
    [cmd setAbsolutePosition:10];
    [cmd pageSetAbsoluteYPos:10];
    [cmd appendQRCodeData:@"hello world" justification:0 leftMargin:10 eccLevel:48 model:49 size:5];
    
    [cmd pageSetPrintDirection:0];
    [cmd setAbsolutePosition:140];
    [cmd pageSetAbsoluteYPos:10];
    [cmd appendQRCodeData:@"hello world" justification:0 leftMargin:100 eccLevel:48 model:49 size:5];
    
    [cmd printAndReturnStandardMode];
    [cmd cancelPrint];
    
    [PrinterPort sendWithData:[cmd getCommandData]];
}

- (void)printBarcode
{
    PTCommandESC *cmd = [[PTCommandESC alloc] init];
    
    [cmd initializePrinter];
    [cmd setJustification:1];
    [cmd setLineSpacing:0];
    
    UIImage *logoImage = [UIImage imageNamed:@"logo2.jpg"];
    [cmd appendRasterImage:logoImage.CGImage mode:PTBitmapModeDithering compress:PTBitmapCompressModeNone];
    
    [PrinterPort sendWithData:[cmd getCommandData]];
}

- (void)printBarcode2D
{
    PTCommandESC *cmd = [[PTCommandESC alloc] init];
    [cmd appendMaxiCodeData:@"Maxicode" mode:50];
    [cmd appendPDF417Data:@"PDF417" row:3 column:15 width:2 rowHeight:6 eccMode:48 eccLevel:48 option:0];
//    [cmd setPageMode];
//    [cmd appendLineWithXPos:20 yPos:20 xEnd:50 yEnd:50 mode:2];
//    [cmd appendRectWithXPos:30 yPos:30 xEnd:60 yEnd:60 mode:2];
//    [cmd setStandardMode];
    
    [PrinterPort sendWithData:[cmd getCommandData]];
}

- (void)printReceipt2
{
    PTCommandESC *cmd = [[PTCommandESC alloc] init];
    
    [cmd appendCommandData:[PTEncode encodeDataWithString:@"尼古拉斯"]];
    [cmd setHorizontalTab];
    [cmd appendCommandData:[PTEncode encodeDataWithString:@"123456\r\n"]];
    
    [cmd appendCommandData:[PTEncode encodeDataWithString:@"赵四"]];
    [cmd setHorizontalTab];
    [cmd setHorizontalTab];
    [cmd setHorizontalTab];
    [cmd appendCommandData:[PTEncode encodeDataWithString:@"123456\r\n"]];
    
    [PrinterPort sendWithData:[cmd getCommandData]];
}

- (void)printReceipt3
{
    PTCommandESC *cmd = [[PTCommandESC alloc] init];
    
    [cmd setPageMode];
    [cmd pageSetPrintAreaWithXPos:0 yPos:0 width:300 height:100];
    
    [cmd pageSetPrintDirection:0];
    [cmd setAbsolutePosition:10];
    [cmd pageSetAbsoluteYPos:10];
    [cmd appendText:@"hello world"];
    
    [cmd pageSetPrintDirection:0];
    [cmd setAbsolutePosition:200];
    [cmd pageSetAbsoluteYPos:10];
    [cmd appendText:@"123456"];

    [cmd printAndReturnStandardMode];
    [cmd cancelPrint];
    
    [PrinterPort sendWithData:[cmd getCommandData]];
}

- (void)printReceipt
{
//    PTBitmap *bitmap = [[PTBitmap alloc] init];
//    UIImage *logoImage = [UIImage imageNamed:@"logo2.jpg"];
//    NSData *compressData = [bitmap generateMonoBitmapDefault:logoImage compressMode:PTBitmapCompressModeNull];
    
    
//    [cmdESC escPrintRasterBitImageScaleMode:PTBitmapCompressModeNull imageWidth:logoImage.size.width imageHeight:logoImage.size.height imageData:compressData];
//    NSData *logoData =  [cmdESC generateCMDData];
    PTCommandESC *cmd = [[PTCommandESC alloc] init];
    [cmd appendText:@"LUO PRINTER\n\n" mode:ESCTextDoubleWidth];
    
    NSMutableString *companyMessage = [[NSMutableString alloc] init];
    [companyMessage appendString:@"2F,8#,Gaoqi Nan Shi'er Road\n"];
    [companyMessage appendString:@"(Aide Industiral Park)\n"];
    [companyMessage appendString:@"Xiamen,China 361006\n\n"];
    [companyMessage appendString:@"Tel:+(86)-(0)592-5885993,5756958\n"];
    [companyMessage appendString:@"Website:http://www.hprt.com\n"];
    [companyMessage appendString:@"Website:http://www.hprtinc.com\n\n"];
    
    [cmd appendText:companyMessage mode:ESCTextNormal];
    
    NSMutableString *receiptText = [[NSMutableString alloc] init];
    [receiptText appendString:@"------------------------------\n"];
    [receiptText appendString:@"           NO.1008            \n"];
    [receiptText appendString:[self getCurrentTime:@"Time"]];
    [receiptText appendString:@"\n"];
    [receiptText appendString:@"------------------------------\n"];
    [receiptText appendString:@"MPT-II          1      $700.00\n"];
    [receiptText appendString:@"MPT-III         1      $700.00\n"];
    [receiptText appendString:@"LPQ58           1      $800.00\n"];
    [receiptText appendString:@"TP-801          1      $800.00\n"];
    [receiptText appendString:@"TP-805          1      $900.00\n"];
    [receiptText appendString:@"POS80A          1      $900.00\n"];
    [receiptText appendString:@"POS80B          1      $950.00\n"];
    [receiptText appendString:@"POS80C          1      $950.00\n"];
    [receiptText appendString:@"SMP-R381        1      $850.00\n"];
    [receiptText appendString:@"SMP-R385        1      $850.00\n"];
    [receiptText appendString:@"SMP-R386        1      $850.00\n"];
    [receiptText appendString:@"MPT-II          1      $700.00\n"];
    [receiptText appendString:@"MPT-III         1      $700.00\n"];
    [receiptText appendString:@"LPQ58           1      $800.00\n"];
    [receiptText appendString:@"TP-801          1      $800.00\n"];
    [receiptText appendString:@"TP-805          1      $900.00\n"];
    [receiptText appendString:@"POS80A          1      $900.00\n"];
    [receiptText appendString:@"POS80B          1      $950.00\n"];
    [receiptText appendString:@"POS80C          1      $950.00\n"];
    [receiptText appendString:@"SMP-R381        1      $850.00\n"];
    [receiptText appendString:@"SMP-R385        1      $850.00\n"];
    [receiptText appendString:@"SMP-R386        1      $850.00\n"];
    [receiptText appendString:@"------------------------------\n\n"];
    [receiptText appendString:@"Total                $18500.00\n"];
    [receiptText appendString:@"-------------------------------\n\n"];
    
    [cmd appendText:receiptText mode:ESCTextNormal];
    
    int barcodeType         = 0;
    int barcodeAligment     = 0;
    int barcodeNumberAround = 2;
    int barcodeWidth        = 3;
    int barcodeHeight       = 30;
    
    [cmd appendBarcode:barcodeType data:@"075678164125" justification:barcodeAligment width:barcodeWidth height:barcodeHeight hri:barcodeNumberAround];
    [cmd appendQRCodeData:@"Hello World" justification:0 leftMargin:0 eccLevel:48 model:50 size:10];
    
    NSMutableString *copyright = [[NSMutableString alloc] init];
    [copyright appendString:[self getCurrentTime:@"Time"]];
    [copyright appendString:@"\n"];
    [copyright appendString:@"WirelessPrinter: V1.4.0\n"];
    [copyright appendString:@"Copyright 2016 WirelessPrinter.\n"];
    [copyright appendFormat:@" All rights reserved\n\n\n\n"];
    [cmd appendText:copyright mode:ESCTextNormal];
    
    [PrinterPort sendWithData:[cmd getCommandData]];
}

- (void)printReceipt1 {
    
    PTCommandESC *cmd = [[PTCommandESC alloc] init];
//    [cmd appendText:@"hello world"];
//    [cmd appendText:@"-----------------------" mode:ESCTextNormal];
//    [cmd appendText:@"验收"];
//    [cmd setPageMode];
//    [cmd setAbsolutePosition:30];
//    [cmd setLeftMargin:150];
    [cmd setJustification:50];
    UIImage *logoImage = [UIImage imageNamed:@"logo2.jpg"];
    [cmd appendRasterImage:logoImage.CGImage mode:PTBitmapModeBinary compress:PTBitmapCompressModeNone];
//    [cmd setStandardMode];
//    [cmd setJustification:1];
//    [cmd setLineSpacing:56];
//    [cmd appendText:@"\n关注绑定 全国联保" mode:ESCTextNormal];
//    [cmd appendText:@"全国服务热线 400-0889889\n\n\n" mode:ESCTextNormal];
//    [cmd setPartialCut1];
    
    [PrinterPort sendWithData:[cmd getCommandData]];
}

@end
