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

#import "PDFPage.h"
#import "GroupDrawable.h"
#import "ImageDrawable.h"
#import "LineDrawable.h"
#import "TextDrawable.h"
#import "TableDrawable.h"
#import "GridDrawable.h"

@implementation PDFPage
-(instancetype) init:(NSMutableDictionary *)page size:(CGRect)rect
{
    if (self = [super init]) {
        self.rect = rect;
        [self setPageTemplate:page];
        self.pagecount=1;
    }
    return self;
    
    
    
}
-(void) setPageTemplate:(NSMutableDictionary *)pageTemplate
{
    _pageTemplate = pageTemplate;
    self.margin_left=[self.pageTemplate[@"margin_left"] doubleValue];
    self.margin_right=[self.pageTemplate[@"margin_right"] doubleValue];
    self.margin_top=[self.pageTemplate[@"margin_top"] doubleValue];
    self.margin_bottom=[self.pageTemplate[@"margin_bottom"] doubleValue];
    self.header=self.pageTemplate[@"header"];
    self.footer=self.pageTemplate[@"footer"];
    
    
    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.margin_left), self.rect.size.height-(self.margin_bottom+self.margin_top));
    
    self.pos_x = _margin_left;
    self.pos_y = _margin_top;
    
    // self.rect = CGRectMake(0, 0, [self.pageTemplate[@"width"] doubleValue], [self.pageTemplate[@"height"] doubleValue]);
}
-(int) QueryPageCount:(CGContextRef) context dataSource:(NSMutableDictionary*)data
{
    
    
    
    
    self.data=data;
    GroupDrawable* pageHeader = [[GroupDrawable alloc]init:self.header];
    
    
    self.header_height=pageHeader.rect.size.height;
    pageHeader.delegate = self;
    GroupDrawable* pageFooter = [[GroupDrawable alloc]init:self.footer];
    
    pageFooter.delegate = self;
    
    
    
    
    // while (true)
    {
        // page 去除页眉，页脚后的区域
        NSRange range = NSMakeRange(self.drawable_rect.origin.y+pageHeader.rect.size.height, self.drawable_rect.origin.y+self.drawable_rect.size.height-pageFooter.rect.size.height);
        
        CGRect rect = self.rect;
        
        //       CGContextBeginPage(context, &rect);
        //       [pageHeader Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
        
        
        for (int i=0;i<[self.pageTemplate[@"contents"][@"count"] intValue];i++)
        {
            NSMutableDictionary * item =self.pageTemplate[@"contents"][ [NSString stringWithFormat:@"item_%d",i ] ];
            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];
                
            }
            else if([item[@"type"] isEqualToString:@"grid"])
            {
                drawable_obj= [[GridDrawable alloc] init:item];
                
            }
            drawable_obj.delegate = self;
            [drawable_obj Query_Rect:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:self.header_height flipHeight:self.rect.size.height range:range];
        }
        
        //    [pageFooter Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
        //    CGContextEndPage(context);
        
    }
    
    return self.pagecount;
}
-(void) DirectDraw:(CGContextRef) context page:(CGPDFPageRef)page
{
    
    
    
//    CGAffineTransform m;
//    m = CGPDFPageGetDrawingTransform (page, kCGPDFCropBox, self.rect, -180, true);
    CGContextSaveGState (context);
    
//    CGContextTranslateCTM(myContext, 80, self.frame.size.height);
//    CGContextScaleCTM(myContext, 1, -1);
    
    CGContextDrawPDFPage (context, page);
    CGContextRestoreGState (context);
    
    
}
-(void) DirectDraw:(CGContextRef) context drawable:(PDFDrawable*)drawable_obj
{
    
    
    if(drawable_obj==nil)
        return;
    
//    self.data=data;
//    
//    
//    
//    GroupDrawable* pageHeader = [[GroupDrawable alloc]init:self.header];
//    self.header_height=pageHeader.rect.size.height;
//    pageHeader.delegate = self;
//    GroupDrawable* pageFooter = [[GroupDrawable alloc]init:self.footer];
//    
//    pageFooter.delegate = self;
//    
//    
//    
//    
//    // while (true)
//    {
//        // page 去除页眉，页脚后的区域
        NSRange range = NSMakeRange(0,self.rect.size.height);
//        //        NSRange range = NSMakeRange(self.drawable_rect.origin.y+pageHeader.rect.size.height, self.drawable_rect.size.height-pageFooter.rect.size.height+self.drawable_rect.size.height);
        CGRect rect = self.rect;
    
//        CGContextBeginPage(context, &rect);
//        [pageHeader Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
//        
//        [pageFooter Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
//        
//        for (int i=0;i<[self.pageTemplate[@"contents"][@"count"] intValue];i++)
//        {
//            NSMutableDictionary * item =self.pageTemplate[@"contents"][ [NSString stringWithFormat:@"item_%d",i ] ];
//            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];
//                
//            }
//            else if([item[@"type"] isEqualToString:@"grid"])
//            {
//                drawable_obj= [[GridDrawable alloc] init:item];
//                
//            }
            drawable_obj.delegate = self;
            [drawable_obj Draw:context dataSource:nil ParentRect:(CGRect)self.drawable_rect startX:0 startY:self.header_height flipHeight:self.rect.size.height range:range];
//        }
    
//        CGContextEndPage(context);
        
//    }
}
-(void) Draw:(CGContextRef) context dataSource:(NSMutableDictionary*)data
{
    
    
    
    
    self.data=data;
    
    
    
    GroupDrawable* pageHeader = [[GroupDrawable alloc]init:self.header];
    self.header_height=pageHeader.rect.size.height;
    pageHeader.delegate = self;
    GroupDrawable* pageFooter = [[GroupDrawable alloc]init:self.footer];
    
    pageFooter.delegate = self;
    
    
    
    
    // while (true)
    {
        // page 去除页眉，页脚后的区域
        NSRange range = NSMakeRange(self.drawable_rect.origin.y+pageHeader.rect.size.height, /*self.drawable_rect.origin.y+*/self.drawable_rect.size.height-pageFooter.rect.size.height-pageHeader.rect.size.height);
//        NSRange range = NSMakeRange(self.drawable_rect.origin.y+pageHeader.rect.size.height, self.drawable_rect.size.height-pageFooter.rect.size.height+self.drawable_rect.size.height);
        CGRect rect = self.rect;
        
        CGContextBeginPage(context, &rect);
        [pageHeader Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
        
        [pageFooter Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
        
        for (int i=0;i<[self.pageTemplate[@"contents"][@"count"] intValue];i++)
        {
            NSMutableDictionary * item =self.pageTemplate[@"contents"][ [NSString stringWithFormat:@"item_%d",i ] ];
            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];
                
            }
            else if([item[@"type"] isEqualToString:@"grid"])
            {
                drawable_obj= [[GridDrawable alloc] init:item];
                
            }
            drawable_obj.delegate = self;
            [drawable_obj Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:self.header_height flipHeight:self.rect.size.height range:range];
        }
        
        CGContextEndPage(context);
        
    }
}
-(void) nextPage:(CGContextRef) context
{
    CGContextEndPage(context);
    CGRect rect = self.rect;
    
    CGContextBeginPage(context, &rect);
    
    
    
    GroupDrawable* pageHeader = [[GroupDrawable alloc]init:self.header];
    pageHeader.delegate = self;
    GroupDrawable* pageFooter = [[GroupDrawable alloc]init:self.footer];
    pageFooter.delegate = self;
    
    self.data[@"CURRENT_PAGE"]=[NSNumber numberWithInt:[self.data[@"CURRENT_PAGE"] intValue]+1];
    
    // page 去除页眉，页脚后的区域
    NSRange range = NSMakeRange(self.drawable_rect.origin.y+pageHeader.rect.size.height, self.drawable_rect.origin.y+self.drawable_rect.size.height-pageFooter.rect.size.height);
    
    
    [pageHeader Draw:context dataSource:self.data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
    
    
    
    
    
    [pageFooter Draw:context dataSource:self.data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
    
    
}

#pragma mark - commoneditor delegate
-(void) outofPage:(CGContextRef) context
{
    [self nextPage:context];
}
-(void) addPageCount:(CGContextRef)context
{
    self.pagecount++;
}
@end
