package com.style24.front.support.interceptor;

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

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.front.biz.service.TsfLoginService;
import com.style24.front.support.security.TsfLoginDetails;

import lombok.extern.slf4j.Slf4j;

/**
 * 자동로그인 RememberMe 선처리
 * 
 * @author gagamel
 * @since 2020. 9. 11
 */
@Component
@Slf4j
public class TsfRememberMeInterceptor extends HandlerInterceptorAdapter {

	@Autowired
	private TsfLoginService loginService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//		// RememberMe Cookie
//		String ckRememberMe = GagaCookieUtil.getCookie(request, FoConstants.CK_PREFIX + "_remember-me");
//
//		if (!FoSession.isLogin() && StringUtils.isNotBlank(ckRememberMe)) {
//			// 로그인유지토큰 조회
//			WfoPersistentToken token = loginService.getPersistentToken(ckRememberMe);
//
//			if (token != null) {
//				WfoCustomer customer = new WfoCustomer();
//				customer.setCustId(token.getCustId());
//				WfoCustomer loginInfo = loginService.getLoginCheckInfo(customer);
//
//				// 로그인 성공 시 로그인실패수가 0보다 크면 로그인실패수 reset
//				int failCnt = loginService.getLoginFailCount(token.getCustNo());
//				if (failCnt > 0) {
//					loginService.createLoginFail(token.getCustId(), "N");
//				}
//
//				// 최종로그인일시 Update
//				loginService.updateLastLoginDate(token.getCustNo());
//
//				// 로그인이력 생성
//				loginService.createLoginHistory(token.getCustNo());
//
//				// 세션 생성
//				List<SimpleGrantedAuthority> authorities = new ArrayList<>();
//				authorities.add(new SimpleGrantedAuthority(loginInfo.getCustGb()));
//				this.createSession(request, new FoLoginDetails(loginInfo, authorities));
//
//				Date expiry = new Date(System.currentTimeMillis() + (1000 * FoConstants.REMEMBER_ME_LIMIT));
//
////				log.debug("String to MD5: {}", token.getCustNo() + ":" + GagaDateUtil.getDate(expiry, "yyyyMMddHHmmss") + ":" + WfoSession.getSessionId());
//				String md5HexToken = GagaCryptoUtil.encryptMD5(token.getCustNo() + ":" + GagaDateUtil.getDate(expiry, "yyyyMMddHHmmss") + ":" + FoSession.getSessionId());
////				log.debug(md5HexToken);
//				String remembermeToken = Base64.getEncoder().encodeToString((FoConstants.REMEMBER_ME_KEY + ":" + md5HexToken).getBytes());
////				log.debug("remembermeToken: {}", remembermeToken);
//
//				// RememberMe 쿠키 생성
//				GagaCookieUtil.setSecureCookie(response, FoConstants.CK_PREFIX + "_remember-me", remembermeToken, FoConstants.REMEMBER_ME_LIMIT);
//
//				// 로그인유지토큰 갱신
//				loginService.updatePersistentToken(token.getCustNo(), token.getRemembermeToken(), remembermeToken, expiry);
//			}
//		}

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

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

	/**
	 * Session 생성
	 * @param request - HttpServletRequest
	 * @param loginDetails - 로그인 상세 정보
	 */
	private void createSession(HttpServletRequest request, TsfLoginDetails loginDetails) {
		HttpSession session = request.getSession(true);
		session.setMaxInactiveInterval(1800);
		session.setAttribute("session", loginDetails);
	}

}
