/*
 * AppController.j
 * LoginTutorial
 *
 * Created by Philippe Laval on 2009/03/26.
 * Copyright 2008 __MyCompanyName__. All rights reserved.
 */

@import <Foundation/CPObject.j>

@import "LoginController.j"

@import "CPString_Base64.j"
@import "CPString_JSON.j"

@implementation AppController : CPObject
{
	CPURLConnection postConnection;
    CPData receivedJsonData;
}

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

    var button = [[CPButton alloc] initWithFrame:CGRectMake(10, 10, 200, 18)];
    [button setTitle:@"Show Login"];
    [button setAction:@selector(showLogin:)];
    [button setTarget:self];
    [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [contentView addSubview:button];

    button = [[CPButton alloc] initWithFrame:CGRectMake(10, 40, 200, 18)];
    [button setTitle:@"Hide Login"];
    [button setAction:@selector(hideLogin:)];
    [button setTarget:self];
    [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [contentView addSubview:button];


	button = [[CPButton alloc] initWithFrame: CGRectMake(20, 140, 100, 18)];
	[button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
	[button setTitle:"GET JSON"];
	[button setTarget:self];
	[button setAction:@selector(getJSON:)];                
	[contentView addSubview:button];

	button = [[CPButton alloc] initWithFrame: CGRectMake(20, 160, 100, 18)];
	[button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
	[button setTitle:"POST JSON"];
	[button setTarget:self];
	[button setAction:@selector(postJSON:)];                
	[contentView addSubview:button];

	button = [[CPButton alloc] initWithFrame: CGRectMake(20, 180, 100, 18)];
	[button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
	[button setTitle:"Base64"];
	[button setTarget:self];
	[button setAction:@selector(playWithBase64:)];                
	[contentView addSubview:button];

    [theWindow orderFront:self];
}

-(void)showLogin:(id)sender
{
	[[LoginController sharedLoginController] showLoginWindow:sender];
}

-(void)hideLogin:(id)sender
{
	[[LoginController sharedLoginController] hideLoginWindow:sender];
}


-(void)getJSON:(id)sender
{
	var request = [[CPURLRequest alloc] initWithURL:@"http://macbookpro.local/~philippe/get_json.php"]; 
	[request setHTTPMethod:@"GET"]; 
	
	var urlConnection = [CPURLConnection connectionWithRequest:request delegate:self]; 
}

-(void)postJSON:(id)sender
{ 
	// debugger;

	var request = [self createRequestWithIdentifier:YES];
	postConnection = [CPURLConnection connectionWithRequest:request delegate:self]; 
}

-(CPURLRequest)createRequestWithIdentifier:(BOOL)useIndentifier
{
	// Create a javascript object from scratch
	var myFirstJSONObject = { "action" : "Login", "user" : "John", "password" : "Doe"};
      
	// Get the JSON representation of the object
	var content = [CPString JSONFromObject:myFirstJSONObject];
	var contentLength = [[CPString alloc] initWithFormat:@"%d", [content length]];

	var request = [[CPURLRequest alloc] initWithURL:@"http://macbookpro.local/~philippe/Private/post_json.php"]; 
	[request setHTTPMethod:@"POST"]; 
	[request setHTTPBody:content]; 
	[request setValue:contentLength forHTTPHeaderField:@"Content-Length"]; 
	[request setValue:"text/plain;charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; 
	if (useIndentifier)
	{
		var user = @"Aladdin";
		var password = @"open sesame";
		var secret = [[CPString stringWithFormat:@"%@:%@", user, password] encodeBase64];
		var authorization = [CPString stringWithFormat:@"Basic %@", secret];
	
		[request setValue:authorization forHTTPHeaderField:@"Authorization"]; 
	}
	
	return request;
} 


-(void)connection:(CPURLConnection)connection didFailWithError:(id)error
{
	//debugger;
	receivedJsonData = nil;
	// alert(error);
}


-(void)connection:(CPURLConnection)connection didReceiveResponse:(CPHTTPURLResponse)response
{
	//debugger;
	var statusCode = [response statusCode];
}

-(void)connection:(CPURLConnection)connection didReceiveData:(CPString)data
{
	// debugger;
	
	// Create a javascript object from the JSON content 
	receivedJsonData = [data objectFromJSON];
}

-(void)connectionDidFinishLoading:(CPURLConnection)connection
{
	//debugger;
}

-(void)connectionDidReceiveAuthenticationChallenge:(CPURLConnection)connection
{
	// debugger;

	// Stop the previous connection
	[connection cancel];

	// Start a new connection with basic authentification
	var request = [self createRequestWithIdentifier:YES];
	postConnection = [CPURLConnection connectionWithRequest:request delegate:self]; 
}


-(void)playWithBase64:(id)sender
{
	var base64TestStrings = [
		[@"leasure.", @"bGVhc3VyZS4="],
		[@"easure.", @"ZWFzdXJlLg=="],
		[@"asure.", @"YXN1cmUu"],
		[@"sure.", @"c3VyZS4="]
	];
	
	var result;
	
	// debugger;
	
	for (var i = 0; i < base64TestStrings.length; i++)
	{
		result = [base64TestStrings[i][0] encodeBase64];
	}

	for (var i = 0; i < base64TestStrings.length; i++)
	{
		result = [base64TestStrings[i][1] decodeBase64];
	}
	
}

@end
