//
//  Canvas.m
//  CoreSDKExample
//
//  Created by Dominik on 02/04/2015.
//
//

#import "SignatureCanvas.h"

@implementation SignatureCanvas
{
    
}

- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setMultipleTouchEnabled:NO]; // (2)
        [self setBackgroundColor:[UIColor whiteColor]];
        _path = [UIBezierPath bezierPath];
        [_path setLineWidth:2.0];
    }
    return self;
}

- (void)drawRect:(CGRect)rect // (5)
{
    [[UIColor blackColor] setStroke];
    [_path stroke];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [_path moveToPoint:p];
    [_signature startTouch:p.x andY:p.y];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [_path addLineToPoint:p]; // (4)
    [_signature moveTouch:p.x andY:p.y];
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_signature upTouch];
    [self touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

- (void)clearCanvas:(bool)clear{
    _path  = nil;  //Set current path nil
    _path  = [UIBezierPath bezierPath]; //Create new path
    [self setNeedsDisplay];
}

- (void) submitSignature:(WTPSTerminal *)terminal{
    [terminal submitSignature:_signature];
}

- (bool) checkSignature{
    if([_signature checkSignature] == true){
        return true;
    }else{
        return false;
    }
}

@end