//
//  pdfCreator.m
//  iSales-NPD
//
//  Created by Ray on 10/21/16.
//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
//

#import "pdfCreator.h"
#import "UIKit/UIDocument.h"

#import "PDFPage.h"
@implementation pdfCreator


+(void) drawPage:(CGContextRef)context template:(NSMutableDictionary*) page size:(CGRect) rect dataSource:(NSMutableDictionary*)data
{
    
    PDFPage* pdfPage = [[PDFPage alloc] init:page size:rect];
    
    [pdfPage Draw:context dataSource:data];
    
    //NSMutableDictionary* header=page[@"header"] ;
    //    double margin_left=[page[@"margin_left"] doubleValue];
    //    double margin_left=[page[@"margin_left"] doubleValue];
}
+(int) QueryPageCount:(CGContextRef)context template:(NSMutableDictionary*) page size:(CGRect) rect dataSource:(NSMutableDictionary*)data
{
    
    PDFPage* pdfPage = [[PDFPage alloc] init:page size:rect];
    
    return [pdfPage QueryPageCount:context dataSource:data];
    
    //NSMutableDictionary* header=page[@"header"] ;
    //    double margin_left=[page[@"margin_left"] doubleValue];
    //    double margin_left=[page[@"margin_left"] doubleValue];
}

+(int)QueryTotalPage:(CGContextRef)pdfContext template:(NSMutableDictionary*) template dataSource:(NSMutableDictionary*)data
{
    
    int count=0;
    //获取路径
    
//    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
//    
//    NSString *saveDirectory=[paths objectAtIndex:0];
//    
//    NSString *saveFileName=@"myPDF.pdf";
//    
//    NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
//    
//    const char *filename=[newFilePath UTF8String];
//    
//    DebugLog(@"%@",newFilePath);
    //设置页面大小 Letter纸
    
    CGRect papersize=CGRectMake(0, 0, [template[@"paper"][@"width"] intValue], [template[@"paper"][@"height"] intValue]);
    
    //关联上下文的对象
    
//    CGContextRef pdfContext;
    
//    CFStringRef path;
//    
//    CFURLRef url;
//    
//    path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
//    
//    url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
//    
//    
//    pdfContext=CGPDFContextCreateWithURL(url, &papersize, nil);
//    
    
    NSMutableDictionary* pages=template[@"pages"];
    for(int i=0;i<[pages[@"count"] intValue];i++)
    {
        NSMutableDictionary* page=pages[[ NSString stringWithFormat:@"page_%d",i ] ];
        
        count+=[self QueryPageCount:pdfContext template:page size:papersize dataSource:data];
    }
    //开始画pdf
    
    //    NSString *temtext=[[NSString alloc]init];
    //
    //    const char *text=(char *)[temtext UTF8String];
    //
    //    int  width;
    //
    //    int height;
    //
    
    //    [self newpage:pdfContext size:papersize];
    
    
    
//    CGContextRelease(pdfContext);
    return count;
    
}


+(NSString*)CreatePdf:(NSMutableDictionary*) template dataSource:(NSMutableDictionary*)data
{
    
    

    //获取路径
    
    NSString* tempDir = NSTemporaryDirectory();
    
    NSString *saveFileName=[NSString stringWithFormat:@"%@.pdf",[[NSUUID UUID ] UUIDString] ] ;
    
    NSString *newFilePath=[tempDir stringByAppendingPathComponent:saveFileName];
    
    const char *filename=[newFilePath UTF8String];
    
    DebugLog(@"%@",newFilePath);
    //设置页面大小 Letter纸
    
    CGRect papersize=CGRectMake(0, 0, [template[@"paper"][@"width"] intValue], [template[@"paper"][@"height"] intValue]);
    
    //关联上下文的对象
    
    CGContextRef pdfContext;
    
    CFStringRef path;
    
    CFURLRef url;
    
    path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
    
    url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
    
    CFBridgingRelease(path);
    pdfContext=CGPDFContextCreateWithURL(url, &papersize, nil);
    
    CFBridgingRelease(url);
    data[@"TOTAL_PAGE"]= [NSNumber numberWithInt:[self QueryTotalPage:pdfContext template:template dataSource:data]];
    
    NSMutableDictionary* pages=template[@"pages"];
    for(int i=0;i<[pages[@"count"] intValue];i++)
    {
        NSMutableDictionary* page=pages[[ NSString stringWithFormat:@"page_%d",i ] ];
        
        data[@"CURRENT_PAGE"]=[NSNumber numberWithInt:[data[@"CURRENT_PAGE"] intValue]+1];
        
        [self drawPage:pdfContext template:page size:papersize dataSource:data];
    }
    //开始画pdf
    
    //    NSString *temtext=[[NSString alloc]init];
    //
    //    const char *text=(char *)[temtext UTF8String];
    //
    //    int  width;
    //
    //    int height;
    //
    
    //    [self newpage:pdfContext size:papersize];
    
    
    
    CGContextRelease(pdfContext);
    
//    NSURL* nsurl=CFBridgingRelease(url);
    
    return newFilePath;
    
}

@end
