package customsocket;

import com.corundumstudio.socketio.AckRequest;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.collections.List;
import anywheresoftware.b4a.objects.collections.Map;

@BA.ShortName("AckRequest")
public class AckRequestWrapper  {
	private final AckRequest request;
	
	public AckRequestWrapper(AckRequest req) {
		request = req;
	}
	
	/**
	 * Check if Acknowledge is required
	 * @return true if required
	 */
	public boolean isAckRequested() {
		if (request!=null) {
			return request.isAckRequested();
		}
		return false;
	}
	
	/**
	 * Reply with array of object
	 * @param value array of object
	 */
	public void Reply_Array(Object[] value) {
		if (value!=null) {
			if (value.length>0) {
				request.sendAckData(value);
			}
		}
		notify_this();
	}
	
	/**
	 * Reply with a String 
	 * @param value String value
	 */
	public void Reply_String(String value) {
		if (value != null) {
			if (value.length()>0) {
				request.sendAckData(value);
			}
		}
		notify_this();
		
	}
	
	/**
	 * Reply with a Map
	 * @param value Map value
	 */
	public void Reply_Map(Map value) {
		
		if (value!=null) {
			if (value.IsInitialized()) {
				if (value.getSize()>0) {
					request.sendAckData(value.getObject());
				}
			}
		}
		notify_this();
	}
	
	/**
	 * Reply with a List
	 * @param value List value
	 */
	public void Reply_List(List value) {
		
		if (value!=null) {
			if (value.IsInitialized()) {
				if (value.getSize()>0) {
					request.sendAckData(value.getObject());
				}
			}
		}
		notify_this();
		
	}
	
	private void notify_this() {
		synchronized(this) {
			try {
				this.notify();	
			} catch(IllegalMonitorStateException e) {
				System.out.println("this.notify exception = "+e.getMessage());
			}
			
		}
	}
}
