package com.style24.admin.support.interceptor;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.support.exception.TscNoSessionException;
import com.style24.core.support.message.TscMessageByLocale;

import lombok.extern.slf4j.Slf4j;

/**
 * 매장POS 접근 시 매장로그인 세션 확인
 * 
 * @author gagamel
 * @since 2020. 10. 5
 */
@Component
@Slf4j
public class TsaPosInterceptor extends HandlerInterceptorAdapter {

	@Autowired
	private TscMessageByLocale message;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		boolean isStoreLogin = TsaSession.isStoreLogin();
		log.info("isStoreLogin: [{}]", isStoreLogin);

		if (!isStoreLogin) {
			throw new TscNoSessionException(message.getMessage("LOGN_0003"));
		}

		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
		ModelAndView modelAndView) throws Exception {
		// Do nothing
	}

}
