/*
 * PLDocument.j
 * Tutorial-Drawing-part2
 *
 * Created by Philippe Laval on 2009/04/20.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

@import <Foundation/CPObject.j>

@import <AppKit/CPDocument.j>
@import <AppKit/CPWindowController.j>
@import <AppKit/CPWindow.j>

@import "PLWindowController.j"
@import "PLGraphic.j"
@import "PLRectangle.j"
@import "PLLine.j"
@import "PLCircle.j"

@implementation PLDocument : CPDocument
{
	CPArray _graphics;
	CPIndexSet _selectionIndexes;
	CPMutableSet _undoGroupInsertedGraphics;
	
	CPString _test;
}

- (id)init
{
    self = [super init];
    if (self)
    {
		_graphics = [CPArray array];
		_selectionIndexes = [CPIndexSet indexSet];
		
		_test = @"cappuccino";	
		
		var graphic = [[PLRectangle alloc] init];
		[graphic setXPosition:10.0];
		[graphic setYPosition:10.0];
		[graphic setWidth:100.0];
		[graphic setHeight:100.0];
		[graphic setIsDrawingFill:YES];
		[graphic setFillColor:[CPColor redColor]];
		[graphic setStrokeColor:[CPColor blueColor]];

		[_graphics addObject:graphic];

		graphic = [[PLCircle alloc] init];
		[graphic setXPosition:150.0];
		[graphic setYPosition:150.0];
		[graphic setWidth:100.0];
		[graphic setHeight:100.0];
		[graphic setIsDrawingFill:YES];
		[graphic setFillColor:[CPColor blueColor]];
		[graphic setStrokeColor:[CPColor redColor]];

		[_graphics addObject:graphic];

		graphic = [[PLLine alloc] init];
		[graphic setBeginPoint:CPPointMake(200.0, 10.0)];
		[graphic setEndPoint:CPPointMake(250.0, 110.0)];
		[graphic setStrokeColor:[CPColor greenColor]];

		[_graphics addObject:graphic];
		
		[_selectionIndexes addIndex:0];
		[_selectionIndexes addIndex:2];
    }
    
    return self;
}

- (void)makeWindowControllers
{
	// debugger;
    var controller = [[PLWindowController alloc] init];
    [self addWindowController:controller];
}


- (CPArray)graphics 
{    
    return _graphics;
}

- (CPIndexSet)selectionIndexes 
{
    return _selectionIndexes;
}

- (void)setSelectionIndexes:(CPIndexSet)selectionIndexes
{
	_selectionIndexes = selectionIndexes;
}


/*
- (void)insertGraphics:(CPArray)graphics atIndexes:(NSIndexSet)indexes 
{
    [_graphics insertObjects:graphics atIndexes:indexes];

    // Register an action that will undo the insertion.
    var undoManager = [self undoManager];
    [undoManager registerUndoWithTarget:self selector:@selector(removeGraphicsAtIndexes:) object:indexes];

    // Record the inserted graphics so we can filter out observer notifications from them. This way we don't waste memory registering undo operations for changes that wouldn't have any effect because the graphics are going to be removed anyway. In Sketch this makes a difference when you create a graphic and then drag the mouse to set its initial size right away. Why don't we do this if undo registration is disabled? Because we don't want to add to this set during document reading. (See what -readFromData:ofType:error: does with the undo manager.) That would ruin the undoability of the first graphic editing you do after reading a document.
    if ([undoManager isUndoRegistrationEnabled])
	{
		if (_undoGroupInsertedGraphics) {
		    [_undoGroupInsertedGraphics addObjectsFromArray:graphics];
		} else {
		    _undoGroupInsertedGraphics = [[CPMutableSet alloc] initWithArray:graphics];
		}
    }
}

- (void)removeGraphicsAtIndexes:(CPIndexSet)indexes
{
    var graphics = [_graphics objectsAtIndexes:indexes];
    
    // Register an action that will undo the removal. Do this before the actual removal so we don't have to worry about the releasing of the graphics that will be done.
    [[[self undoManager] prepareWithInvocationTarget:self] insertGraphics:graphics atIndexes:indexes];
    
    // Do the actual removal.
    [_graphics removeObjectsAtIndexes:indexes];
}


- (CPData)dataOfType:(CPString)aType error:({CPError})anError
{
	debugger;
	return [CPData dataWithString:_test];
}

- (void)readFromData:(CPData)aData ofType:(CPString)aType error:(CPError)anError
{
	debugger;
	_test = [aData string];
}
*/

@end