package com.takensoft.pohangTp.common.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.http.HttpSession;

import java.util.HashMap;

public class AuthUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(AuthUtil.class);

    //Session에 등록된 Login User ID Key
    public static final String LOGIN_USER_SESSION = "loginUser";

    //중복 로그인가능 여부
    public static final boolean IS_POSSIBLE_DUPLICATION_LOGIN = false;

    //Session Max 시간(초)
    public static final int SESSION_MAX_TIME = 60*60*6;//6시간

    public static HashMap<String, Object> getLoginUser () {
        try {
            //현재 client의 HttpSession 조회
            HttpSession session = CommonUtil.getHttpSession(false);
            if(session == null || session.getAttribute(LOGIN_USER_SESSION) == null || ((HashMap<String, Object>) session.getAttribute(LOGIN_USER_SESSION)).get("user_id") == null) {
                return null;
            }else {
                return (HashMap<String, Object>) session.getAttribute(LOGIN_USER_SESSION);
            }
        } catch(NullPointerException e) {
            LOGGER.error(e.toString());
         //   System.out.println("AuthUtil getLoginUser Error : ");
         //   e.printStackTrace();
            return null;
        }
    }

    public static String getLoginUserId () {
        HashMap<String, Object> user = getLoginUser();
        if (user != null) {
            return (String) user.get("user_id");
        } else {
            return null;
        }
    }
    public static HashMap<String, Object> getKey () {
        try {
            //현재 client의 HttpSession 조회
            HttpSession session = CommonUtil.getHttpSession(true);
            if(session == null || session.getAttribute("key") == null || ((HashMap<String, Object>) session.getAttribute("key")).get("salt") == null) {
                return null;
            }else {
                return ((HashMap<String, Object>) session.getAttribute("key"));
            }
        } catch(NullPointerException e) {
            LOGGER.error(e.toString());
            return null;
        }
    }

    public static String getKeySaltKey () {
        HashMap<String, Object> key = getKey();
        if (key != null ) {
            if(key.get("salt") != null) {
                return key.get("salt").toString();
            }else {
                return null;
            }
        } else {
            return null;
        }
    }
    public static String getKeyIvtKey () {
        HashMap<String, Object> key = getKey();
        if (key != null ) {
            if(key != null) {
                return key.get("iv").toString();
            }else {
                return null;
            }
        } else {
            return null;
        }
    }
    public static String getKeyENC_KEY () {
        HashMap<String, Object> key = getKey();
        if (key != null ) {
            if(key.get("ENC_KEY") != null) {
                return key.get("ENC_KEY").toString();
            }else{
                return null;
            }
        } else {
            return null;
        }
    }
}
