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

#import "GridDrawable.h"
#import "GroupDrawable.h"
@implementation GridDrawable
-(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
{
    
    
    NSMutableDictionary* table_data = data[self.drawableTemplate[@"data_source"]];
    
    NSMutableDictionary* celljson = self.drawableTemplate[@"cell"];
    NSString* direction = celljson[@"direction"];
    if(direction==nil)
    direction=@"H";
    
    //    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 pdfrect =  [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
    CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
    
    PDFDrawable* drawable_obj= [[GroupDrawable alloc] init:celljson];
    drawable_obj.delegate = self.delegate;
    NSMutableDictionary* rows_data = table_data[celljson[@"data_source"]];
    int row_data_count=[rows_data[@"count"] intValue];
    float dy=y;
    float dx=0;
    for(int j=0;j<row_data_count;j++)
    {
        
        
        
        NSMutableDictionary * row_data =rows_data[ [NSString stringWithFormat:@"item_%d",j ] ];
        CGRect row_rect= [drawable_obj Query_Rect:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
        
        if(row_rect.size.width>parentrect.size.width||row_rect.size.height>parentrect.size.height)
        {
            // cell 比 grid 还要大，模板有错，直接返回。
            return parentrect;
        }
        if([direction isEqualToString:@"H"])
        {
            if(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length )
            {
                //越界，新起一页
                if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
                    [self.delegate addPageCount:context];
                    dy=y;
                    dx=0;
                }
            }
        }
        else
        {
            if(/*dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length && */dx+row_rect.size.width>parentrect.size.width )
            {
                //越界，新起一页
                if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
                    [self.delegate addPageCount:context];
                    dy=y;
                    dx=0;
                }
            }
            
        }
        
        
        
        
    //    [drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:dx startY:dy flipHeight:flip_height range:between_header_and_footer];
        if([direction isEqualToString:@"H"])
        {
            dx+=row_rect.size.width;
            if(dx+row_rect.size.width>parentrect.size.width)
            {
                dx=0;
                dy+=row_rect.size.height;
            }
            
        }
        else
        {
            dy+=row_rect.size.height;
            if(dy-y+row_rect.size.height>parentrect.size.height)
            {
                dy=y;
                dx+=row_rect.size.width;
            }
            
            
        }
        
        
        
    }
    
    
    
    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* table_data = data[self.drawableTemplate[@"data_source"]];
    
    NSMutableDictionary* celljson = self.drawableTemplate[@"cell"];
    NSString* direction = celljson[@"direction"];
    if(direction==nil)
        direction=@"H";
    
    //    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 pdfrect =  [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
    CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
    
    PDFDrawable* drawable_obj= [[GroupDrawable alloc] init:celljson];
    drawable_obj.delegate = self.delegate;
    NSMutableDictionary* rows_data = table_data[celljson[@"data_source"]];
    int row_data_count=[rows_data[@"count"] intValue];
    float dy=y;
    float dx=0;
    for(int j=0;j<row_data_count;j++)
    {
        
        
        
        NSMutableDictionary * row_data =rows_data[ [NSString stringWithFormat:@"item_%d",j ] ];
        CGRect row_rect= [drawable_obj Query_Rect:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
        
        if(row_rect.size.width>parentrect.size.width||row_rect.size.height>parentrect.size.height)
        {
            // cell 比 grid 还要大，模板有错，直接返回。
            return parentrect;
        }
        if([direction isEqualToString:@"H"])
        {
            if(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length )
            {
                //越界，新起一页
                if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
                    [self.delegate outofPage:context];
                    dy=y;
                    dx=0;
                }
            }
        }
        else
        {
            if(/*dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length && */dx+row_rect.size.width>parentrect.size.width )
            {
                //越界，新起一页
                if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
                    [self.delegate outofPage:context];
                    dy=y;
                    dx=0;
                }
            }
            
        }

        
        
        
        [drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:dx startY:dy flipHeight:flip_height range:between_header_and_footer];
        if([direction isEqualToString:@"H"])
        {
            dx+=row_rect.size.width;
            if(dx+row_rect.size.width>parentrect.size.width)
            {
                dx=0;
                dy+=row_rect.size.height;
            }

        }
        else
        {
            dy+=row_rect.size.height;
            if(dy-y+row_rect.size.height>parentrect.size.height)
            {
                dy=y;
                dx+=row_rect.size.width;
            }

            
        }
        
        
        
    }
    
    
    
    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
