package info.elexis.server.core.service;

import static info.elexis.server.core.constants.RestPathConstants.*;

import java.util.Date;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.osgi.service.component.annotations.Component;

import info.elexis.server.core.connector.elexis.ElexisDBConnection;
import info.elexis.server.core.extension.DBConnection;
import info.elexis.server.core.internal.Application;
import info.elexis.server.core.scheduler.Scheduler;
import info.elexis.server.core.scheduler.SchedulerStatus;
import info.elexis.server.core.security.HTTPAuthHandler;

@Component(service = HTTPService.class, immediate = true)
@Path(BASE_URL_CORE)
public class HTTPService {
	@Context
	UriInfo uriInfo;
	@Context
	HttpServletRequest request;

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public Response getStatus() {
		long millis = new Date().getTime() - Application.getStarttime().getTime();
		String result = "Uptime: " + String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(millis),
				TimeUnit.MILLISECONDS.toSeconds(millis)
						- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
		return Response.ok(result).build();
	}

	@GET
	@Path(LOGIN+"/{userid}")
	@Produces(MediaType.TEXT_PLAIN)
	public Response getSessionId(@PathParam("userid") String userid, @HeaderParam("encoded") String oPassword) {
		// TODO we should encode the IP into the session
		Optional<String> password = Optional.ofNullable(oPassword);
		if (password.isPresent()) {
			String id = HTTPAuthHandler.getSessionId(userid, password.get()).toString();
			return Response.ok(id).build();
		}
		return Response.status(Response.Status.BAD_REQUEST).build();
	}

	@GET
	@Path(HALT)
	@RolesAllowed("admin")
	public Response haltApplication() {
		Application.getInstance().shutdown();
		return Response.ok().build();
	}

	@GET
	@Path(RESTART)
	@RolesAllowed("admin")
	public Response restartApplication() {
		Application.getInstance().restart();
		return Response.ok().build();
	}

	@POST
	@Path(ELEXIS_CONNECTION)
	@RolesAllowed("admin")
	public Response setData(DBConnection connection) {
		ElexisDBConnection.updateConnection(connection);
		return Response.ok().build();
	}

	@GET
	@Path(ELEXIS_CONNECTION)
	@Produces(MediaType.APPLICATION_XML)
	public Response getElexisDBConnectionStatus() {
		Optional<DBConnection> connectiono = ElexisDBConnection.getConnection();
		if (connectiono.isPresent()) {
			DBConnection dbc = connectiono.get();
			dbc.password = "removed";
			return Response.ok(dbc).build();
		}
		return Response.status(Response.Status.NOT_FOUND).build();
	}

	@GET
	@Path(SCHEDULER)
	@Produces(MediaType.APPLICATION_XML)
	public Response getSchedulerStatus() {
		SchedulerStatus schedulerStatus = Scheduler.INSTANCE.getSchedulerStatus();
		return Response.ok(schedulerStatus).build();
	}

	@GET
	@Path("/{subResources:.*}")
	public Response getInESPluginBundle(@PathParam("subResources") String subResources) {
		// consider access rights, call registered resources
		System.out.println(subResources);
		return null;
	}
}

@@ -644,16 +644,73 @@
 mponent;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1219,16 +1219,84 @@
 request;
+%0A%09%0A%09private Logger log = LoggerFactory.getLogger(HTTPService.class);
 %0A%0A%09@GET%0A
@@ -2915,16 +2915,57 @@
 tion();%0A
+%09%09DBConnection dbc = new DBConnection();%0A
 %09%09if (co
@@ -2992,29 +2992,16 @@
 )) %7B%0A%09%09%09
-DBConnection 
 dbc = co
@@ -3040,108 +3040,58 @@
  = %22
-removed%22;%0A%09%09%09return Response.ok(dbc).build();%0A%09%09%7D%0A%09%09return Response.status(Response.Status.NOT_FOUND
+%5BPASSWORD REMOVED%5D%22;%0A%09%09%7D %0A%09%09return Response.ok(dbc
 ).bu
@@ -3449,24 +3449,70 @@
 esources) %7B%0A
+%09%09log.info(%22Unhandled GET on %22+subResources);%0A
 %09%09// conside
@@ -3560,55 +3560,43 @@
 s%0A%09%09
-System.out.println(subResources);%0A%09%09return null
+return Response.noContent().build()
 ;%0A%09%7D

Refactor HTTPService