package com.style24.front.support.interceptor;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.style24.front.support.env.TsfConstants;
import com.style24.front.support.security.session.TsfSession;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaCookieUtil;
import com.gagaframework.web.util.GagaStringUtil;

/**
 * Return URL Interceptor
 * 
 * @author gagamel
 * @since 2020. 9. 11
 */
@Component
@Slf4j
public class TsfReturnUrlInterceptor extends HandlerInterceptorAdapter {

	@Value("${has-ssl}")
	private String hasSsl;

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String queryString = GagaStringUtil.convertParameterToQueryString(request.getParameterMap());
		if (StringUtils.isNotBlank(queryString)) {
			queryString = "?" + queryString;
		}
//		if (StringUtils.isNotBlank(TsfSession.getAttribute("afLinkCd"))) {
//			queryString += (queryString.contains("?") ? "&" : "?") + "afLinkCd=" + TsfSession.getAttribute("afLinkCd");
//		}

		String returnUrl = request.getRequestURL() + queryString;
		log.info("returnUrl: [{}]", returnUrl);

		// SSL Server
		boolean isSslServer = Boolean.parseBoolean(hasSsl);
		log.info("isSslServer: [{}]", isSslServer);

		// 공유하기 URL을 통해 접근한 경우 shareUrl로 쿠키에 설정
		if (StringUtils.isNotBlank(returnUrl) && returnUrl.lastIndexOf("share") > -1) {
			returnUrl = returnUrl.replaceAll("&share=Y", "");
			log.info("shareUrl==> {}", returnUrl);
			if (isSslServer) {
				GagaCookieUtil.setSecureCookie(response, TsfConstants.CK_PREFIX + "_share_url", returnUrl, -1);
			} else {
				GagaCookieUtil.setCookie(response, TsfConstants.CK_PREFIX + "_share_url", returnUrl, -1);
			}

		}

		if (isSslServer) {
			GagaCookieUtil.setSecureCookie(response, TsfConstants.CK_PREFIX + "_return_url", returnUrl, -1);
		} else {
			GagaCookieUtil.setCookie(response, TsfConstants.CK_PREFIX + "_return_url", returnUrl, -1);
		}

//		log.info("request.isSecure(): [{}]", request.isSecure());
//		log.info("request.getServerName(): [{}]", request.getServerName());
//		log.info("request.getRequestURI(): [{}]", request.getRequestURI());
//		log.info("request.getRequestURL(): [{}]", request.getRequestURL());
//		log.info("request.getQueryString(): [{}]", request.getQueryString());

		if (!request.getRequestURI().contains("/noMember")) {
			TsfSession.removeAtrribute("noMemberOrdNo");
			TsfSession.removeAtrribute("noMemberOrdNm");
			TsfSession.removeAtrribute("noMemberOrdPhnno");
		}

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

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

}
