/**
	 * requestAsync
	 * 
	 * Request Async Method to JSONProxy 
	 * 
	 * @param[in] jsonRequest request json string for request method [mandatory]
	 * @param[in] callback The Callback hanlder for request [optional]
	 * @see http://wiki.wemade.com/display/TECHDIV/WmInterfaceBroker_JSONProtocols
	 */
	public void   requestAsync(string jsonString, WmInterfaceBrokerDelegate callback ){
		Debug.Log("send " + jsonString);
		JSONObject jsonObject = JSONObject.Parse(jsonString);
		string uriString = jsonObject.GetString("uri");
		
		if(uriString == null){
			Debug.LogError(DOMAIN + "WmInterfaceBroker cannot use without uri");
			onResponse(jsonResultForError("WmInterfaceBroker",(long)WemeManager.WemeErrType.WM_ERR_INVALID_PARAMETER,"cannot use without uri",null).ToString());
			return;	
		}
		if(callback!=null){
			addEventHandler(uriString,callback);
		}else{
			Debug.LogError(DOMAIN + "WmInterfaceBroker cannot use without callback");
			onResponse(jsonResultForError("WmInterfaceBroker",(long)WemeManager.WemeErrType.WM_ERR_INVALID_PARAMETER,"cannot use without callback",uriString).ToString());
			return;	
		}
	}
	private void addEventHandler(string uri,WmInterfaceBrokerDelegate callback){
		if(eventDictionary.Contains(uri)){
			eventDictionary.Remove(uri);	
		}
		eventDictionary.Add(uri,callback);
	}
/*	
	public static void onResult(string resultObject,string resultMethod,string resultString){
		WemeLoom.QueueOnMainThread(()=>{
			GameObject obj = GameObject.Find(resultObject);
			if (obj != null)
				obj.SendMessage(resultMethod, resultString);
			//onResponse(resultString);
		});
		
	}*/
	
	void onResponse(string jsonResult){
		Debug.Log("result log " + jsonResult);
		try{
			JSONObject requestJSON  = JSONObject.Parse(jsonResult);
			string uri = requestJSON.GetString("uri");
			if(string.IsNullOrEmpty(uri)==true){
				Debug.LogError("WmInterfaceBroker must have uri param onResonse result :  "+jsonResult);
				return;	
			}	
			WmInterfaceBrokerDelegate callback =getValueDictionaryForKey(uri);
			if(callback==null){
				Debug.LogError("WmInterfaceBroker callback storage error :  "+jsonResult);
				return;
			}
			WmInterfaceBrokerDelegate eventDelegate = new WmInterfaceBrokerDelegate(callback);
			eventDelegate(jsonResult);
			
		}catch(Exception e){
			Debug.LogError(DOMAIN + " UncaughtException : "+e.Message);
		}
		
	}