package classes.matuto.social.facebook
{
	
	import classes.adobe.serialization.json.*;
	import classes.com.*;
	import classes.matuto.core.*;
	import classes.matuto.loaders.*;
	import classes.matuto.model.*;
	import classes.matuto.utils.display.*;
	import classes.matuto.utils.form.*;
	import classes.matuto.utils.model.*;
	
	import flash.display.*;
	import flash.external.ExternalInterface;
	import flash.net.*;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	
	
	public class Facebook
	{
		
		public var dadosFriends					:XMLList;
		public var dadosProfile					:Object = null;
		
		public var urlFace						:String   = "php/facebook/MatutoFb.php";
		public var urlSave						:String   = "php/saveImg.php";	
		public var redirect						:String   = "https://www.mundosafari.com.br/testes/matuto/";	
		public var status						:Boolean;
		public var id							:String   = "100001306516326";
		public var onComplete					:Function;
		public var local:Boolean						  = false;
		
		private static var instance				:Facebook = null;
		private static var possoInstance		:Boolean;
		private static var dadosEstaoCarregados	:Boolean  = false;
		
		
		public function Facebook():void
		{
			if (!Facebook.possoInstance)
			{
				MonsterDebugger.trace("FACEBOOK","FACEBOOK JÁ INSTANCIADO");
			}
		}
		
		//###############################################################################//
		//INCIALIZE
		//###############################################################################//
		
		
		public function INICIALIZE(value:Function,local:Boolean=false):void
		{	
			Security.loadPolicyFile("http://profile.ak.fbcdn.net/crossdomain.xml");
			
			var loaderContext:LoaderContext = new LoaderContext();
			loaderContext.checkPolicyFile 	= true;    
			loaderContext.securityDomain	= SecurityDomain.currentDomain;
			
			onComplete = value;
			
			if(!local)
			{
				ExternalInterface.addCallback("callFace",onCompleteCall);
				ExternalInterface.call("callFace");			
			}else
			{
				status = true;
				value();
			}
			
			
			return void;
		}
		
		private function onCompleteCall(url:String,_status:Boolean,_id:String):void
		{
			redirect = url;
			status   = _status;
			id       = _id;
			
			dadosEstaoCarregados = true;
			
			onComplete();
		}
		
		
		//###############################################################################//
		//###############################################################################//
		
		
		//###############################################################################//
		//GET
		//###############################################################################//
		
		public function get getUser():Object
		{
			return dadosProfile;		
		}
		
		public function get getAmigos():XMLList
		{
			return dadosFriends;
		}
		
		public function get getRedirect():String
		{
			return redirect;
		}
		
		public function get getStatus():Boolean
		{
			return status;
		}
		
		public function get getId():String
		{
			return id;
		}
		
		public function get getStatusDados():Boolean
		{
			return Facebook.dadosEstaoCarregados;		
		}
		
		public function get pictureUser():String
		{
			return "https://graph.facebook.com/"+id+"/picture"
		}
		
		/**
		 * 
		 * @param posicao Foto do perfil principal do amigo
		 * @return 
		 * 
		 */
		public function getPictureFriends(posicao:int):String
		{
			return "https://graph.facebook.com/"+dadosFriends[posicao].id+"/picture"
		}
		
		/**
		 * 
		 * @param id ID DE AMIGO DO FACEBOOK
		 * @param name NAME DE UM AMIGO DO FACEBOOK
		 * @return @[100002049651181:Túlio Hoffimann] 
		 * 
		 */
		public function getFriendMake(id:String,name:String):String
		{
			return "@["+id+":"+name+"]";
		}
		
		/**
		 * 
		 * @param value [{id:IdFriendFace,name:nameFriendFacebook}]
		 * @return @[100002049651181:Túlio Hoffimann],@[100002049651181:Túlio Hoffimann]
		 * 
		 */
		public function getFriendMakeList(value:Array):String
		{
			var resposta:String = "";
			
			for each(var item:Object in value)
			{
				resposta += getFriendMake(item.id,item.name)+",";
			}
			
			return resposta;
		}
		
		/**
		 * 
		 * 
		 * @return @[100002049651181:Túlio Hoffimann],@[100002049651181:Túlio Hoffimann]...
		 * 
		 */
		public function get getFriendMakeAll():String
		{
			var resposta:String = "";
			
			for each(var item:XMLList in dadosFriends)
			{
				resposta += getFriendMake(item.id,item.name)+",";
			}
			
			return resposta;
		}
		
		
		//###############################################################################//
		//###############################################################################//
		
		//###############################################################################//
		//SET
		//###############################################################################//
		
		public function setPostPhotoSave(mensagem:String="",foto:DisplayObject=null,complete:Function=null):void
		{
			
			var nomeFoto		:String 		= TimeName.getName;
			var url				:String 		= Matuto.getConfig.getServer+urlSave+"?nome="+nomeFoto+".jpg";
			var bitmapdata		:BitmapData		= DisplayObjectUtils.PhotoDisplay(foto).bitmapData;
			
			SaveImg.load(url,function():void
			{
				var foto:String = Matuto.getConfig.getServer+"visao/imagens/cards/"+nomeFoto+".jpg";					
				navigateToURL(new URLRequest(Matuto.getConfig.getServer+urlFace+"?photo="+foto+"&id="+getId+"&texto="+mensagem),"_blank");
				complete();		
				
			},bitmapdata, "jpg");
			
		}
		
		public function setPostPhotoUrl(mensagem:String="",foto:String=""):void
		{
			var foto:String = foto+"&id="+getId+"&texto="+mensagem;
			navigateToURL(new URLRequest(Matuto.getConfig.getServer+urlFace+"?photo="+foto),"_blank");
		}
		
		public function setPost(mensagem:String=""):void
		{
			navigateToURL(new URLRequest(Matuto.getConfig.getServer+urlFace+"?id="+id+"&texto="+mensagem+"&feed=true"),"_blank");
		}
		
		//###############################################################################//
		//###############################################################################//
		
		//###############################################################################//
		//SINGLETON//
		//###############################################################################//
		
		public static function getInstance():Facebook
		{
			if(Facebook.instance==null)
			{
				Facebook.possoInstance 	= true;
				Facebook.instance 		= new Facebook();
				Facebook.possoInstance 	= false;
			}
			
			return Facebook.instance;
		}
		
		//###############################################################################//
		//###############################################################################//
		
		
	}//end class
	

	
}