package classes.matuto.core.module
{
	import classes.matuto.core.Matuto;
	import classes.matuto.core.PreloaderDefaultBase;
	import classes.matuto.erro.Erro;
	import classes.matuto.utils.display.LibraryExternal;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.system.System;
	import flash.utils.getDefinitionByName;
	import flash.utils.setTimeout;
	
	
	public class ModuleBase extends Sprite
	{
		
		private var container				:MovieClip;
		private var moduleInfo				:ModuleInfo;
		private var modulo					:Module;
		
		private var c          	 			:Class;
		private var obj         			:DisplayObject;
		
		private var typeAnterior			:String;
		
		private var preloader				:PreloaderDefault;
		
		private var carregamento			:Boolean = false;
		
		private var modulesExternal			:Object;
		
		
		public function ModuleBase(value:MovieClip)
		{
			super();
			container = value;
			modulesExternal = new Object();
		}
		
		//========================================== INIT ======================================================//
		
		
		public function init(value:ModuleInfo):void
		{
		   moduleInfo = value;	
		   
		   INICIALIZE_MODULE();  
		}
		
		
		public function INICIALIZE_MODULE():void
		{
			if(moduleInfo.getType=="local")
			{
		        INICIALIZE_MODULE_LOCAL();	
			
			}else
			{	
			    INICIALIZE_MODULE_EXTERNAL();
			}	
		}
		
		public function INICIALIZE_MODULE_LOCAL():void
		{
			
			if(getDefinitionByName(moduleInfo.getClass))
			{
				
				c                          = getDefinitionByName(moduleInfo.getClass) as Class;
				obj            			   = new c() as DisplayObject;
				modulo					   = obj as Module;
				
			}
			
			try
			{
							
				container.addChild(modulo);
				modulo.init();	
				modulo.resize();

			}catch(e:Error){
				
				
				Erro.add("ERRO na init do module "+ moduleInfo.getTitle);
				
			}
	
			c 							= null;
			obj 						= null;
			
		}
		
		public function addExternal(id:String,CLASS:Class):void
		{
			modulesExternal[id] = CLASS;
		}
			
		public function INICIALIZE_MODULE_EXTERNAL():void
		{
			
			try{
			
					preloader = LibraryExternal.getLibrarySymbol('PreloaderDefault') as PreloaderDefault;
					preloader.load(moduleInfo.getId+".swf");
					
					preloader.resize();
					preloader.init();
					
					container.addChild(preloader);
		
					preloader.onComplete 	 = function():void
					{
						modulo = preloader.getSWF as Module;
						container.addChild(modulo);
						carregamento = true;
					}
					
					preloader.onCompleteRemove = function():void
					{
						preloader.dispose();
						container.removeChild(preloader);
						preloader = null;
					}
				
			}catch(e:Error)
			{
				
				Erro.add("erro no defaultPreloader ou no Module "+typeAnterior);	
			
			}
			
	
		}
		
		
		
		
		
		
		//=============================================================================================//
		
		
		
		
		//=========================================== CHANGE ==========================================//
		
		
		public function change(value:ModuleInfo):void
		{
			
			typeAnterior = moduleInfo.getType;
			moduleInfo 	 = null;			
			moduleInfo 	 = value;

			CHANGE_MODULE();

		
		}
		
		
		private function CHANGE_MODULE():void
		{
			
			if(typeAnterior=="local")
			CHANGE_MODULE_LOCAL();	
			else
			CHANGE_MODULE_EXTERNAL();
			
		}
		
		private function CHANGE_MODULE_LOCAL():void
		{
			try{
				
				modulo.close();				
				
			}catch(e:Error){
				
				Erro.add("ERRO no close do module "+ moduleInfo.getTitle);
				
			}
		}
		
		private function CHANGE_MODULE_EXTERNAL():void
		{
			
			try{
				
				if(carregamento)
				{
					modulo.close();		
					carregamento = false;	
				}
				else{
				
					
					preloader.disposeLocal();
					
					preloader.onClose = function():void{
					
						preloader.dispose();
						container.removeChild(preloader);
						preloader = null;
						
						Matuto.getModuleBase.dispose();
						Matuto.getModuleBase.INICIALIZE_MODULE();
					
					};
					
					preloader.close();
					
				}
				
			}catch(e:Error){
				
				Erro.add("ERRO no close do module "+ typeAnterior);
				
			}
			
		}
		
		
		
		
		
		//=============================================================================================//

		
		
		
		
		//========================================== GET ===============================================//
		
		
		public function get getModule():Module
		{
		   return modulo;
		}
		
		public function get getPreloader():PreloaderDefaultBase
		{
		   return preloader;	
		}
				
		//=============================================================================================//

		
		public function dispose():void
		{
			try{
			
				
				if(modulo)
				{
					modulo.dispose();
					container.removeChild(modulo);	
					modulo = null;
				}
	
				
			}catch(e:Error){
			
			
				Erro.add("ERRO na dispose do modulo");
				
			}

			
		}
		
		
	}//end class
}