//
//  BarcodeViewController.m
//  SDK212
//
//  Created by woosimsystems on 2016. 7. 04..
//  Copyright © 2016년 woosimsystems. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "BarcodeViewController.h"
#import "WSSessionController.h"
#import "WSEncoder.h"


WSEncoder *_wsBarcode;
NSMutableData *_printDataBarcode;

@implementation BarcodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"Barcode"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_sessionDataReceived:) name:WSDataReceivedNotification object:nil];
    
    
    _sessionController = [[WSSessionController sharedController]init];
    _wsBarcode = [[WSEncoder alloc]init];
    
}



-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [[EAAccessoryManager sharedAccessoryManager] unregisterForLocalNotifications];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidDisconnectNotification object:nil];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidConnectNotification object:nil];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:WSDataReceivedNotification object:nil];
    
    
    if(_sessionController != nil) _sessionController = nil;
    if (_wsBarcode != nil) _wsBarcode = nil;
    if (_printDataBarcode != nil) _printDataBarcode = nil;
    
}


- (void)_accessoryDidDisconnect:(NSNotification *)notification{
    
    [_sessionController closeSession];
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

/*
//no use in this viewcontroller
- (void)_accessoryDidConnect:(NSNotification *)notification{
    

}
- (void)_sessionDataReceived:(NSNotification *)notification
{
    
    NSInteger bytesAvailable = 0;
    NSMutableData *rcvData = [[NSMutableData alloc] init];
    
    while ((bytesAvailable = [_sessionController getAvailableReadBytes]) > 0) {
        NSData *data = [_sessionController readData:bytesAvailable];
        if (data) {
            [rcvData appendData:data];
        }
    }
}
 */


-(IBAction)Btn_Barcode1D:(id)sender
{
    
    _printDataBarcode = [[NSMutableData alloc]init];
    WSEncoder *_wsBarcode = [[WSEncoder alloc]init];
    
    static NSString *barcode12 = @"065100004328";
    static NSString *barcode11 = @"01234567890";
    static NSString *barcode8 = @"01234567";
    
    [_printDataBarcode appendData:[_wsBarcode clearPrinterBuffer]];
    
    [_printDataBarcode appendData:[_wsBarcode enableHRI:YES]];
    [_printDataBarcode appendData:[_wsBarcode addString:@"UPC-A Barcode\r\n" encoding:NSASCIIStringEncoding]];
    
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode11 barcodeType:BARCODE_UPCA]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"UPC-E Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode12 barcodeType:BARCODE_UPCE]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"EAN13 Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode12 barcodeType:BARCODE_EAN13]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"EAN8 Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode8 barcodeType:BARCODE_EAN8]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"CODE39 Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode11 barcodeType:BARCODE_CODE39]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"ITF Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode8 barcodeType:BARCODE_ITF]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"CODABAR Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode11 barcodeType:BARCODE_CODABAR]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"CODE93 Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode11 barcodeType:BARCODE_CODE93]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"CODE128 Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode11 barcodeType:BARCODE_CODE128]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:5]];
    
    
    [_sessionController writeData:_printDataBarcode];
    
    
}

-(IBAction)Btn_Barcode2D:(id)sender
{
    
    
    _printDataBarcode = [[NSMutableData alloc]init];
    _wsBarcode = [[WSEncoder alloc] init];
    
    static NSString *barcode = @"http://www.woosim.com/";
    
    
    [_printDataBarcode appendData:[_wsBarcode clearPrinterBuffer]];
    [_printDataBarcode appendData:[_wsBarcode enableHRI:NO]];
    
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"PDF417 Barcode\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcodePDF417:barcode column:3 securityLevel:4 HVRatio:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"Data Matrix Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcodeDataMatrix:barcode symbolHeight:0 symbolWidth:0 moduleSize:6]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"QR-Code Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcodeQRCode:barcode symbolVersion:0 ECLevel:'M' moduleSize:5]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"Micro PDF417 Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcodeMicroPDF417:barcode column:2 row:0 HVRatio:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"Truncated PDF417 Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcodeTruncPDF417:barcode column:3 securityLevel:4 HVRatio:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"Maxicode Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcodeMaxicode:barcode mode:4]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:5]];


    
    [_sessionController writeData:_printDataBarcode];
}

-(IBAction)Btn_BarcodeGS1:(id)sender
{

    static NSString *barcode = @"0001234567890\0";
    static NSString *barcode5 = @"[01]90012345678908[3103]012233\0";
    static NSString *barcode6 = @"[01]90012345678908[3103]012233[15]991231\0";

    _printDataBarcode = [[NSMutableData alloc]init];
    _wsBarcode = [[WSEncoder alloc] init];
    
    [_printDataBarcode appendData:[_wsBarcode clearPrinterBuffer]];
    
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"GS1 Databar type0\n" encoding:NSASCIIStringEncoding]];
    
    [_printDataBarcode appendData:[_wsBarcode createGS1Databar:barcode type:0 rowSegment:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"GS1 Databar type1\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createGS1Databar:barcode type:1 rowSegment:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"GS1 Databar type2\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createGS1Databar:barcode type:2 rowSegment:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"GS1 Databar type3\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createGS1Databar:barcode type:3 rowSegment:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"GS1 Databar type4\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createGS1Databar:barcode type:4 rowSegment:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    [_printDataBarcode appendData:[_wsBarcode addString:@"GS1 Databar type5\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createGS1Databar:barcode5 type:5 rowSegment:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"GS1 Databar type6\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createGS1Databar:barcode6 type:6 rowSegment:4]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:5]];
    
    
    [_sessionController writeData:_printDataBarcode];

}


-(IBAction)Btn_AlignBarcode:(id)sender{
    
    _printDataBarcode = [[NSMutableData alloc]init];
    WSEncoder *_wsBarcode = [[WSEncoder alloc]init];
    
    static NSString *barcode12 = @"065100004328";
    static NSString *barcode11 = @"01234567890";
    static NSString *barcode = @"http://www.woosim.com/";
    
    [_printDataBarcode appendData:[_wsBarcode clearPrinterBuffer]];
    [_printDataBarcode appendData:[_wsBarcode enableHRI:YES]];
    
    [_printDataBarcode appendData:[_wsBarcode setAlignInStandardMode:ALIGN_CENTER]];
    [_printDataBarcode appendData:[_wsBarcode addString:@"*Center\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode addString:@"UPC-A Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode11 barcodeType:BARCODE_UPCA]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"UPC-E Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcode:barcode12 barcodeType:BARCODE_UPCE]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    

    [_printDataBarcode appendData:[_wsBarcode enableHRI:NO]];
    
    [_printDataBarcode appendData:[_wsBarcode setAlignInStandardMode:ALIGN_RIGHT]];
    [_printDataBarcode appendData:[_wsBarcode addString:@"*Right\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode addString:@"PDF417 Barcode\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcodePDF417:barcode column:3 securityLevel:4 HVRatio:2]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    [_printDataBarcode appendData:[_wsBarcode addString:@"Data Matrix Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcodeDataMatrix:barcode symbolHeight:0 symbolWidth:0 moduleSize:6]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:1]];
    
    
    [_printDataBarcode appendData:[_wsBarcode setAlignInStandardMode:ALIGN_LEFT]];
    [_printDataBarcode appendData:[_wsBarcode addString:@"*Left\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode addString:@"QR-Code Barcode\r\n" encoding:NSASCIIStringEncoding]];
    [_printDataBarcode appendData:[_wsBarcode createBarcodeQRCode:barcode symbolVersion:0 ECLevel:'M' moduleSize:5]];
    [_printDataBarcode appendData:[_wsBarcode printAndFeedNLineInStandardMode:3]];
    
    [_sessionController writeData:_printDataBarcode];
    
}




@end
