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

#import "IFXPDFArcsPiesSample.h"

@implementation IFXPDFArcsPiesSample

+ (IFXPDFDocument*) run
{
    IFXPDFDocument* pdfDocument = [[IFXPDFDocument alloc] init];
    IFXPDFPage* page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];
    
    IFXPDFBrush* blackBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor blackColor]];
    IFXPDFPen* blackPen = [IFXPDFPen penWithColor:[IFXPDFRgbColor blackColor] andWidth:1];
    IFXPDFPen* darkGreenPen = [IFXPDFPen penWithColor:[IFXPDFRgbColor darkGreenColor] andWidth:3];
    
    IFXPDFFont* titleFont = [[IFXPDFFont alloc] init];
    titleFont.fontFace = IFXPDFHelveticaFontFace;
    titleFont.size = 16;
    IFXPDFFont* sectionFont = [[IFXPDFFont alloc] init];
    sectionFont.fontFace = IFXPDFHelveticaFontFace;
    sectionFont.size = 10;
    
    [page.graphics drawText:@"Arcs" withFont:titleFont brush:blackBrush atX:20 y:50];
    [page.graphics drawText:@"Pies" withFont:titleFont brush:blackBrush atX:310 y:50];
    
    [page.graphics drawLineWithPen:blackPen fromX1:20 y1:210 toX2:300 y2:210];
    [page.graphics drawLineWithPen:blackPen fromX1:160 y1:70 toX2:160 y2:350];
    [page.graphics drawLineWithPen:blackPen fromX1:310 y1:210 toX2:590 y2:210];
    [page.graphics drawLineWithPen:blackPen fromX1:450 y1:70 toX2:450 y2:350];
    blackPen.dashPattern = @"2 2";
    [page.graphics drawLineWithPen:blackPen fromX1:20 y1:70 toX2:300 y2:350];
    [page.graphics drawLineWithPen:blackPen fromX1:20 y1:350 toX2:300 y2:70];
    [page.graphics drawLineWithPen:blackPen fromX1:310 y1:70 toX2:590 y2:350];
    [page.graphics drawLineWithPen:blackPen fromX1:310 y1:350 toX2:590 y2:70];
    
    [page.graphics drawArcWithPen:darkGreenPen atX:30 y:80 withWidth:260 height:260 startAngle:0 sweepAngle:135];
    [page.graphics drawPieWithPen:darkGreenPen atX:320 y:80 withWidth:260 height:260 startAngle:45 sweepAngle:270];
    
    [page.graphics drawText:@"Random arcs and pies" withFont:sectionFont brush:blackBrush atX:20 y:385];
    IFXPDFPath* clipPath = [[IFXPDFPath alloc] init];
    [clipPath rectangleAtX:20 y:400 withWidth:570 height:300 rotation:0];
    
    [page.graphics saveGraphicsState];
    [page.graphics setClippingPath:clipPath];
    
    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 = 20 + rand() % 570;
        double y = 400 + rand() % 300;
        double width = rand() % 570;
        double height = rand() % 300;
        double startAngle = rand() % 360;
        double sweepAngle = rand() % 360;
        
        int mode = rand() % 4;
        
        switch (mode) {
            case 0:
                [page.graphics drawArcWithPen:randomPen atX:x y:y
                                    withWidth:width height:height startAngle:startAngle sweepAngle:sweepAngle];
                break;
            case 1:
                [page.graphics drawPieWithPen:randomPen atX:x y:y
                                    withWidth:width height:height startAngle:startAngle sweepAngle:sweepAngle];
                break;
            case 2:
                [page.graphics drawPieWithBrush:randomBrush atX:x y:y
                                      withWidth:width height:height startAngle:startAngle sweepAngle:sweepAngle];
            case 3:
                [page.graphics drawPieWithPen:randomPen andBrush:randomBrush atX:x y:y
                                    withWidth:width height:height startAngle:startAngle sweepAngle:sweepAngle];
                break;
        }
    }
    
    [page.graphics restoreGraphicsState];
    
    return pdfDocument;
}

@end