//
// Created by Ross Boucher for FlickrPhotoDemo.
// Copyright 2005 - 2008, 280 North, Inc. All rights reserved.
//
// Philippe Laval (2009/03/19) : Simplify the code to just have a tutorial on CPToolbar and CPToolbarItem.
//

@import <Foundation/CPObject.j>

@import <AppKit/CPToolbar.j>
@import <AppKit/CPToolbarItem.j>


@import "PhotoResizeView.j"

var SliderToolbarItemIdentifier = "SliderToolbarItemIdentifier",
    AddToolbarItemIdentifier = "AddToolbarItemIdentifier",
    RemoveToolbarItemIdentifier = "RemoveToolbarItemIdentifier";

@implementation AppController : CPObject
{
    CPWindow theWindow;
}

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
    CPLogRegister(CPLogPopup);
    
    theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
    var contentView = [theWindow contentView];

	// Create the toolbar and set its delegate to be the AppController instance.
    var toolbar = [[CPToolbar alloc] initWithIdentifier:"Photos"];    
    [toolbar setDelegate:self];
	[toolbar setVisible:YES];
	// Associate the toolbar with the window
	[theWindow setToolbar:toolbar];

	// Add a label to see the slider value changes
    label = [[CPTextField alloc] initWithFrame:CGRectMake(0, 0, 400, 50)];
    [label setStringValue:@"Use the toolbar Luke!"];
    [label setAlignment:CPCenterTextAlignment];
    [label setFont:[CPFont systemFontOfSize:24.0]];
    [label setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [label setFrameOrigin:CGPointMake((CGRectGetWidth([contentView bounds]) - CGRectGetWidth([label frame])) / 2.0, (CGRectGetHeight([contentView bounds]) - CGRectGetHeight([label frame])) / 2.0)];

    [contentView addSubview:label];

    // Bring forward the window
    [theWindow orderFront:self];
}

- (void)add:(id)sender
{
    [label setStringValue:@"Add clicked"];
}

- (void)remove:(id)sender
{
    [label setStringValue:@"Remove clicked"];
}


- (void)adjustImageSize:(id)sender
{
	// debugger;
    // var newSize = [sender doubleValue];
    
    var newSizeAsString = [CPString stringWithFormat:@"%d", [[CPNumber numberWithDouble:[sender doubleValue]] intValue]];
    [label setStringValue:newSizeAsString];
}


// **************************** Toolbar delegate code ****************************

// Return an array of toolbar item identifier (all the toolbar items that may be present in the toolbar)
- (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
{
   return [CPToolbarFlexibleSpaceItemIdentifier, SliderToolbarItemIdentifier, AddToolbarItemIdentifier, RemoveToolbarItemIdentifier];
}

// Return an array of toolbar item identifier (the default toolbar items that are present in the toolbar)
- (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
{
   return [AddToolbarItemIdentifier, RemoveToolbarItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier, SliderToolbarItemIdentifier];
}

// Create the toolbar item that is requested by the toolbar.
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anItemIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag
{
	// Create the toolbar item and associate it with its identifier
    var toolbarItem = [[CPToolbarItem alloc] initWithItemIdentifier:anItemIdentifier];

    if (anItemIdentifier == SliderToolbarItemIdentifier)
    {
    	// The toolbar is using a custom view (of class PhotoResizeView)
        [toolbarItem setView:[[PhotoResizeView alloc] initWithFrame:CGRectMake(0, 0, 180, 50)]];
        
        // We don't associate a target/action with the toolbar item. 
        // This will be done in the slider contained in the PhotoResizeView.
        [toolbarItem setLabel:"Scale"];

        [toolbarItem setMinSize:CGSizeMake(180, 32)];
        [toolbarItem setMaxSize:CGSizeMake(180, 32)];
    }
    else if (anItemIdentifier == AddToolbarItemIdentifier)
    {
	    var mainBundle = [CPBundle mainBundle];
	    
        var image = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"add.png"] size:CPSizeMake(30, 25)];
        var highlighted = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"addHighlighted.png"] size:CPSizeMake(30, 25)];
            
        [toolbarItem setImage:image];
        [toolbarItem setAlternateImage:highlighted];
        
        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(add:)];
        [toolbarItem setLabel:"Add Photo List"];

        [toolbarItem setMinSize:CGSizeMake(32, 32)];
        [toolbarItem setMaxSize:CGSizeMake(32, 32)];
    }
    else if (anItemIdentifier == RemoveToolbarItemIdentifier)
    {        
    	var mainBundle = [CPBundle mainBundle];

        var image = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"remove.png"] size:CPSizeMake(30, 25)];
        var highlighted = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"removeHighlighted.png"] size:CPSizeMake(30, 25)];
            
        [toolbarItem setImage:image];
        [toolbarItem setAlternateImage:highlighted];

        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(remove:)];
        [toolbarItem setLabel:"Remove Photo List"];
        
        [toolbarItem setMinSize:CGSizeMake(32, 32)];
        [toolbarItem setMaxSize:CGSizeMake(32, 32)];
    }
    
    return toolbarItem;
}


@end

