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

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

@implementation SignPadViewController

WSEncoder *_wsSP;
NSMutableData *_printDataSP;

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setTitle:@"Image + Signature Pad"];
    
}

-(void) viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if (signPad == nil) {
        
        signPad = [[UIImageView alloc]init];
        signPad.frame = [self.signPadView frame];
        [self.view addSubview:signPad];
        self.view.backgroundColor = [UIColor whiteColor];
        
    }
}

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

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_wifiDidDisConnect:) name:socketDidDisconnectNotification object:nil];
    
    _sessionController = [[WSSessionController sharedController]init];
    _wsSP = [[WSEncoder alloc]init];
    
}



-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:socketDidDisconnectNotification object:nil];
    
    if (signPad.image) {
        signPad.image = nil;
    }
    
    
    if(_sessionController != nil) _sessionController = nil;
    if (_wsSP != nil) _wsSP = nil;
    if (_printDataSP != nil) _printDataSP = nil;
    
}

- (void)_wifiDidDisConnect:(NSNotification *)notification{

    [self.navigationController popToRootViewControllerAnimated:YES];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.signPadView];
    lastPoint.y -=0;
    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    // touch locationInView:self.card 라고 되어있는데 이건 UIImageView를 가리키는 부분이다.
    currentPoint = [touch locationInView:self.signPadView];
    currentPoint.y -= 0;
    
    UIGraphicsBeginImageContextWithOptions(self.signPadView.frame.size, NO, NO);
    [signPad.image drawInRect:CGRectMake(0, 0, self.signPadView.frame.size.width, self.signPadView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    signPad.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    lastPoint = currentPoint;
    
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    if (!mouseSwiped) {
        
        UIGraphicsBeginImageContext(self.signPadView.frame.size);
        [signPad.image drawInRect:CGRectMake(0, 0, self.signPadView.frame.size.width, self.signPadView.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        signPad.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
    }
}


-(IBAction)Btn_PrintSignPad:(id)sender
{
    if (signPad.image) {
        
        _printDataSP = [[NSMutableData alloc]init];
        _wsSP = [[WSEncoder alloc] init];
        
        //싸인이미지 출력
        UIGraphicsBeginImageContext(self.signPadView.frame.size);
        [signPad.layer renderInContext:UIGraphicsGetCurrentContext()];
        
        UIImage *signImg =  UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        [_printDataSP appendData:[_wsSP clearPrinterBuffer]];
        [_printDataSP appendData:[_wsSP addString:@"*Sign Pad\n" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP createImageInStandardMode:[signImg CGImage] withShiftPosition:0x00 withDithering:NO]];
        [_printDataSP appendData:[_wsSP printImageInStandardMode]];
        [_printDataSP appendData:[_wsSP printAndFeedNLineInStandardMode:5]];
        
        [_sessionController writeData:_printDataSP];
        signPad.image = nil;
        return;
        
    }
    
    alert = [[UIAlertView alloc]
             initWithTitle:nil
             message:@"\r\nPlease write signature.\r\n"
             delegate:self
             cancelButtonTitle:@"OK"
             otherButtonTitles:nil];
    
    [alert show];
    
    
}

-(IBAction)Btn_PrintWithSample:(id)sender
{
    if(signPad.image)
    {
        
        _printDataSP = [[NSMutableData alloc]init];
        _wsSP = [[WSEncoder alloc] init];
        [_printDataSP appendData:[_wsSP clearPrinterBuffer]];
        
        //2inch printer: 384dot, 3inch printer: 576dot, 4inch printer: 832dot
        [_printDataSP appendData:[_wsSP setPrintableAreaWidthInStandardMode:384]];
        
        //이미지 출력
        UIImage *wsLogo = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"bmp"]];
        
        [_printDataSP appendData:[_wsSP createImageInStandardMode:[wsLogo CGImage] withShiftPosition:0x00 withDithering:NO]];
        [_printDataSP appendData:[_wsSP printImageInStandardMode]];
        
        [_printDataSP appendData:[_wsSP setFontSize:FONTSIZE_A]];
        [_printDataSP appendData:[_wsSP setTextAlignInStandardMode:ALIGN_CENTER]];
        //영수증 주소 출력
        NSString *address = @"\r\n#501, Daerung Technotown 3rd,\r\n448 Gasan-Dong, Geumchun-Gu,\r\nSeoul, KOREA\r\n82-2-2107-3700\r\nhttp://www.woosim.com\r\n\r\n";
        
        [_printDataSP appendData:[_wsSP addString:address encoding:NSASCIIStringEncoding]];
        
        [_printDataSP appendData:[_wsSP setTextAlignInStandardMode:ALIGN_RIGHT]];
        NSDate *time = [NSDate date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy/M/d H:mm"];
        NSString *dateString = [dateFormatter stringFromDate:time];
        [_printDataSP appendData:[_wsSP addString:dateString encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP printDataInStandardMode]];
        
        [_printDataSP appendData:[_wsSP setTextAlignInStandardMode:ALIGN_LEFT]];
        NSString *line = @"--------------------------------\r\n";
        NSString *title = @"  ITEM         QTY       PRICE\r\n";
        
        [_printDataSP appendData:[_wsSP addString:line encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP addString:title encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP addString:line encoding:NSASCIIStringEncoding]];
        
        uint16_t left = 0x10;
        uint16_t center = 0xb8;
        uint16_t right = 0x135;
        [_printDataSP appendData:[_wsSP printDataInStandardMode]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:left]];
        [_printDataSP appendData:[_wsSP addString:@"itemA" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:center]];
        [_printDataSP appendData:[_wsSP addString:@"1" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:right]];
        [_printDataSP appendData:[_wsSP addString:@"3$\n" encoding:NSASCIIStringEncoding]];
        
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:left]];
        [_printDataSP appendData:[_wsSP addString:@"itemB" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:center]];
        [_printDataSP appendData:[_wsSP addString:@"1" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:right]];
        [_printDataSP appendData:[_wsSP addString:@"4$\n" encoding:NSASCIIStringEncoding]];
        
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:left]];
        [_printDataSP appendData:[_wsSP addString:@"itemC" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:center]];
        [_printDataSP appendData:[_wsSP addString:@"1" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:right]];
        [_printDataSP appendData:[_wsSP addString:@"5$\n" encoding:NSASCIIStringEncoding]];
        
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:left]];
        [_printDataSP appendData:[_wsSP addString:@"itemD" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:center]];
        [_printDataSP appendData:[_wsSP addString:@"1" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:right]];
        [_printDataSP appendData:[_wsSP addString:@"6$\n\n" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP addString:line encoding:NSASCIIStringEncoding]];
        
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:left]];
        [_printDataSP appendData:[_wsSP setTextBold:YES]];
        [_printDataSP appendData:[_wsSP addString:@"TOTAL:" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:right]];
        [_printDataSP appendData:[_wsSP addString:@"18$\n\n" encoding:NSASCIIStringEncoding]];
        
        [_printDataSP appendData:[_wsSP setTextBold:NO]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:left]];
        
        [_printDataSP appendData:[_wsSP addString:@"WOOSIM Card\n" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:left]];
        [_printDataSP appendData:[_wsSP addString:@"1340 0431 **** ****\n\n" encoding:NSASCIIStringEncoding]];
        [_printDataSP appendData:[_wsSP shiftAbsolutePosition:left]];
        [_printDataSP appendData:[_wsSP addString:@"Signature\n" encoding:NSASCIIStringEncoding]];
        
        UIGraphicsBeginImageContext(self.signPadView.frame.size);
        [signPad.layer renderInContext:UIGraphicsGetCurrentContext()];
        
        UIImage *signImg =  UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        [_printDataSP appendData:[_wsSP createImageInStandardMode:[signImg CGImage] withShiftPosition:0x1b withDithering:NO]];
        [_printDataSP appendData:[_wsSP printImageInStandardMode]];
        [_printDataSP appendData:[_wsSP printAndFeedNLineInStandardMode:5]];
        
        signPad.image = nil;
        [_sessionController writeData:_printDataSP];
        
        return;
    }
    
    alert = [[UIAlertView alloc]
             initWithTitle:nil
             message:@"\r\nPlease write signature.\r\n"
             delegate:self
             cancelButtonTitle:@"OK"
             otherButtonTitles:nil];
    
    [alert show];
    
}


@end

