package ArchiChaT.Rest;

import ArchiChaT.Models.Message;
import ArchiChaT.Models.User;
import ArchiChaT.Server;
import ArchiChaT.Services.IMessageService;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;

/**
 * Created by SMITHE on 14-Jun-17.
 */

@Path( "/messages" )
public class RestMessage implements IRest< Message > {
	
	@Override
	@GET
	@Path( "{id}" )
	@Produces( MediaType.APPLICATION_JSON )
	public Message getOne( int id ) throws RemoteException, NotBoundException {

		IMessageService messageService = (IMessageService) lookupService(Server.MESSAGE_SERVICE_NAME);
		return messageService.getOne(id);

	}

	//@Override
	@GET
	@Path( "{exp}/{dest}" )
	@Produces( MediaType.APPLICATION_JSON )
	public ArrayList<Message> getDmMessage(User exp, User dest) throws RemoteException, NotBoundException {

		IMessageService messageService = (IMessageService) lookupService(Server.MESSAGE_SERVICE_NAME);
		return messageService.getDmMessage(exp, dest);

	}

	@Override
	@GET
	@Produces( MediaType.APPLICATION_JSON )
	public ArrayList< Message > getAll() throws RemoteException, NotBoundException {
		IMessageService messageService = (IMessageService) lookupService(Server.MESSAGE_SERVICE_NAME);
		return messageService.getAllMessage();
	}
	
	@Override
	@PUT
	@Path( "{id}" )
	@Produces( MediaType.APPLICATION_JSON )
	@Consumes( MediaType.APPLICATION_JSON )
	public Message put( @PathParam( "id" ) int id, Message resource ) {
		// TODO Update file
		
		return resource;
	}

	@Override
	@POST
	@Produces( MediaType.APPLICATION_JSON )
	@Consumes( MediaType.APPLICATION_JSON )
	public Message post( Message resource ) throws RemoteException, NotBoundException {
		// TODO Update file
		IMessageService messageService = (IMessageService) lookupService(Server.MESSAGE_SERVICE_NAME);
		return messageService.getOne(messageService.saveMessage(resource));
	}
	
	@Override
	@DELETE
	@Path( "{id}" )
	public void delete( @PathParam( "id" ) int id ) {
		// TODO Delete on file
	}

	@Override
	public Object lookupService(String serviceName) throws RemoteException, NotBoundException {
		Registry registry = LocateRegistry.getRegistry( Server.HOST, Server.PORT );
		return registry.lookup( serviceName );
	}
}

@@ -148,16 +148,66 @@
 Service;
+%0Aimport ArchiChaT.Services.IUserManagementService;
 %0A%0Aimport
@@ -650,16 +650,35 @@
  getOne(
+ @PathParam( %22id%22 )
  int id 
@@ -898,15 +898,19 @@
 %7Bexp
+ID
 %7D/%7Bdest
+ID
 %7D%22 )
@@ -995,73 +995,365 @@
 age(
-User exp, User dest) throws RemoteException, NotBoundException %7B%0A
+ @PathParam( %22expID%22 ) int expID, @PathParam( %22destID%22 ) int destID) throws RemoteException, NotBoundException %7B%0A%09%09IUserManagementService userManagementService = ( IUserManagementService ) lookupService( Server.USERMANAGEMENT_SERVICE_NAME );%0A%09%09User expUser = userManagementService.find( expID );%0A%09%09User destUser = userManagementService.find( destID );%0A%09%09%0A%09%09
 %0A%09%09I
@@ -1490,14 +1490,22 @@
 (exp
+User
 , dest
+User
 );%0A%0A
@@ -2209,38 +2209,16 @@
 ption %7B%0A
-%09%09// TODO Update file%0A
 %09%09IMessa
@@ -2418,16 +2418,16 @@
 %7Bid%7D%22 )%0A
+
 %09public 
@@ -2473,33 +2473,8 @@
 ) %7B%0A
-%09%09// TODO Delete on file%0A
 %09%7D%0A%0A

Fix RestUser: Add forget annotation for REST