package com.armundia.app.session;

import java.util.Date;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import com.armundia.arm_am.services.IOpzioni;
import com.armundia.arm_am.services.OpzioniApp;
import com.armundia.v2.shared.interfaces.ISessionUser;
import com.armundia.v2.shared.rest.RestBase;


@Path("session")
@Produces("application/json")
public class SessionRest extends RestBase {
	private static final Logger log = Logger.getLogger(SessionRest.class.getName());

	@Inject
	private ISessionUser sessionUser;
	
	@GET
	@Path("keep-alive")
	public Response keepAlive(@Context HttpServletRequest request) {
		AliveStatusPojo aliveStatus = new AliveStatusPojo();
		
		if (sessionUser != null) {
			if (sessionUser.getUserId() != null) {
				aliveStatus.session = true;
			}
			String ipAddress = getIpAddress(request);
			String jsessionId = request.getSession().getId();
			log.info("keepAlive: " + sessionUser.getUserId() + " IP: " + ipAddress + " jsessionId: " + jsessionId + " timestamp: " + new Date());
		}
		
		IOpzioni opt = OpzioniApp.getInstance();
		boolean isAppLocked = opt.getBoolean("lock-stub-war", "stub-war", false);
		aliveStatus.application = !isAppLocked;
		
		return Response.ok(aliveStatus).build();
	}
}
