package com.style24.front.biz.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.style24.core.support.message.TscMessageByLocale;
import com.style24.front.support.controller.TsfBaseController;
import com.style24.front.support.security.session.TsfSession;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.rest.server.GagaResponseStatus;

/**
 * Index Controller
 * 
 * @author gagamel
 * @since 2020. 9. 11
 */
@Controller
@Slf4j
public class TsfIndexController extends TsfBaseController {

	@Autowired
	private Environment env;

	@Autowired
	private TscMessageByLocale message;

	/**
	 * 에러 페이지
	 * @return
	 * @author gagamel
	 * @throws IOException
	 * @since 2020. 3. 18
	 */
	@GetMapping("/error")
	public ModelAndView error(HttpServletRequest request, HttpServletResponse response) throws HttpRequestMethodNotSupportedException, IOException {
		ModelAndView mav = new ModelAndView(super.getDeviceViewName("error/500"));

		Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

		if (status != null) {
			Integer statusCode = Integer.valueOf(status.toString());
			log.debug("statusCode: {}", statusCode);

			if (statusCode == GagaResponseStatus.NOT_FOUND.getCode()) {
				mav.addObject("status", GagaResponseStatus.NOT_FOUND.getCode());

				// 404 오류 시 잘못된 링크 유입 시 몰메인으로 이동 처리
				String prevRequestUri = (String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
				log.debug("prevRequestUri: {}", prevRequestUri);
				if (prevRequestUri.startsWith("/m")) {
					response.sendRedirect("/");
				}

				String profiles = env.getProperty("spring.profiles.active").toLowerCase();
				if ("locd".equals(profiles) || "locp".equals(profiles) || "dev".equals(profiles)) {
					mav.addObject("message", "No mapping found for HTTP request with URI ["
						+ String.valueOf(request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI)) + "]");
				}

				return mav;
			}
		}

		mav.addObject("status", GagaResponseStatus.INTERNAL_SERVER_ERROR.getCode());

		String profiles = env.getProperty("spring.profiles.active").toLowerCase();
		if ("locd".equals(profiles) || "locp".equals(profiles) || "dev".equals(profiles)) {
			mav.addObject("message", String.valueOf(request.getAttribute(RequestDispatcher.ERROR_MESSAGE)));
		}

		return mav;
	}

	/**
	 * 첫 페이지
	 * @return
	 * @author gagamel
	 * @since 2020. 2. 3
	 */
	@GetMapping("/")
	public String index() {
		return "forward:/display/mall/main/form";
	}

	/**
	 * 로그인 페이지
	 * @param error - 로그인 오류 정보
	 * @param session - HttpSession
	 * @param snsType - SNS유형(NV:네이버, KK:카카오, FB:페이스북)
	 * @return
	 * @author gagamel
	 * @throws IOException
	 * @since 2020. 2. 4
	 */
	@GetMapping("/signin")
	public ModelAndView signin(@RequestParam(value = "error", required = false) boolean isError, HttpSession session, HttpServletResponse response) throws IOException {
		ModelAndView mav = new ModelAndView();

		if (TsfSession.isLogin()) {
			response.sendRedirect("/");
		}

		log.debug("isError: {}", isError);

		if (isError) {
			Exception e = (Exception)session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
			log.debug("e.getMessage(): {}", e.getMessage());
			mav.addObject("error", e.getMessage());
		}

		mav.addObject("front", env.getProperty("domain.front"));

		// 로그인 페이지로
		mav.setViewName(super.getDeviceViewName("Signin"));

		return mav;
	}

}
