package com.ejie.m62fb.control;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * LoginFromExternalAppController
 * 
 * loginFromExternalAppController.rest.be
 * 
XXXX
 */
@Controller
@RequestMapping(value = "/loginFromExternalApp")
public class LoginFromExternalAppController {
	private static final Logger logger = LoggerFactory.getLogger(LoginFromExternalAppController.class);
	/**
	 * Cambiar variable a true cuando se pase a desarrollo/pruebas/producción.
	 */
	private static final boolean produccion = true;
	/**
	 * Method loginFromExternalApp/inicio
	 * 
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @param n38uidsesion
	 *            String
	 * @param n38dominio
	 *            String
	 * @param loginPath
	 *            String
	 * @throws IOException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public void loginPath( HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "n38uidsesion", required = true) String n38uidsesion,
			@RequestParam(value = "n38dominio", required = true) String n38dominio)

	throws IOException {
		logger.info("[GET] : login from external");
		try {
			// url a la que redireccionamos
			String ctxPath = request.getContextPath();
			StringBuffer urlNueva = new StringBuffer(200);
			urlNueva.append(ctxPath);

			setCookiesAndRedirect(request, response, n38uidsesion, n38dominio, urlNueva.toString());

		} catch (Exception e) {
			return;
		}
	}
	/**
	 * Method loginFromExternalApp/login
	 * 
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @param n38uidsesion
	 *            String
	 * @param n38dominio
	 *            String
	 * @param loginPath
	 *            String
	 * @throws IOException
	 */
	@RequestMapping(value = "/{url}", method = RequestMethod.GET)
	public void loginPath(@PathVariable String url, HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "n38uidsesion", required = true) String n38uidsesion,
			@RequestParam(value = "n38dominio", required = true) String n38dominio,
			@RequestParam(value = "id", required = true) String detailId)

	throws IOException {
		logger.info("[GET] : login from external");
		try {
			// url a la que redireccionamos
			String ctxPath = request.getContextPath();
			StringBuffer urlNueva = new StringBuffer(200);
			urlNueva.append(ctxPath);
			urlNueva.append("/" + url);
			urlNueva.append("/maint/" + detailId);

			setCookiesAndRedirect(request, response, n38uidsesion, n38dominio, urlNueva.toString());

		} catch (Exception e) {
			return;
		}
	}
	/**
	 * Method loginFromExternalApp/login
	 * 
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @param n38uidsesion
	 *            String
	 * @param n38dominio
	 *            String
	 * @param tipter
	 *            String
	 * @param codter
	 *            String
	 * @param verter
	 *            String
	 * @param dni
	 *            String
	 * @param idRecuperarFactura
	 *            String
	 * @throws IOException
	 */
	@RequestMapping(value = "/{url}/{suburl}", method = RequestMethod.GET)
	public void loginPathSubPath(@PathVariable String url, HttpServletRequest request, HttpServletResponse response,
			@PathVariable String suburl,
			@RequestParam(value = "n38uidsesion", required = true) String n38uidsesion,
			@RequestParam(value = "n38dominio", required = true) String n38dominio,
			@RequestParam(value = "id", required = true) String detailId)

	throws IOException {
		logger.info("[GET] : login from external");
		try {
			// url a la que redireccionamos
			String ctxPath = request.getContextPath();
			StringBuffer urlNueva = new StringBuffer(200);
			urlNueva.append(ctxPath);
			urlNueva.append("/" + url+"/"+suburl);
			urlNueva.append("/maint/" + detailId);

			setCookiesAndRedirect(request, response, n38uidsesion, n38dominio, urlNueva.toString());

		} catch (Exception e) {
			return;
		}
	}

	private void setCookiesAndRedirect(HttpServletRequest request,
			HttpServletResponse response, String n38uidsesion, String n38dominio, String url) throws IOException {
		if (LoginFromExternalAppController.produccion) {
			// Con XLNETS
			// cookie n38UidSesion
			Cookie cookieSesion = new Cookie("n38UidSesion", n38uidsesion);
			cookieSesion.setMaxAge(-1);
			cookieSesion.setPath(request.getContextPath());
			response.addCookie(cookieSesion);
			// cookie n38DominioUid
			cookieSesion = new Cookie("n38DominioUid", n38dominio);
			cookieSesion.setMaxAge(-1);
			cookieSesion.setPath(request.getContextPath());
			response.addCookie(cookieSesion);
		} else {
			// Con MOCK
			// cookie udaMockUserName
			// TODO
			// Por peticion de Irene metemos el usuario a pelo
			Cookie cookieSesion = new Cookie("udaMockUserName", "M62FBU01");
			cookieSesion.setMaxAge(-1);
			cookieSesion.setPath(request.getContextPath());
			response.addCookie(cookieSesion);
		}
		response.sendRedirect(url);
	}

}
