package com.its.op.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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

@Slf4j
@Controller
@RequestMapping("")
public class WebController {

    private final String operContext = "forward:/application/op";
    private final String wallContext = "forward:/application/wall";
    private final String fcltContext = "forward:/application/facility";

    /**
     * 로그인 화면 리다이렉션
     * @param request
     * @param response
     * @return
     */
    @GetMapping({"", "/", "/index.do", "/login.do"})
    public ModelAndView getLogin(HttpServletRequest request, HttpServletResponse response) {
        log.error("getLogin1: {}", request);
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if(auth != null) {
            log.error("getLogin2: {}", auth);
            return new ModelAndView("redirect:/application/op/00.main/main.html");
            //new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return new ModelAndView("forward:/application/login/login.html");
        //return new ModelAndView("forward:/api/auth/login.do");
    }

    /**
     * 시설물 관리 리다이렉션
     * @param request
     * @param response
     * @return
     */
    @GetMapping({"/facility", "/facility/", "/facility/index.do"})
    public ModelAndView facility(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView(this.fcltContext + "/index.html");
    }

    /**
     * 상황판 리다이렉션
     * @param request
     * @param response
     * @return
     */
    @GetMapping({"/wall", "/wall/", "/wall/index.do"})
    public ModelAndView wall(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView(this.wallContext + "/index.html");
    }

}
