/*
 * AppController.j
 *
 * Created by Philippe Laval on 2009/03/06.
 * Copyright 2009 Philippe Laval. All rights reserved.
 */

@import <Foundation/CPObject.j>

@import "Person.j"

@implementation AppController : CPObject
{
}

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

	var button = [[CPButton alloc] initWithFrame: CGRectMake(
					CGRectGetWidth([contentView bounds])/2.0 - 100,
					CGRectGetHeight([contentView bounds])/2.0 - 9,
					200, 24
				 )];
							  
	[button setAutoresizingMask:CPViewMinXMargin | 
								CPViewMaxXMargin | 
								CPViewMinYMargin | 
								CPViewMaxYMargin];
	
	[button setTitle:"Play with array"];
	
	[button setTarget:self];
	[button setAction:@selector(playWithArray:)];                
				  
	[contentView addSubview:button];

    [theWindow orderFront:self];

    // Uncomment the following line to turn on the standard menu bar.
    // [CPMenu setMenuBarVisible:YES];
}

- (void)playWithArray:(id)sender
{
	debugger;

	// Playing with a Person class
	
	// Creating an object
	var john = [[Person alloc] initWithName:@"dummy"]; 
	
	// Creating some CPString object and using methods
	var name = [[CPString alloc] initWithString:@"Jo"];
	name = [[name stringByAppendingFormat:@"%@", @"hn"] uppercaseString];
	[john setName:name]; 
	[john setJobTitle:@"Engineer" company:@"Best sofware Inc."];

	var joe = [Person personWithName:"Joe"]; 
	[joe setJobTitle:@"Funder" company:@"Even better sofware Co."];
	
	// Use our description method
	CPLog.trace([joe description]);
	
	var keys = [CPArray arrayWithObjects:@"key1", @"key2", @"John", @"Joe", nil];
	var objects = [CPArray arrayWithObjects:@"value1", [CPNumber numberWithBool:NO], john, joe, nil];
	var dict = [CPDictionary dictionaryWithObjects:objects forKeys:keys];
	
	[dict setObject:[CPNumber numberWithDouble:9.9] forKey:@"key3"];
	[dict removeObjectForKey:@"key1"];

	CPLog.trace("dict : " + [dict description]);

	var myNull = [CPNull null];
	var data = [CPData dataWithString:@"Cappuccino"]; 	 
	var date = [CPDate date];
	var number = [CPNumber numberWithBool:YES];
	var array = [CPArray arrayWithObjects:@"string1", @"string2", myNull, date, number, data, dict, nil];
	
	var mutableArray = [CPArray arrayWithCapacity:10];
	[mutableArray addObjectsFromArray:array];
	[mutableArray addObject:@"new one"];

	CPLog.trace("dict : " + [mutableArray description]);

	CPLog.trace("end");

	return;
	
	
	var keys = [CPArray arrayWithObjects:@"key1", @"key2", @"key3", @"key4", nil];
	var objects = [CPArray arrayWithObjects:@"value1", 
						[CPNumber numberWithBool:NO], 
						[CPNumber numberWithDouble:9.9], 
						[CPNumber numberWithDouble:8.8], 
						nil];
	var dict = [CPDictionary dictionaryWithObjects:objects forKeys:keys];

	var array = [CPArray arrayWithObjects:@"value1", [CPNumber numberWithBool:NO], 
							[CPDate date], [CPData dataWithString:@"Cappuccino"], nil];

	
	var result = CPJSObjectCreateJSON(array);
	CPLog.trace(result);

	
	//var result = CPJSObjectCreateWithJSON(data);

}


@end
