
import com.adobe.serialization.json.JSON;
import com.tp.helpers.Global;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;

import mx.controls.Alert;
import mx.core.Application;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;

private var callbackOnExit:Function;
private var callbackOnCancel:Function;


private function onCreationComplete():void
{
	trace("user login controller");
	addListeners();
	this.x = Application.application.width/2 - this.width/2;
	this.y = 0; 
}

private function addListeners():void {
	doLoginBtn.addEventListener(MouseEvent.CLICK, doLogin);
	doRegBtn.addEventListener(MouseEvent.CLICK, doReg);
	//doCloseBtn.addEventListener(MouseEvent.CLICK, doClose);
	createProfileBtn.addEventListener(MouseEvent.CLICK, changeCanvas);
	loginIntoBtn.addEventListener(MouseEvent.CLICK, changeCanvas);
	
	this.addEventListener(CloseEvent.CLOSE, doClose);
}

private function changeCanvas (e:MouseEvent = null):void{
	userLoginCanvas.visible = !userLoginCanvas.visible;
	userRegCanvas.visible = !userLoginCanvas.visible;
	if (userLoginCanvas.visible)
		this.title = 'Login';
	else
		this.title = 'Register';
	reglogin.text = regpassword.text = login.text = password.text = ''
}

private function doLogin(event:MouseEvent):void {
	var req:URLRequest = new URLRequest(Global.loginPath);
	var variables:URLVariables = new URLVariables();
	
	variables.login = login.text;
	variables.password = password.text;
	variables.action = 'login';
	
	req.method = URLRequestMethod.POST;
	req.data = variables;
	
	var urlLoader:URLLoader = new URLLoader();
	
	urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS , httpStatusHandler);
	urlLoader.addEventListener(IOErrorEvent.IO_ERROR ,ioErrorHandler);
	urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR , securityErrorHandler);
	urlLoader.addEventListener(Event.COMPLETE, onLoginComplete);
	urlLoader.load(req);
//	Alert.show(" login + password = "+ login.text + " "+ password.text);		
}
private function doLogout(event:MouseEvent):void
{
		
	
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
	trace ( "Load failed: Security Error: " + event.text );
}
private function ioErrorHandler(event:IOErrorEvent):void
{
	trace ( "Load failed: IO error: " + event.text );
}
private function httpStatusHandler(event:HTTPStatusEvent):void
{
	trace ( "Load failed: HTTP Status = " + event.status );
}
private function doReg(event:MouseEvent):void {
	var req:URLRequest = new URLRequest(Global.loginPath);
	var variables:URLVariables = new URLVariables();
	
	variables.login = reglogin.text;
	variables.password = regpassword.text;
	variables.action = 'register';
	
	req.method = URLRequestMethod.POST;
	req.data = variables;
	
	var urlLoader:URLLoader = new URLLoader();
	
	urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS , httpStatusHandler);
	urlLoader.addEventListener(IOErrorEvent.IO_ERROR ,ioErrorHandler);
	urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR , securityErrorHandler);
	urlLoader.addEventListener(Event.COMPLETE, onRegComplete);
	urlLoader.load(req);	
}

private function onRegComplete(event:Event):void {
	var res:Object = JSON.decode(URLLoader(event.target).data);
	if (res.result == 'success') {
		changeCanvas();
	} else if (res.result == 'error') {
		Alert.show(res.message, "Registration Error");
	} else {
		Alert.show("Unknown error occurred. Please contact with administrator", "Error");
	}
}

private function onLoginComplete(event:Event):void {
//	Alert.show("res.result = ");
	var res:Object = JSON.decode(URLLoader(event.target).data);
	if (res.result == 'success') {
		Global.userData = res.data;
		if (callbackOnExit != null) 
			callbackOnExit();
		PopUpManager.removePopUp(this);
		this.parentApplication.step1.changeLoginButtonToLogout();
	} else if (res.result == 'error') {
		Alert.show(res.message, "Login Error");
	} else {
		Alert.show("Unknown error occurred. Please contact with administrator", "Error");
	}
}

private function doClose(event:* = null):void {
	if (callbackOnCancel != null) 
		callbackOnCancel();
	PopUpManager.removePopUp(this);
}

public function setCallBack(callback:Function):void {
	callbackOnExit = callback;
}

public function setCancelCallBack(callback:Function):void {
	callbackOnCancel = callback;
}