package com.guru.facebook 
{
	import com.facebook.data.users.GetInfoFieldValues;
	import com.guru.call.FunctionCall;
	import com.guru.call.ICall;
	import com.guru.conn.ConnAbstract;
	import com.guru.conn.ConnPath;
	import com.guru.panel.Panel;
	import com.guru.panel.StatusBar;
	import com.guru.utils.TimerUtil;
	import com.guru.utils.TraceUtil;
	import flash.system.Security;
	/**
	 * ...
	 * @author Eddy Lee
	 */
	public class FacebookIFrameCenter
	{
		private static var sSessionForBrowserBug:String = "";
		
		//Guru Aya uid
		public static var UID:String = "100001033246788";
		public static var EMAIL:String;
		public static var NAME:String;
		public static var BIRTHDAY:String;
		public static var PIC:String;
		public static var SEX:String;
		public static var PROFILE_URL:String;
		public static var FRIEND_LIST:Array;
		
		private static var sInitStack_array:Array;
		private static var sInitDoneCall:ICall;
		private static var sInfoFieldsValue_str:String;
		private static var sConnAbstract:ConnAbstract;
		
		public function FacebookIFrameCenter() 
		{
			
		}
		
		public static function init(pDoneCall:ICall = null):void
		{
			FacebookDataBridge.MODE = FacebookDataBridge.IFRAME;
			
			sInitStack_array = new Array();
			sInitDoneCall = pDoneCall;
			
			Security.loadPolicyFile("http://profile.ak.fbcdn.net/crossdomain.xml");
			sInfoFieldsValue_str = GetInfoFieldValues.NAME + "," + GetInfoFieldValues.SEX + "," + GetInfoFieldValues.PROXIED_EMAIL + "," + GetInfoFieldValues.PROFILE_URL + "," + GetInfoFieldValues.PIC_SQUARE + "," + GetInfoFieldValues.BIRTHDAY;
			
			Panel.showStatusBar(StatusBar.LOADING, "資料載入中，請稍候");
			
			//sInitStack_array.push(new FunctionCall(FacebookIFrameCenter, FacebookIFrameCenter.getSelfProfile));
			//sInitStack_array.push(new FunctionCall(FacebookIFrameCenter, FacebookIFrameCenter.getFriends));
			
			invokeNextCallInStack();
		}
		
		public static function getSessionForBrowserBug():String
		{
			return encodeURIComponent(sSessionForBrowserBug);
		}
		
		public static function setSessionForBrowserBug(pSession_str:String):void
		{
			Logger.eddy("SessionForBrowserBug set");
			sSessionForBrowserBug = pSession_str;
		}
		
		public static function setUid(pUid_str:String):void
		{
			UID = pUid_str;
		}

		public static function getSelfProfile(pDoneCall:ICall = null):void
		{
			Logger.eddy("FacebookIFrameCenter.getSelfProfile()");
			sConnAbstract = new ConnAbstract(ConnPath.getProfilePath(), new FunctionCall(FacebookIFrameCenter, onGetSelfProfileSuccess, pDoneCall), new FunctionCall(FacebookIFrameCenter, onConnFail));
			sConnAbstract.send({uid:UID, fields:sInfoFieldsValue_str});
		}
		
		private static function onGetSelfProfileSuccess(pDoneCall:ICall = null):void
		{
			var xml:XML = sConnAbstract.getXML();
			//Logger.eddy(xml);
			if (xml..status[0] == "true")
			{
				EMAIL = xml..item[0][GetInfoFieldValues.PROXIED_EMAIL][0];
				NAME = xml..item[0][GetInfoFieldValues.NAME][0];
				SEX = xml..item[0][GetInfoFieldValues.SEX][0];
				BIRTHDAY = xml..item[0][GetInfoFieldValues.BIRTHDAY][0];
				PIC = xml..item[0][GetInfoFieldValues.PIC_SQUARE][0];
				PROFILE_URL = xml..item[0][GetInfoFieldValues.PROFILE_URL][0];
			}
			else
			{
				
			}
			
			if(pDoneCall != null)
				pDoneCall.execute();
		}
		
		public static function getFriends(pDoneCall:ICall = null):void
		{
			Logger.eddy("FacebookIFrameCenter.getFriends()");
			sConnAbstract = new ConnAbstract(ConnPath.getFriendsPath(), new FunctionCall(FacebookIFrameCenter, onGetFriendsSuccess, pDoneCall), new FunctionCall(FacebookIFrameCenter, onConnFail));
			sConnAbstract.send({fields:sInfoFieldsValue_str});
		}
		
		private static function onGetFriendsSuccess(pDoneCall:ICall = null):void
		{
			var xml:XML = sConnAbstract.getXML();
			
			if (xml..status[0] == "true")
			{
				FRIEND_LIST = new Array();
				
				var xml_array:Array = new Array();
				for  each (var tempItem in  xml.item)
				{
					xml_array.push(tempItem);
				}
				xml_array.sortOn("name");
				for (var i:Number = 0; i < xml_array.length; i++ )
				{
					var data_array:Array = new Array();
					
					data_array["uid"] = xml_array[i]["uid"][0];
					data_array["name"] = xml_array[i][GetInfoFieldValues.NAME][0];
					data_array["sex"] = xml_array[i][GetInfoFieldValues.SEX][0];
					data_array["pic"] = xml_array[i][GetInfoFieldValues.PIC_SQUARE][0];
					data_array["profile_url"] = xml_array[i][GetInfoFieldValues.PROFILE_URL][0];
					data_array["birthday"] = xml_array[i][GetInfoFieldValues.BIRTHDAY][0];
					data_array["proxied_email"] = xml_array[i][GetInfoFieldValues.PROXIED_EMAIL][0];
					
					FRIEND_LIST.push(data_array);
				}
			}
			else
			{
				
			}
			
			if(pDoneCall != null)
				pDoneCall.execute();
		}
		
		private static function onConnFail():void
		{
			invokeNextCallInStack();
		}
		
		private static function invokeNextCallInStack():void
		{
			if (sInitStack_array.length > 0)
			{
				var call:ICall = sInitStack_array.shift();
				call.execute(new FunctionCall(FacebookIFrameCenter, invokeNextCallInStack));
			}
			else
			{
				onGetAllDataDone();
			}
		}
		
		private static function onGetAllDataDone():void
		{
			Logger.eddy("*****************************************");
			Logger.eddy("* UID: " + UID);
			Logger.eddy("* NAME: " + NAME);
			Logger.eddy("* SEX: " + SEX);
			Logger.eddy("* BIRTHDAY: " + BIRTHDAY);
			Logger.eddy("* PROFILE_PIC: " + PIC);
			Logger.eddy("* PROFILE_URL: " + PROFILE_URL);
			Logger.eddy("* EMAIL: " + EMAIL);
			//Logger.eddy("* Number of friends: " + FRIEND_LIST.length);
			Logger.eddy("* Get Date From Facebook Done!");
			Logger.eddy("*****************************************");
			
			Panel.hideStatusBar();
			
			if (sInitDoneCall != null)
			{
				sInitDoneCall.execute();
				sInitDoneCall = null;
			}
		}
	}

}