package com.style24.scm.support.security.session;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.style24.core.support.session.TscSession;
import com.style24.persistence.domain.Menu;
import com.style24.persistence.domain.User;
import com.style24.scm.support.security.TssLoginDetails;

import lombok.extern.slf4j.Slf4j;

/**
 * Session
 * 
 * @author gagamel
 * @since 2020. 10. 19
 */
@Slf4j
public class TssSession extends TscSession {

	/**
	 * Get Session Info.
	 * @return 세션 정보
	 */
	public static User getInfo() {
		TssLoginDetails loginDetails = (TssLoginDetails)RequestContextHolder.currentRequestAttributes().getAttribute("session", RequestAttributes.SCOPE_SESSION);

		if (loginDetails == null)
			return null;

		return loginDetails.getLoginInfo();
	}

	/**
	 * 로그인 여부
	 * @return true/false
	 */
	public static boolean isLogin() {
		TssLoginDetails loginDetails = (TssLoginDetails)RequestContextHolder.currentRequestAttributes().getAttribute("session", RequestAttributes.SCOPE_SESSION);

		if (loginDetails == null)
			return false;

		return loginDetails.isLogin();
	}

	/**
	 * Get the login menu list.
	 * @return 로그인 메뉴 목록
	 */
	public static Collection<Menu> getLoginMenuList() {
		TssLoginDetails loginDetails = (TssLoginDetails)RequestContextHolder.currentRequestAttributes().getAttribute("session", RequestAttributes.SCOPE_SESSION);

		if (loginDetails == null)
			return null;

		return loginDetails.getLoginMenuList();
	}

	/**
	 * 2 fatcory 인증여부
	 * @return true/false
	 */
	public static boolean isTwoFactorCertify() {
		if (StringUtils.isBlank(TscSession.getAttribute("isTfcertify"))) {
			return false;
		}

		return new Boolean(TscSession.getAttribute("isTfcertify"));
	}

}
