package server.ws;
	import java.util.ArrayList;

import javax.websocket.OnClose;
	import javax.websocket.OnError;
	import javax.websocket.OnMessage;
	import javax.websocket.OnOpen;
	import javax.websocket.server.ServerEndpoint;
	
@ServerEndpoint("/websocketendpoint")	
public class Server {
	
		private ArrayList<Chat> connexions_chat=new ArrayList<Chat>();
		private ArrayList<MatchingId> annuaire_extern_users=new ArrayList<MatchingId>();
		private ArrayList<MatchingId> annuaire_intern_users=new ArrayList<MatchingId>();
	 
	     
		public boolean estUnEntier(String chaine) {
			try {
				Integer.parseInt(chaine);
			} catch (NumberFormatException e){
				return false;
			}
	 
			return true;
		}
	
	    @OnOpen
	    public void onOpen(){
	        System.out.println("Open Connection ...");
	    }
	     
	    @OnClose
	    public void onClose(){
	        System.out.println("Close Connection ...");
	    }
	     
	    @OnMessage
	    public void onMessage(String message){
	    	String chara=message.substring(0);
	    	if(chara=="I" ) {
	    		//insertion tab matchinid;
	    		//-----> BROADCAST
	    	}
	    	else if (chara=="P"){
	    		//insertion tab;
	    	}
	    	else if(estUnEntier(message)) {
	    		String msg="Le Client correspond avec le destinataire numéro: "+message;
	    		System.out.println(msg);
	    	}
	    	else {
	    		System.out.println("Message from the client: " + message);
	        }
	        //String echoMsg = "Echo from the server : " + message;
	        //return echoMsg;
	    }
	 
	    @OnError
	    public void onError(Throwable e){
	        e.printStackTrace();
	    }
}
