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

#import "ImageDrawable.h"
#import "ImageUtils.h"
//#import "iSalesDB.h"

@implementation ImageDrawable


-(instancetype) init:(CGRect) rect source_type:(NSString*) source_type source:(NSString*)source hAlign:(NSString*)hAlign vAlign:(NSString*)vAlign
{
    
    if (self = [super init]) {
        //        NSMutableDictionary* m1=[drawableTemplate mutableCopy];
        //        NSMutableDictionary* m2=[drawableTemplate mutableCopy];
        //        [self setDrawableTemplate:[[RAUtils string2dict:[RAUtils dict2string:drawableTemplate]]  mutableCopy] ];
        
        NSMutableDictionary * drawableTemplate = [ImageDrawable createImageTemplate:rect source_type:source_type source:source hAlign:hAlign vAlign:vAlign];
        
        [self setDrawableTemplate:[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:drawableTemplate]] ];
        
    }
    return self;
    
}
+(NSMutableDictionary *) createImageTemplate:(CGRect) rect source_type:(NSString*) source_type source:(NSString*)source hAlign:(NSString*)hAlign vAlign:(NSString*)vAlign
{

    
    if(hAlign.length==0)
        hAlign= @"Center";
    if(vAlign.length==0)
        vAlign= @"Middle";
    
    
    NSMutableDictionary* ret=[super createDrawableTemplate:rect];
    
    ret[@"type"]=@"img" ;
    ret[@"source_type"]=source_type ;
    ret[@"source"]=source ;
    ret[@"hAlign"]=hAlign ;
    ret[@"vAlign"]=vAlign ;
    
    
    return ret;
}
-(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate
{
    
    [super setDrawableTemplate:drawableTemplate];
    
    
    if(self.hAlign==nil)
    self.hAlign=@"center";
    if(self.vAlign==nil)
    self.vAlign=@"middle";
}
-(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
{
    CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
    
    return parentrect;
}

-(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
{
//    [super flipContext:context ContextHeight:flip_height];
    
    UIImage* image=nil;
    NSString * source=self.drawableTemplate[@"source_type"];
    if([source isEqualToString:@"const"])
    {
        image= [UIImage imageNamed:self.drawableTemplate[@"source"]];
        
    }
    else if([source isEqualToString:@"local"])
    {
        image= [UIImage imageWithContentsOfFile:self.drawableTemplate[@"source"]];
        
    }
    else if([source isEqualToString:@"data"])

    {
        
        NSString* url=data[self.drawableTemplate[@"source"]];
        
        NSString* file_name=[url lastPathComponent];
        file_name=url;
        if(url.length==0)
        {
            image=nil;
                CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
            [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
            
            return parentrect;
        }
        else
        {
            NSData* img_data=[ImageUtils load_cached_img:file_name loadFrom:url];
            if([self.drawableTemplate[@"source_path_type"] isEqualToString:@"local"])
            {
                img_data= [NSData dataWithContentsOfFile: file_name];
            }
            if(img_data!=nil)
            {
                
                image=[UIImage imageWithData:img_data];
                
            }
            else
            {
                image= [UIImage imageNamed:@"portfolio_img_404"];
            }

        }

    }
    
    
    
//    CGRect rect=[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];//CGRectMake(self.rect.origin.x+x, self.rect.origin.y, self.rect.size.width, self.rect.size.height);
//    rect=CGRectMake(rect.origin.x, flip_height-rect.origin.y-rect.size.height, rect.size.width, rect.size.height);
    
    CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
    
    
    
    CGRect scalerect = [ImageUtils scaleToSize:CGRectMake(0, 0, image.size.width, image.size.height) to:parentrect.size];
    
    
    CGRect rect=[ImageUtils rectAlign:parentrect rect:scalerect hAlign:self.hAlign vAlign:self.vAlign];
                        
    //image=[RAUtils scaleToSize:image size:parentrect.size];

    rect= [self parent_to_pdf_rect:rect contextHeight:flip_height];
    //CGRect rect = [self to_pdf_rect:p_rect pos:self.rect contextHeight:flip_height];
    CGContextDrawImage(context, rect, image.CGImage);
    
    
    [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
  
    return parentrect;
//    return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);

    
//    // 切换上下文 Core Graphics 和 UIKit 混用
//    UIGraphicsPushContext(context);
//
//    // 使用UIKit绘制内容
//    [image drawInRect:rect];
//    
//    // 绘制完位图后，再恢复上下文
//    UIGraphicsPopContext();
    
//    [super restoreFlip:context ContextHeight:flip_height];
}


@end
