package de.tools;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.eclnt.jsfserver.util.HttpSessionAccess;

import jakarta.servlet.http.HttpSession;

public class SessionUtil {

	private SessionUtil() {

	}

	/**
	 * Function to return Client IP
	 * @return Client IP
	 */
	public static String getClientIP(){
		return HttpSessionAccess.getCurrentRequest().getRemoteAddr();
	}
	
	/**
	 * Function to return Client IP
	 * @return Client IP
	 */
	public static String getClientAddr(){
		return HttpSessionAccess.getCurrentRequest().getRemoteHost();
	}
	
	
	public static String getInetAddress() {
		String adr = null;
		
		try {
			adr = InetAddress.getByName(HttpSessionAccess.getCurrentRequest().getRemoteAddr()).getHostName();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return adr;
	}
	
	/**
	 * Function to return the Servers IP
	 * @return Server IP
	 */
	public static String getServerIP(){
		try {
			return InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * Function to return the ServerHostname
	 * @return Server Hostname
	 */
	public static String getServerHostname(){
		try {
			return InetAddress.getLocalHost().getHostName();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * Return activ HttpSession
	 * 
	 * @return HttpSession
	 */
	public static HttpSession getHttpSession() {
		HttpSession httpSession = null;
		try {
			httpSession = HttpSessionAccess.getCurrentHttpSession();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return httpSession;
	}
	
		
//	/**
//	 * Return SessionType
//	 * 
//	 * @return SessionType
//	 */
//	public static SessionTyp getServerSessionType() {
//		SessionTyp sessionType = SessionTyp.PORTAL_SESSION;
//		
//		try {
//			
//			String serverName = getServerHostname();
//			
//			if (serverName != null) {
//				if (serverName.startsWith("NESAS")) {
//					sessionType = SessionTyp.PORTAL_SESSION;
//				} else {
//					sessionType = SessionTyp.CLIENT_SESSION;
//				}
//			}
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return sessionType;
//	}
	
	/**
	 * Return SessionID
	 * @return SessionID
	 */
	public static String getHttpSessionID() {
		return HttpSessionAccess.getCurrentHttpSession().getId();
	}
	
	public static String getRootPath() {
		String rootPath = null;
		try {
			rootPath = HttpSessionAccess.getServletContext().getRealPath("/");			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rootPath;
	}
	
	/**
	 * @return gibt den Namen des Tomcats zurück, z.B. GWPTESTTC1
	 */
	public static String getSessionName() {
		String id = null;
		try {
			id = HttpSessionAccess.getCurrentHttpSession().getId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (id != null && id.indexOf(".") != -1) {
			id = id.substring(id.indexOf(".")+1);
		} else {
			id = "";
		}
		return id;
	}
	
}
