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

#import "GroupDrawable.h"
#import "ImageDrawable.h"
#import "LineDrawable.h"
#import "TextDrawable.h"
#import "TableDrawable.h"
@implementation GroupDrawable


-(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
{
    
    bool expand = [self.drawableTemplate[@"expand"] boolValue];
    if(!expand)
    {
    CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:y];
    
    return parentrect;
    }
    else
    {
        
        NSMutableDictionary* items = self.drawableTemplate[@"items"];
        int count = [items[@"count"] intValue];

        
        //    CGRect pdfrect = /*CGRectMake(self.rect.origin.x+self.margin_left+p_rect.origin.x, self.rect.origin.y+self.margin_top+p_rect.origin.y, self.rect.size.width-self.margin_right, self.rect.size.height-self.margin_bottom);//*/[self to_pdf_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
        
        CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:x offsetY:y];
        CGRect pdfrect =  [self to_parent_rect:p_rect pos:self.rect offsetX:x offsetY:y];
        
        CGRect expand_rect=parentrect;
        for(int i=0;i<count;i++)
        {
            NSMutableDictionary * item =items[ [NSString stringWithFormat:@"item_%d",i ] ];
//            if([item[@"height"] floatValue]==-1)
//                item[@"height"]= [NSNumber numberWithFloat:self.rect.size.height];

            PDFDrawable* drawable_obj=nil;
            if([item[@"type"] isEqualToString:@"img"])
            {
                drawable_obj= [[ImageDrawable alloc] init:item];
                
            }
            else if([item[@"type"] isEqualToString:@"line"])
            {
                drawable_obj= [[LineDrawable alloc] init:item];
                
            }
            else if([item[@"type"] isEqualToString:@"text"])
            {
                drawable_obj= [[TextDrawable alloc] init:item];
                
            }
            else if([item[@"type"] isEqualToString:@"group"])
            {
                drawable_obj= [[GroupDrawable alloc] init:item];
                
            }
            else if([item[@"type"] isEqualToString:@"table"])
            {
                drawable_obj= [[TableDrawable alloc] init:item];
                
            }
            drawable_obj.delegate=self.delegate;
            CGRect item_rect=[drawable_obj Query_Rect:context dataSource:data ParentRect:(CGRect)pdfrect startX:0 startY:0 flipHeight:flip_height range:between_header_and_footer];
            
            if(expand)
            {
                if(item_rect.origin.y+item_rect.size.height>pdfrect.origin.y+pdfrect.size.height)
                {
                    self.rect=CGRectMake(self.rect.origin.x, self.rect.origin.y, self.rect.size.width, item_rect.origin.y+item_rect.size.height-pdfrect.origin.y+self.margin_bottom+self.margin_top);
                    expand_rect=CGRectMake(parentrect.origin.x, parentrect.origin.y, parentrect.size.width, item_rect.origin.y+item_rect.size.height-pdfrect.origin.y);
                }
            }
        }
    //    [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
        
        if(expand)
            return expand_rect;
        else
            return parentrect;
        //    return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
        //    if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
        //        [self.delegate outofPage:context];
        //    }
    }
}
-(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
{
    
    NSMutableDictionary* items = self.drawableTemplate[@"items"];
    int count = [items[@"count"] intValue];
    
    bool expand = [self.drawableTemplate[@"expand"] boolValue];
    if(expand)
       [self Query_Rect:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
//    CGRect pdfrect = /*CGRectMake(self.rect.origin.x+self.margin_left+p_rect.origin.x, self.rect.origin.y+self.margin_top+p_rect.origin.y, self.rect.size.width-self.margin_right, self.rect.size.height-self.margin_bottom);//*/[self to_pdf_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
    
    CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:x offsetY:y];
    CGRect pdfrect =  [self to_parent_rect:p_rect pos:self.rect offsetX:x offsetY:y];
    
    CGRect expand_rect=parentrect;
    for(int i=0;i<count;i++)
    {
        NSMutableDictionary * item =items[ [NSString stringWithFormat:@"item_%d",i ] ];
        if([item[@"height"] floatValue]==-1)
            item[@"height"]= [NSNumber numberWithFloat:self.rect.size.height];
        PDFDrawable* drawable_obj=nil;
        if([item[@"type"] isEqualToString:@"img"])
        {
            drawable_obj= [[ImageDrawable alloc] init:item];
            
        }
        else if([item[@"type"] isEqualToString:@"line"])
        {
            drawable_obj= [[LineDrawable alloc] init:item];
            
        }
        else if([item[@"type"] isEqualToString:@"text"])
        {
            drawable_obj= [[TextDrawable alloc] init:item];
            
        }
        else if([item[@"type"] isEqualToString:@"group"])
        {
            drawable_obj= [[GroupDrawable alloc] init:item];
            
        }
        else if([item[@"type"] isEqualToString:@"table"])
        {
            drawable_obj= [[TableDrawable alloc] init:item];
            
        }
        drawable_obj.delegate=self.delegate;
        CGRect item_rect=[drawable_obj Draw:context dataSource:data ParentRect:(CGRect)pdfrect startX:0 startY:0 flipHeight:flip_height range:between_header_and_footer];
        
        if(expand)
        {
            if(item_rect.origin.y+item_rect.size.height>pdfrect.origin.y+pdfrect.size.height)
            {
                self.rect=CGRectMake(self.rect.origin.x, self.rect.origin.y, self.rect.size.width, item_rect.origin.y+item_rect.size.height-pdfrect.origin.y+self.margin_top+self.margin_bottom);
                expand_rect=CGRectMake(parentrect.origin.x, parentrect.origin.y, parentrect.size.width, item_rect.origin.y+item_rect.size.height-pdfrect.origin.y);
            }
        }
    }
    [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
    
    if(expand)
        return expand_rect;
    else
        return parentrect;
//    return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
//        [self.delegate outofPage:context];
//    }
}
@end
