//
//  LineDrawable.m
//  pdftest
//
//  Created by Ray on 10/15/16.
//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
//

#import "LineDrawable.h"

@implementation LineDrawable


+(NSMutableDictionary *) createlineTemplate:(double) width from:(CGPoint) from to:(CGPoint) to
{
    NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
//    "type": "line",
//    "x0": 10,
//    "y0": 10,
//    "x1": 100,
//    "y1": 100,
//    "line_width": 0.02
    ret[@"type"]=@"line" ;
    ret[@"x0"]=[NSNumber numberWithDouble:from.x] ;
    ret[@"y0"]=[NSNumber numberWithDouble:from.y] ;
    ret[@"x1"]=[NSNumber numberWithDouble:to.x] ;
    ret[@"y1"]=[NSNumber numberWithDouble:to.y] ;
    ret[@"line_width"]=[NSNumber numberWithDouble:width] ;

    return ret;
}

-(CGRect) Query_Rect:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer
{
    
    return CGRectMake(0, 0, 0, 0);
}

-(CGRect) Draw:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer
{
    CGContextSaveGState(context);
    //        CGContextSetRGBStrokeColor(context,1,1,1,1.0);//画笔线的颜色
    CGContextSetLineWidth(context, [self.drawableTemplate[@"line_width"] doubleValue]);
//    CGContextMoveToPoint(context, [self.drawableTemplate[@"x0"] doubleValue], [self.drawableTemplate[@"y0"] doubleValue]);
//    
//    CGContextAddLineToPoint(context, [self.drawableTemplate[@"x1"] doubleValue], [self.drawableTemplate[@"y1"] doubleValue]);
//    
//    CGContextStrokePath(context);
    
    
    CGPoint aPoints[2];//坐标点
    aPoints[0] =[self to_pdf_point:p_rect point:CGPointMake([self.drawableTemplate[@"x0"] doubleValue]+x, [self.drawableTemplate[@"y0"] doubleValue]+y) contextHeight:flip_height];//坐标1
    aPoints[1] =[self to_pdf_point:p_rect point:CGPointMake([self.drawableTemplate[@"x1"] doubleValue]+x, [self.drawableTemplate[@"y1"] doubleValue]+y) contextHeight:flip_height];//坐标2
    
    
    DebugLog(@"line from %@ to %@",NSStringFromCGPoint(aPoints[0]),NSStringFromCGPoint(aPoints[1]));
    
    //CGContextAddLines(CGContextRef c, const CGPoint points[],size_t count)
    //points[]坐标数组，和count大小
    CGContextAddLines(context, aPoints, 2);//添加线
    CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径
    
    CGContextRestoreGState(context);
    
     return CGRectMake(0, 0, 0, 0);
}
@end
