//
//  PDFView.m
//  PDFViewTest
//
//  Created by Evan Lynn on 10-6-20.
//  Copyright 2010 Tera Age. All rights reserved.
//

#import "PDFView.h"


@implementation PDFView

- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)drawLine:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 1);  //线宽
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetRGBStrokeColor(context, 0,0,0, 1.0);  //线的颜色
    CGContextBeginPath(context);
    
    CGContextMoveToPoint(context, rect.origin.x, rect.origin.y+rect.size.height);  //起点坐标
    CGContextAddLineToPoint(context, rect.origin.x+rect.size.width, rect.origin.y+rect.size.height);   //终点坐标
    
    CGContextStrokePath(context);
}

-(void)drawWaterMask
{
    if(_imgs != nil)
    {
        int index = 0;
        
        NSArray *arr = @[@"医生签名：",@"受试者签名：",@"监护人签名：",@"见证人签名："];
        for (int i=0; i<_imgs.count; i++) {
            NSString *strBase64 = _imgs[i];
            if (![strBase64 isEqualToString:@""]) {
                NSData *data = [[NSData alloc]initWithBase64EncodedString:strBase64 options:0];
                if (data != nil) {
                    UIImage *img = [UIImage imageWithData:data];
                    CGFloat imgWidth = 70;
                    CGFloat imgHeight = imgWidth*img.size.height/img.size.width;
                    NSString *title = arr[i];
                    CGFloat x =self.bounds.size.width - 220;
                    CGFloat y =self.bounds.size.height-imgHeight*(arr.count - index) - 10;
                    
                    [title drawInRect:CGRectMake(x, y+(imgHeight-20)/2, 100, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
                    
                    
                    [img drawInRect:CGRectMake(x+110, y, imgWidth, imgHeight)];
                    [self drawLine:CGRectMake(x+100, y, 100, imgHeight+1)];
                    index ++;
                }
                
            }
        }
        
    }
}

- (void)drawRect:(CGRect)rect {
    [self drawWaterMask];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGContextDrawPDFPage(context, _page);
}


-(void)reloadView{
    [self setNeedsDisplay];
}



@end
