/*
 * PLToolPaletteController.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/CPWindowController.j>

@import "PLCircle.j"
@import "PLLine.j"
@import "PLRectangle.j"
@import "PLText.j"

@import "ImageCell.j"


PLArrowToolRow = 0;
PLRectToolRow = 1;
PLCircleToolRow = 2;
PLLineToolRow = 3;
PLTextToolRow = 4;

PLSelectedToolDidChangeNotification = @"PLSelectedToolDidChange";

var sharedToolPaletteController = nil;


@implementation PLToolPaletteController : CPWindowController
{
	CPCollectionView toolsCollectionView;
}

+ (id)sharedToolPaletteController 
{
    if (!sharedToolPaletteController) 
	{
        sharedToolPaletteController = [[PLToolPaletteController alloc] init];
    }

    return sharedToolPaletteController;
}

- (id)init
{
	var theWindow = [[CPPanel alloc] initWithContentRect:CGRectMake(100, 100, 40, 160) styleMask:/*CPTitledWindowMask |*/ CPClosableWindowMask];
    self = [super initWithWindow:theWindow];
    if (self)
    {
		[theWindow setTitle:@"Tools"];
		[theWindow setLevel:CPFloatingWindowLevel];

		var contentView = [theWindow contentView];
	    var bounds = [contentView bounds];
	
	    var toolCollectionViewItem = [[CPCollectionViewItem alloc] init];
	    [toolCollectionViewItem setView:[[ImageCell alloc] initWithFrame:CGRectMake(0, 0, 36, 36)]];

	    toolsCollectionView = [[CPCollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
		[toolsCollectionView setMaxNumberOfColumns:1];
		[toolsCollectionView setMaxNumberOfRows:4];
		[toolsCollectionView setVerticalMargin:0.0];
	    [toolsCollectionView setDelegate:self];
	    [toolsCollectionView setItemPrototype:toolCollectionViewItem];
	    [toolsCollectionView setMinItemSize:CGSizeMake(36, 36)];
	    [toolsCollectionView setMaxItemSize:CGSizeMake(48, 48)];
	    [toolsCollectionView setAutoresizingMask:CPViewHeightSizable | CPViewWidthSizable];
		[toolsCollectionView setAllowsMultipleSelection:NO];

		// Associate the content array with the collection view
		[toolsCollectionView setContent:[self imagePathArray]];

	    [contentView addSubview:toolsCollectionView];    
    }
    
    return self;
}


-(CPArray) imagePathArray
{
	var mainBundle = [CPBundle mainBundle];
	var path1 = [mainBundle pathForResource:@"Arrow.png"];
	var path2 = [mainBundle pathForResource:@"Rectangle.png"];
	var path3 = [mainBundle pathForResource:@"Circle.png"];
	var path4 = [mainBundle pathForResource:@"Line.png"];
	var path5 = [mainBundle pathForResource:@"TextGraphic.png"];
	// Pas de texte pour le moment
 	return [[CPArray alloc] initWithObjects:path1, path2, path3, path4, nil];
}


- (IBAction)selectToolAction:(id)sender
{
    [[CPNotificationCenter defaultCenter] postNotificationName:PLSelectedToolDidChangeNotification object:self];
}

- (Class)currentGraphicClass
{
	var row = [[toolsCollectionView selectionIndexes] firstIndex];
    var theClass = nil;

	// debugger;
	
    if (row == PLRectToolRow) {
        theClass = [PLRectangle class];
    } 
	else if (row == PLCircleToolRow) {
        theClass = [PLCircle class];
    } 
	else if (row == PLLineToolRow) {
        theClass = [PLLine class];
    } 
	else if (row == PLTextToolRow) {
//        theClass = [PLText class];
	}

    return theClass;
}

- (void)selectArrowTool {
//    [toolButtons selectCellAtRow:PLArrowToolRow column:0];
    [[CPNotificationCenter defaultCenter] postNotificationName:PLSelectedToolDidChangeNotification object:self];
}



@end