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

#import "IFXPDFOptionalContentSample.h"

@implementation IFXPDFOptionalContentSample

+ (IFXPDFDocument*) run
{
    IFXPDFDocument* pdfDocument = [[IFXPDFDocument alloc] init];
    pdfDocument.optionalContentProperties = [IFXPDFOptionalContentProperties optionalContentProperties];

    IFXPDFBrush* blackBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor blackColor]];
    IFXPDFBrush* blueBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor darkBlueColor]];
    IFXPDFBrush* redBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor darkRedColor]];
    
    IFXPDFFont* titleFont = [[IFXPDFFont alloc] init];
    titleFont.fontFace = IFXPDFHelveticaBoldFontFace;
    titleFont.size = 16;
    IFXPDFFont* sectionFont = [[IFXPDFFont alloc] init];
    sectionFont.fontFace = IFXPDFHelveticaFontFace;
    sectionFont.size = 10;

    IFXPDFPage* page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];

    [page.graphics drawText:@"Optional Content" withFont:titleFont brush:blackBrush atX:20 y:50];
    [page.graphics drawText:@"Simple optional content - blue rectangle" withFont:sectionFont brush:blackBrush atX:20 y:70];
    
    IFXPDFOptionalContentGroup* ocgPage1 = [[IFXPDFOptionalContentGroup alloc] init];
    ocgPage1.name = @"Page 1 - Blue Rectangle";
    [page.graphics beginOptionalContentGroup:ocgPage1];
    [page.graphics drawRectangleWithBrush:blueBrush atX:20 y:100 withWidth:570 height:400];
    [page.graphics endOptionalContentGroup];
    
    page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];
    
    [page.graphics drawText:@"Multipart optional content - blue rectangles" withFont:sectionFont brush:blackBrush atX:20 y:70];
    
    IFXPDFOptionalContentGroup* ocgPage2 = [[IFXPDFOptionalContentGroup alloc] init];
    ocgPage2.name = @"Page 2 - Blue Rectangles";
    [page.graphics beginOptionalContentGroup:ocgPage2];
    [page.graphics drawRectangleWithBrush:blueBrush atX:20 y:200 withWidth:570 height:200];
    [page.graphics endOptionalContentGroup];
    
    [page.graphics drawRectangleWithBrush:redBrush atX:250 y:90 withWidth:110 height:680];
    
    [page.graphics beginOptionalContentGroup:ocgPage2];
    [page.graphics drawRectangleWithBrush:blueBrush atX:20 y:500 withWidth:570 height:200];
    [page.graphics endOptionalContentGroup];
    
    page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];
    
    [page.graphics drawText:@"Nested optional content - blue rectangle and red rectangle" withFont:sectionFont brush:blackBrush atX:20 y:70];
    
    IFXPDFOptionalContentGroup* ocgPage31 = [[IFXPDFOptionalContentGroup alloc] init];
    ocgPage31.name = @"Page 3 - Blue Rectangle";
    [page.graphics beginOptionalContentGroup:ocgPage31];
    [page.graphics drawRectangleWithBrush:blueBrush atX:20 y:100 withWidth:570 height:600];
    
    IFXPDFOptionalContentGroup* ocgPage32 = [[IFXPDFOptionalContentGroup alloc] init];
    ocgPage32.name = @"Page 3 - Red Rectangle";
    [page.graphics beginOptionalContentGroup:ocgPage32];
    [page.graphics drawRectangleWithBrush:redBrush atX:100 y:200 withWidth:400 height:300];
    [page.graphics endOptionalContentGroup]; // ocgPage32
    
    [page.graphics endOptionalContentGroup]; // ocgPage31
    
    page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];
    
    [page.graphics drawText:@"Multipage optional content - blue rectangles on pages 4 & 5" withFont:sectionFont brush:blackBrush atX:20 y:70];
    
    IFXPDFOptionalContentGroup* ocgPage45 = [[IFXPDFOptionalContentGroup alloc] init];
    ocgPage45.name = @"Page 4 & 5 - Blue Rectangles";
    [page.graphics beginOptionalContentGroup:ocgPage45];
    [page.graphics drawRectangleWithBrush:blueBrush atX:20 y:200 withWidth:570 height:200];
    [page.graphics endOptionalContentGroup];
    
    [page.graphics drawRectangleWithBrush:redBrush atX:250 y:90 withWidth:110 height:680];
    
    [page.graphics beginOptionalContentGroup:ocgPage45];
    [page.graphics drawRectangleWithBrush:blueBrush atX:20 y:500 withWidth:570 height:200];
    [page.graphics endOptionalContentGroup];
    
    page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];
    
    [page.graphics drawText:@"Multipage optional content - continued" withFont:sectionFont brush:blackBrush atX:20 y:70];
    
    [page.graphics beginOptionalContentGroup:ocgPage45];
    [page.graphics drawRectangleWithBrush:blueBrush atX:20 y:200 withWidth:570 height:200];
    [page.graphics endOptionalContentGroup];
    
    [page.graphics drawRectangleWithBrush:redBrush atX:250 y:90 withWidth:110 height:680];
    
    [page.graphics beginOptionalContentGroup:ocgPage45];
    [page.graphics drawRectangleWithBrush:blueBrush atX:20 y:500 withWidth:570 height:200];
    [page.graphics endOptionalContentGroup];
    
    // Build the visual tree for the optional content.
    // This is what the user sees in Adobe Reader.
    IFXPDFOptionalContentVisualTreeNode* ocgPage1Node = [[IFXPDFOptionalContentVisualTreeNode alloc] initWithOptionalContentGroup:ocgPage1];
    [pdfDocument.optionalContentProperties.visualTree.nodes addOptionalContentVisualTreeNode:ocgPage1Node];
    IFXPDFOptionalContentVisualTreeNode* ocgPage2Node = [[IFXPDFOptionalContentVisualTreeNode alloc] initWithOptionalContentGroup:ocgPage2];
    [pdfDocument.optionalContentProperties.visualTree.nodes addOptionalContentVisualTreeNode:ocgPage2Node];
    IFXPDFOptionalContentVisualTreeNode* ocgPage31Node = [[IFXPDFOptionalContentVisualTreeNode alloc] initWithOptionalContentGroup:ocgPage31];
    [pdfDocument.optionalContentProperties.visualTree.nodes addOptionalContentVisualTreeNode:ocgPage31Node];
    IFXPDFOptionalContentVisualTreeNode* ocgPage32Node = [[IFXPDFOptionalContentVisualTreeNode alloc] initWithOptionalContentGroup:ocgPage32];
    [ocgPage31Node.nodes addOptionalContentVisualTreeNode:ocgPage32Node];
    IFXPDFOptionalContentVisualTreeNode* ocgPage45Node = [[IFXPDFOptionalContentVisualTreeNode alloc] initWithOptionalContentGroup:ocgPage45];
    [pdfDocument.optionalContentProperties.visualTree.nodes addOptionalContentVisualTreeNode:ocgPage45Node];
    
    return pdfDocument;
}

@end