package classes.matuto.model
{
	import classes.matuto.erro.Erro;
	import flash.net.URLVariables;
	import flash.text.TextField;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequestMethod;
	import flash.events.SecurityErrorEvent;

	public class Model extends Object
	{
		
		
		public static 	var 	NAME_PHP			:String;
		public static 	var 	GATEWAY				:String;
		protected static var    conexao				:NetConnection;
		
		
		
		
		public function Model()
		{
			
		}
		
		
		
		//==========================AMF=======================================//

		/**
		 * Método que inicia uma conexao com o AMFPHP
		 *
		 * @param	name O nome da classe do service do AMFPHP
		 * @param	gateway O gateway do AMFPHP
		 */
		
		public static function INIT_AMF(name:String,gateway:String):void
		{

			try{
		
				NAME_PHP = name;
				GATEWAY  = gateway;
				conexao  = new NetConnection();
				conexao.connect(GATEWAY);
				
			}catch(e:Error){
				
				Erro.add("ERRO NA CONECAO COM O AMFPHP");
				
			}
			
			
		}
		
		public static function AMF_CONNECT(name:String,complete:Function,erro:String="ERRO NA CONEXAO COM O BANCO",params:Object=null):void
		{
			
			try{
			
				conexao.call(NAME_PHP+"."+name,new Responder(complete,function():void{ Erro.add(erro) }),params);	
				
			}catch(e:Error)
			{
				
				Erro.add("NAO INICIALIZADO O INIT_AMF");
				
			}
			
		}
		
		
		//===========================================================================//
		

		//==============================XML=========================================//		
		
		
		public static function XML_CONNECT(url:String,complete:Function,erro:String="erro no xml"):void
		{
			
			var xml			:XML;
			var urlLoader	:URLLoader;
		
			urlLoader = new URLLoader();
			urlLoader.load(new URLRequest(url));
				
			urlLoader.addEventListener(Event.COMPLETE,function(e:Event):void{
			
				xml = new XML(e.target.data);
				complete(xml);
				
			});
			
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
				
				Erro.add(erro);
				
			});
		
			return void;
		
		}
		
		public static function populeXML(value:XML):Array
		{
		    var array:Array = new Array();
			
			for each (var dado:XML in value.dado) 
			array.push(dado);
			
			return array;
		}
		
		public static function pesquisaId(value:Array,id:int):*
		{	
			var retorno:*;
			
			for each (var dado:* in value) 
			{
			   if(dado.getId == id)
			   retorno = dado
			}
			
			return retorno;
		}
		
		

		//===========================================================================//
		//===========================================================================//

		
		//===========================================================================//
		// PHP
		//===========================================================================//
		
		public static function PHP_CONNECT(url:String,vars:URLVariables,call:Function=null,erro:Function=null):void
		{
			var loader 		:URLLoader    	= new URLLoader;
			var urlreq		:URLRequest 	= new URLRequest(url);
			loader.dataFormat 			  	= URLLoaderDataFormat.VARIABLES;
			
			urlreq.method 				  	= URLRequestMethod.POST;
			
			var urlvars		:URLVariables 	= vars;
			urlreq.data 				  	= urlvars;
		
			loader.addEventListener(Event.COMPLETE, call);
			
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(e:SecurityErrorEvent):void {
			
				erro();
				
			});
			
			loader.load(urlreq);			
		}
		
		
		//===========================================================================//
		//===========================================================================//
		
		
	}//end class
}