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

#import "PDFDrawable.h"
#import "LineDrawable.h"
#import "config.h"
//#import "RAUtils.h"


@implementation PDFDrawable
+(NSMutableDictionary *) createDrawableTemplate:(CGRect) rect
{
    NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
    
    ret[@"x"]=[NSNumber numberWithDouble:rect.origin.x] ;
    ret[@"y"]=[NSNumber numberWithDouble:rect.origin.y] ;
    ret[@"width"]=[NSNumber numberWithDouble:rect.size.width] ;
    ret[@"height"]=[NSNumber numberWithDouble:rect.size.height] ;
    
    return ret;
}

-(instancetype) init:(NSMutableDictionary *)drawableTemplate
{
    
    if (self = [super init]) {
//        NSMutableDictionary* m1=[drawableTemplate mutableCopy];
//        NSMutableDictionary* m2=[drawableTemplate mutableCopy];
//        [self setDrawableTemplate:[[RAUtils string2dict:[RAUtils dict2string:drawableTemplate]]  mutableCopy] ];
        [self setDrawableTemplate:[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:drawableTemplate]] ];

    }
    return self;
    
}
-(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate
{
    _drawableTemplate = drawableTemplate;
    self.margin_left=[self.drawableTemplate[@"margin_left"] doubleValue];
    self.margin_right=[self.drawableTemplate[@"margin_right"] doubleValue];
    self.margin_top=[self.drawableTemplate[@"margin_top"] doubleValue];
    self.margin_bottom=[self.drawableTemplate[@"margin_bottom"] doubleValue];
    self.hAlign =self.drawableTemplate[@"hAlign"];
    self.vAlign =self.drawableTemplate[@"vAlign"];
    self.rect = CGRectMake([self.drawableTemplate[@"x"] doubleValue], [self.drawableTemplate[@"y"] doubleValue], [self.drawableTemplate[@"width"] doubleValue], [self.drawableTemplate[@"height"] doubleValue]);
 //   self.drawable_rect = CGRectMake(self.rect.origin.x+self.margin_left, self.rect.origin.y+self.margin_top, self.rect.size.width-self.margin_right, self.rect.size.height-self.margin_bottom);
}
-(void) DrawBound:(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
{
    
    if(!PDF_DEBUG&&![self.drawableTemplate[@"draw_bound"] boolValue] )
    return;
    
    
    NSArray* arr_linewidth=self.drawableTemplate[@"line_width"];
    CGRect rect = [self rect];//[self to_pdf_rect:p_rect pos:self.rect contextHeight:flip_height];


    

    
    
    if(arr_linewidth.count<4)
        arr_linewidth = [NSArray arrayWithObjects:@(0.05),@(0.05),@(0.05),@(0.05), nil];
    
    NSMutableDictionary* tline1= [LineDrawable createlineTemplate:[arr_linewidth[0] floatValue] from:CGPointMake(rect.origin.x, rect.origin.y) to:CGPointMake(rect.origin.x+rect.size.width, rect.origin.y)];
        LineDrawable* line1=nil;
    line1= [[LineDrawable alloc] init:tline1];
    [line1 Draw:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
    
    NSMutableDictionary* tline2= [LineDrawable createlineTemplate:[arr_linewidth[1] floatValue] from:CGPointMake(rect.origin.x+rect.size.width, rect.origin.y) to:CGPointMake(rect.origin.x+rect.size.width, rect.origin.y+rect.size.height)];
    LineDrawable* line2=nil;
    line2= [[LineDrawable alloc] init:tline2];
    [line2 Draw:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
    
    NSMutableDictionary* tline3= [LineDrawable createlineTemplate:[arr_linewidth[2] floatValue] from:CGPointMake(rect.origin.x+rect.size.width, rect.origin.y+rect.size.height) to:CGPointMake(rect.origin.x, rect.origin.y+rect.size.height)];
    LineDrawable* line3=nil;
    line3= [[LineDrawable alloc] init:tline3];
    [line3 Draw:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
    
    NSMutableDictionary* tline4= [LineDrawable createlineTemplate:[arr_linewidth[3] floatValue] from:CGPointMake(rect.origin.x, rect.origin.y+rect.size.height) to:CGPointMake(rect.origin.x, rect.origin.y)];
    LineDrawable* line4=nil;
    line4= [[LineDrawable alloc] init:tline4];
    [line4 Draw:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
    
}


-(CGRect)to_parent_rect:(CGRect)parentRect pos:(CGRect)rect offsetX:(double)x offsetY:(double)y
{
    return CGRectMake(parentRect.origin.x+self.margin_left+rect.origin.x+x, parentRect.origin.y+self.margin_top+self.rect.origin.y+y/*-self.rect.size.height*/, self.rect.size.width-self.margin_left-self.margin_right, self.rect.size.height-self.margin_top-self.margin_bottom);
}
-(CGRect)to_pdf_rect:(CGRect)parentRect pos:(CGRect)rect contextHeight:(double) height
{
    CGRect pdfrect=[self to_parent_rect:parentRect pos:rect offsetX:0 offsetY:0];//CGRectMake(self.rect.origin.x+x, self.rect.origin.y, self.rect.size.width, self.rect.size.height);
    pdfrect=CGRectMake(pdfrect.origin.x, height-pdfrect.origin.y-pdfrect.size.height, pdfrect.size.width, pdfrect.size.height);
    return pdfrect;
}
-(CGRect)parent_to_pdf_rect:(CGRect)rect contextHeight:(double) height
{
  //  CGRect pdfrect=[self to_parent_rect:parentRect pos:rect offsetX:0 offsetY:0];//CGRectMake(self.rect.origin.x+x, self.rect.origin.y, self.rect.size.width, self.rect.size.height);
    CGRect pdfrect=CGRectMake(rect.origin.x, height-rect.origin.y-rect.size.height, rect.size.width, rect.size.height);
    return pdfrect;
}

-(CGPoint)to_parent_point:(CGRect)parentRect point:(CGPoint)p offsetX:(double)x offsetY:(double)y
{
    //return CGRectMake(parentRect.origin.x+self.margin_left+rect.origin.x, parentRect.size.height-self.margin_top-self.rect.origin.y-self.rect.size.height, rect.size.width-self.margin_right, rect.size.height-self.margin_bottom);
    
    return CGPointMake(parentRect.origin.x+ p.x, parentRect.origin.y+p.y);
}

-(CGPoint)to_pdf_point:(CGRect)parentRect point:(CGPoint)p contextHeight:(double) height
{
    
    CGPoint pdfpoint=[self to_parent_point:parentRect point:p offsetX:0 offsetY:0];
    
    pdfpoint = CGPointMake(pdfpoint.x, height-pdfpoint.y);
    return pdfpoint;
}

//-(void) Draw:(CGContextRef) context startX:(double) x startY:(double) y
//{
//
//}

@end
