//
//  SignatureViewController.m
//  RedAnt ERP Mobile
//
//  Created by Ray on 14-8-7.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//

#import "SignatureViewController.h"
//#import "const.h"
@interface SignatureViewController ()

@end

@implementation SignatureViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)onBackClick:(UIButton *)sender {
    
    
    [self.navigationController popViewControllerAnimated:FALSE];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = self.title;
    
    
    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                                    style:UIBarButtonItemStylePlain
                                                                   target:self
                                                                   action:@selector( onBackClick:)];
    
 //   closeButton.tintColor = UIColorFromRGB(0x996633);
    self.navigationItem.leftBarButtonItem=closeButton;
    
    
    UIBarButtonItem *clearBtn=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"clear"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                              action:@selector( onClear:)];
     //   clearBtn.tintColor = UIColorFromRGB(0x996633);
    UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                              style:UIBarButtonItemStylePlain
                                                             target:self
                                                             action:@selector( onDone:)];
     //   doneBtn.tintColor = UIColorFromRGB(0x996633);
    
    //  id aa = self.navigationItem;
    
    //  self.navi_item.title = @"ttttt";
    
    //  self.navigationItem.rightBarButtonItem = searchButton;
    
    
    self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:doneBtn,clearBtn , nil];
    
    

    
    [self.signatureView setLineWidth:10.0];
    self.signatureView.foregroundLineColor = [UIColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.000];
//    UIImage* img = [UIImage alloc]initw
    
    
    
  //  cgimagecre
    if(self.existSignature!= nil)
    {
        CGSize size =self.signatureView.bounds.size;
    self.signatureView.image = [self addImage:self.existSignature size:size] ;
        self.signatureView.boundary =CGRectMake((size.width-self.existSignature.size.width)/2,(size.height-self.existSignature.size.height)/2, self.existSignature.size.width, self.existSignature.size.height);
    }
    // Do any additional setup after loading the view.
}
- (UIImage *)addImage:(UIImage *)image size:(CGSize )size {
    UIGraphicsBeginImageContext(size);
    UIImage *cleanImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // Draw image1
    
    
    [image drawInRect:CGRectMake((size.width-image.size.width)/2,(size.height-image.size.height)/2, image.size.width, image.size.height)];
    
    // Draw image2
    [cleanImage drawInRect:CGRectMake(0, 0, cleanImage.size.width, cleanImage.size.height)];
    
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resultingImage;
}
- (void)onDone:(UIButton *)sender {
    
    
    
    UIImage* img = [self.signatureView signatureImage];

//    CGSize imgsize = img.size;
    NSData* data = [self.signatureView signatureData];
    
   if (self.delegate && [self.delegate respondsToSelector:@selector(SignatureVCReturnImage:indexPath:)]) {
       [self.delegate SignatureVCReturnImage:img indexPath:self.indexPath];
   }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(SignatureVCReturnData:indexPath:)]) {
        [self.delegate SignatureVCReturnData:data indexPath:self.indexPath];
    }
    if(self.onReturnImg)
        self.onReturnImg(img);
    [self.navigationController popViewControllerAnimated:true];
    
    //    [self.aBNewPersonNav dismissViewControllerAnimated:true completion:^{
    //        ;
    //    }];
}
- (void)onClear:(UIButton *)sender {
    [self.signatureView clear];

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
