//
// LoginController.j
// Photo Display
//
// Created by Philippe Laval.
//

@import <Foundation/CPObject.j>

@import <AppKit/CPWindowController.j>



var loginControllerSharedInstance = nil;

@implementation LoginController : CPWindowController
{
	CPTextField userTextField;
	CPSecureTextField passwordTextField;
	CPSecureTextField confirmTextField;
	
	CPButton okButton;
	CPButton cancelButton;
	CPButton registerButton;
}

+ (LoginController)sharedLoginController
{
	// debugger;
	
    if (loginControllerSharedInstance == nil)
    {
        loginControllerSharedInstance = [[LoginController alloc] init];
    }
    
    return loginControllerSharedInstance;
}

-(void)showLoginWindow:(id)sender
{
    var sharedLoginController = [LoginController sharedLoginController];
    [sharedLoginController showWindow:sender];
}

-(void)hideLoginWindow:(id)sender
{
    var sharedLoginController = [LoginController sharedLoginController];
    [[sharedLoginController window] orderOut:sender];
}

-(void)toggleLoginWindow:(id)sender 
{
	// debugger;
    var sharedLoginController = [LoginController sharedLoginController];
    var loginWindow = [sharedLoginController window];
    
  	if ([loginWindow isVisible] == YES) {
    	[loginWindow orderOut:sender];
  	}
  	else {
    	[sharedLoginController showWindow:sender];
  	}
}

- (id)init
{
	// Create the login window
    var theWindow = [[CPPanel alloc] initWithContentRect:CGRectMake(100,100,380,240) styleMask:CPHUDBackgroundWindowMask | CPClosableWindowMask | CPTitledWindowMask];
    self = [super initWithWindow:theWindow];
    if (self)
    {
		[theWindow setTitle:@"Login"];
		[theWindow setLevel:CPFloatingWindowLevel];
        
        [theWindow setDelegate:self];

		var contentView = [theWindow contentView];
		
		
		var textField = [[CPTextField alloc] initWithFrame:CGRectMake(10, 10, 360, 24)];
		[textField setObjectValue:@"Enter your email and password to login/register."];
		[textField setFont:[CPFont systemFontOfSize:12]];
		[textField setTextColor:[CPColor whiteColor]];
		[textField setAlignment:CPCenterTextAlignment];
		[contentView addSubview:textField]; 

		textField = [[CPTextField alloc] initWithFrame:CGRectMake(10, 40, 70, 24)];
		[textField setObjectValue:@"email:"];
		[textField setFont:[CPFont systemFontOfSize:12]];
		[textField setTextColor:[CPColor whiteColor]];
		[textField setAlignment:CPRightTextAlignment];
		[contentView addSubview:textField]; 

		textField = [[CPTextField alloc] initWithFrame:CGRectMake(10, 70, 70, 24)];
		[textField setObjectValue:@"password:"];
		[textField setFont:[CPFont systemFontOfSize:12]];
		[textField setTextColor:[CPColor whiteColor]];
		[textField setAlignment:CPRightTextAlignment];
		[contentView addSubview:textField]; 

		textField = [[CPTextField alloc] initWithFrame:CGRectMake(10, 100, 70, 24)];
		[textField setObjectValue:@"confirm:"];
		[textField setFont:[CPFont systemFontOfSize:12]];
		[textField setTextColor:[CPColor whiteColor]];
		[textField setAlignment:CPRightTextAlignment];
		[contentView addSubview:textField]; 

/*
		textField = [[CPTextField alloc] initWithFrame:CGRectMake(10, 120, 300, 24)];
		[textField setObjectValue:@"Password troubles ?"];
		[textField setFont:[CPFont systemFontOfSize:12]];
		[textField setTextColor:[CPColor whiteColor]];
		[textField setAlignment:CPCenterTextAlignment];
		[contentView addSubview:textField]; 
*/

		// Add the fields
		userTextField = [[CPTextField alloc] initWithFrame:CGRectMake(100, 40, 250, 24)];
		[userTextField setPlaceholderString:@"user"];
		[userTextField setObjectValue:@"cool"];
		[userTextField setEditable:YES];
		[userTextField setSelectable:YES];
		// [userTextField setBordered:YES];
		[userTextField setDrawsBackground:YES];
		[userTextField setTextFieldBackgroundColor:[CPColor whiteColor]];
		[contentView addSubview:userTextField]; 
		
		passwordTextField = [[CPSecureTextField alloc] initWithFrame:CGRectMake(100, 70, 250, 24)];
		[passwordTextField setFont:[CPFont systemFontOfSize:12]];
		[contentView addSubview:passwordTextField]; 

		confirmTextField = [[CPSecureTextField alloc] initWithFrame:CGRectMake(100, 100, 250, 24)];
		[confirmTextField setFont:[CPFont systemFontOfSize:12]];
		[contentView addSubview:confirmTextField]; 
		
		okButton = [[CPButton alloc] initWithFrame:CGRectMake(270, 160, 80, 18)];
		[okButton setTitle:"Login"];
	    [okButton setAction:@selector(login:)];
	    [okButton setTarget:self];
		[contentView addSubview:okButton]; 
		
		cancelButton = [[CPButton alloc] initWithFrame:CGRectMake(180, 160, 80, 18)];
	    [cancelButton setAction:@selector(cancel:)];
	    [cancelButton setTarget:self];
		[cancelButton setTitle:"Cancel"];
		[contentView addSubview:cancelButton]; 

		registerButton = [[CPButton alloc] initWithFrame:CGRectMake(10, 160, 80, 18)];
		[registerButton setTitle:"Register"];
	    [registerButton setAction:@selector(register:)];
	    [registerButton setTarget:self];
		[contentView addSubview:registerButton]; 
	}
	
	return self;
}

// http://www.w3schools.com/jS/js_form_validation.asp
// http://www.smartwebby.com/dhtml/email_validation.asp
// http://www.journaldunet.com/developpeur/ressource/javascript/js_form_datemail.shtml

-(void)login:(id)sender
{
	if ([self checkPassword])
	{
		if ([self checkPasswordLength:6])
		{
		
			[self hideLoginWindow:sender];

			// Empty the password
			// [passwordTextField setStringValue:@""];
			// [confirmTextField setStringValue:@""];
		}
		else
		{
			// Show an error
		}		
	}
	else
	{
		// Show an error
	}
}

-(void)cancel:(id)sender
{
	[self hideLoginWindow:sender];
}

-(void)register:(id)sender
{
	// Checks that the password are the same
	if ([self checkPassword])
	{
		if ([self checkPasswordLength:6])
		{
			[self hideLoginWindow:sender];
		
			// Empty the password
			// [passwordTextField setStringValue:@""];
			// [confirmTextField setStringValue:@""];
		}
		else
		{
			// Show an error
			alert([CPString stringWithFormat:"Password must have more than %d characters", 6]);
		}
	}
	else
	{
		// Show an error
		alert("Password are different")
	}
}

-(BOOL)checkEmail
{
	var email = [userTextField stringValue];
	if ([password length] > 0)
	{
		var re_email = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/ ;
		
		return re_email.test(email);
	}
	
	return NO;
}


-(BOOL)checkPasswordLength:(int)length
{
	var password = [passwordTextField stringValue];
	
	return ([password length] >= length);
}

-(BOOL)checkPassword
{
	var password = [passwordTextField stringValue];
	var confirm = [confirmTextField stringValue];
	
	if ([password compare:confirm] == CPOrderedSame)
		return YES;
		
	return NO;
}


- (void)windowWillClose:(id)aSender
{
	// Update the menu state
}

@end
