/*
 * AppController.j
 * Tutorial-CPCookie
 *
 * Created by __Me__ on __Date__.
 * Copyright 2008 __MyCompanyName__. All rights reserved.
 */

@import <Foundation/CPObject.j>

@import "CPCookie_Additions.j"

@implementation AppController : CPObject
{
}

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

	var button = [[CPButton alloc] initWithFrame:CGRectMake(10, 10, 200, 24)];
    [button setTitle:@"Add Cookies"];
    [button setAction:@selector(addCookies:)];
    [button setTarget:self];
    [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [contentView addSubview:button];

	button = [[CPButton alloc] initWithFrame:CGRectMake(10, 40, 200, 24)];
    [button setTitle:@"Read Cookies"];
    [button setAction:@selector(readCookies:)];
    [button setTarget:self];
    [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [contentView addSubview:button];

	button = [[CPButton alloc] initWithFrame:CGRectMake(10, 70, 200, 24)];
    [button setTitle:@"Remove Cookies"];
    [button setAction:@selector(removeCookies:)];
    [button setTarget:self];
    [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [contentView addSubview:button];

	button = [[CPButton alloc] initWithFrame:CGRectMake(300, 10, 200, 24)];
    [button setTitle:@"Set Arguments"];
    [button setAction:@selector(setArguments:)];
    [button setTarget:self];
    [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [contentView addSubview:button];

	button = [[CPButton alloc] initWithFrame:CGRectMake(300, 40, 200, 24)];
    [button setTitle:@"Get Arguments"];
    [button setAction:@selector(getArguments:)];
    [button setTarget:self];
    [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [contentView addSubview:button];

	button = [[CPButton alloc] initWithFrame:CGRectMake(300, 70, 200, 24)];
    [button setTitle:@"Get Named Arguments"];
    [button setAction:@selector(getNamedArguments:)];
    [button setTarget:self];
    [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [contentView addSubview:button];


    [theWindow orderFront:self];
}

-(void)addCookies:(id)sender
{
	debugger;
	var cookie1 = [[CPCookie alloc] initWithName:@"key1"];
	// 2 hours in seconds
	var date = [CPDate dateWithTimeIntervalSinceNow:2*60*60];
	[cookie1 setValue:@"value1" expires:date domain:@"nice-panorama.com"];
	
	var cookie2 = [CPCookie cookieWithName:@"key2" andValue:@"value2" forDays:2 inDomain:@"nice-panorama.com"];

	// Default domain (the one for the html page hosting the cappuccino application)
	// Default path "/" : the cookie will be accessible for the whole domain
	var cookie3 = [CPCookie cookieWithName:@"key3" andValue:@"value3" forDays:2];
}

-(void)readCookies:(id)sender
{
	debugger;
	console.log("readCokies");
	
	var cookie1 = [CPCookie cookieWithName:@"key1"];
	console.log([cookie1 value]);
	
	var cookie2 = [CPCookie cookieWithName:@"key2"];
	console.log([cookie2 value]);

	var cookie3 = [CPCookie cookieWithName:@"key3"];
	console.log([cookie3 value]);	
}

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

	[CPCookie removeCookieWithName:@"key1" inDomain:@"nice-panorama.com"];
	[CPCookie removeCookieWithName:@"key2" inDomain:@"nice-panorama.com"];

	[CPCookie removeCookieWithName:@"key3"];

	[self readCookies:self];

//  Empty the cookie value (the cookies will be removed only from the browser when we quit the browser)	
//	[CPCookie cookieWithName:@"key1" andValue:@"" forDays:0 inDomain:@"nice-panorama.com"];
//	[CPCookie cookieWithName:@"key2" andValue:@"" forDays:0 inDomain:@"nice-panorama.com"];

//	[self readCookies:self];
}


// https://developer.mozilla.org/en/window.location
// hash: the part of the URL that follows the # symbol, including the # symbol.
// search: the part of the URL that follows the ? symbol, including the ? symbol.	
//
// window.location.hash
// window.location.search
// index.html?test=1&cool=YES&cool=NO#param1%3Dvalue1/param2/param3%3DCappuccino%20%26%20Objective-J

// #debug
//
// if([args containsObject:"debug"])
//    CPLogRegister(CPLogPopup);


-(void)getNamedArguments:(id)sender
{
	var sharedApplication = [CPApplication sharedApplication];
	
	debugger;
	
	// index.html?test=1&cool=YES&cool=NO
	//	
	// keys : "test" "cool"
	// values : "1", "NO"
	var namedArguments = [sharedApplication namedArguments];
	
	var enumerator = [namedArguments keyEnumerator];
    var key;
	while ((key = [enumerator nextObject]) != nil)
	{
		var value = [namedArguments objectForKey:key];
		console.log(key + " = " + value);
	}
	
}

// index.html#param1%3Dvalue1/param2/param3%3DCappuccino%20%26%20Objective-J
//
-(void)setArguments:(id)sender
{
	var sharedApplication = [CPApplication sharedApplication];
	
	var args = [CPArray arrayWithObjects:@"param1=value1", @"param2", "param3=Cappuccino & Objective-J", nil];
	[sharedApplication setArguments:args];
}

-(void)getArguments:(id)sender
{
	var sharedApplication = [CPApplication sharedApplication];
	
	debugger;
	var args = [sharedApplication arguments];
	
	var enumerator = [args objectEnumerator];
	var arg;
	while ((arg = [enumerator nextObject]) != nil)
	{
		console.log(arg);
	}
	
}



@end
