//
//  IFXPDFFormXObjectsSample.m
//  IFXPDFFactory-iOS-SamplesBrowser
//
//  Created by Sorin Nistor on 11/11/13.
//  Copyright (c) 2013 IFXFactory. All rights reserved.
//

#import "IFXPDFFormXObjectsSample.h"

@implementation IFXPDFFormXObjectsSample

+ (IFXPDFDocument*) run
{
    IFXPDFDocument* pdfDocument = [[IFXPDFDocument alloc] init];
    IFXPDFPage* page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];
    
    IFXPDFBrush* blackBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor blackColor]];
    
    IFXPDFFont* titleFont = [[IFXPDFFont alloc] init];
    titleFont.fontFace = IFXPDFHelveticaBoldFontFace;
    titleFont.size = 16;
    IFXPDFFont* sectionFont = [[IFXPDFFont alloc] init];
    sectionFont.fontFace = IFXPDFHelveticaFontFace;
    sectionFont.size = 10;
    
    [page.graphics drawText:@"Form XObjects" withFont:titleFont brush:blackBrush atX:20 y:50];
    [page.graphics drawText:@"Scaling" withFont:sectionFont brush:blackBrush atX:20 y:70];
    
    IFXPDFFormXObject* xObject = [[IFXPDFFormXObject alloc] initWithWidth:300 andHeight:300];
    // Create the XObject content
    IFXPDFRgbColor* randomPenColor = [[IFXPDFRgbColor alloc] init];
    IFXPDFPen* randomPen = [IFXPDFPen penWithColor:randomPenColor andWidth:1];
    IFXPDFRgbColor* randomBrushColor = [[IFXPDFRgbColor alloc] init];
    IFXPDFBrush* randomBrush = [IFXPDFBrush brushWithColor:randomBrushColor];
    srand(time(NULL));
    for (int i = 0; i < 200; i++) {
        randomPenColor.red = (rand() % 256) / 255.0;
        randomPenColor.green = (rand() % 256) / 255.0;
        randomPenColor.blue = (rand() % 256) / 255.0;
        randomBrushColor.red = (rand() % 256) / 255.0;
        randomBrushColor.green = (rand() % 256) / 255.0;
        randomBrushColor.blue = (rand() % 256) / 255.0;
        
        double x = rand() % 300;
        double y = rand() % 300;
        double width = rand() % 300;
        double height = rand() % 300;
        double rotation = rand() % 360;
        
        int mode = rand() % 3;
        
        switch (mode) {
            case 0:
                [xObject.graphics drawRectangleWithPen:randomPen
                                                   atX:x y:y withWidth:width height:height rotation:rotation];
                break;
            case 1:
                [xObject.graphics drawRectangleWithBrush:randomBrush
                                                     atX:x y:y withWidth:width height:height rotation:rotation];
                break;
            case 2:
                [xObject.graphics drawRectangleWithPen:randomPen andBrush:randomBrush
                                                   atX:x y:y withWidth:width height:height rotation:rotation];
                break;
        }
    }
    
    // Draw the form XObject 3 times on the page at different sizes.
    [page.graphics drawFormXObject:xObject atX:3 y:90 withWidth:100 height:100];
    [page.graphics drawFormXObject:xObject atX:106 y:90 withWidth:200 height:200];
    [page.graphics drawFormXObject:xObject atX:309 y:90 withWidth:300 height:300];

    [page.graphics drawText:@"Flipping" withFont:sectionFont brush:blackBrush atX:20 y:420];
    [page.graphics drawFormXObject:xObject atX:20 y:440
                         withWidth:150 height:150 rotation:0 flip:IFXPDFNoContentFlip];
    [page.graphics drawFormXObject:xObject atX:200 y:440
                         withWidth:150 height:150 rotation:0 flip:IFXPDFVerticalContentFlip];
    [page.graphics drawFormXObject:xObject atX:20 y:620
                         withWidth:150 height:150 rotation:0 flip:IFXPDFHorizontalContentFlip];
    [page.graphics drawFormXObject:xObject atX:200 y:620
                         withWidth:150 height:150 rotation:0 flip:IFXPDFVerticalContentFlip | IFXPDFHorizontalContentFlip];

    return pdfDocument;
}

@end