package com.style24.admin.support.controller;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import lombok.extern.slf4j.Slf4j;

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

/**
 * Error Controller
 * 
 * @author gagamel
 * @since 2020. 10. 5
 */
@Controller
@Slf4j
@RequestMapping(value = "/error")
public class TsaErrorController extends TsaBaseController {

	/**
	 * Error
	 */
	@GetMapping("/500")
	public ModelAndView error(HttpServletRequest request) throws HttpRequestMethodNotSupportedException {
		ModelAndView mav = new ModelAndView("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());
				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());
		mav.addObject("message", String.valueOf(request.getAttribute(RequestDispatcher.ERROR_MESSAGE)));

		return mav;
	}

	@GetMapping("/noSession")
	public ModelAndView noSession(HttpServletRequest request) throws HttpRequestMethodNotSupportedException {
		ModelAndView mav = new ModelAndView("error/500");

		mav.addObject("status", GagaResponseStatus.UNAUTHORIZED.getCode());
		mav.addObject("message", String.valueOf(request.getAttribute(RequestDispatcher.ERROR_MESSAGE)));

		return mav;
	}

}
