package sgc.portal.websquare.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sgc.portal.SgcPortalAttributes;
import sgc.portal.SgcPortalProperties;
import sgc.portal.base.util.SessionUtil;
import sgc.portal.security.annotation.Secured;
import sgc.portal.security.annotation.SecurityPolicy;
import sgc.portal.com.sym.user.model.UserDetails;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

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

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import sgc.portal.com.sym.user.model.Login;
import sgc.portal.com.sym.user.service.LoginService;

/**
 * <pre>
 * 시스템명   : Common
 * 프로그램ID : WebsquareController
 * 프로그램명  : Websquare Controller
 * 설명 : 웹스퀘어 jsp 호출 처리
 * </pre>
 *
 * @author 이범근
 * @version 2020.01.31
 * @since JDK1.8
 */
@RestController
@RequestMapping("/websquare")
public class WebsquareController {
	@Autowired
	private SgcPortalProperties properties;
	@Autowired
	private LoginService loginService;

	/**
	 * websequare default controller
	 *
	 * @param request
	 *            the request
	 * @return model and view
	 */
	@RequestMapping(value = "/")
	@Secured(policy = SecurityPolicy.NONE)
	public ModelAndView WebSquareIndex(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		String userAgent = request.getHeader("User-Agent").toUpperCase();
		if (userAgent.indexOf("MOBI") > -1) {
			mv.setViewName("redirect:/mobile/");
		} else {
			String jspUrl = "websquare/websquare";
			UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
			String movePage = request.getParameter("w2xPath");
			if (movePage == null || movePage.equals("")) {
				movePage = "/ux/main/main.xml";
			}
			String w2xIsAs = request.getParameter("w2xIsAs");
			if (w2xIsAs == null || "".equals(w2xIsAs)) {
				w2xIsAs = "false";
			}

			// session이 없을 경우 login 화면으로 이동.
			if (userDetails == null || "".equals(userDetails.getUserId())) {
				if (!"/ux/main/login-as.xml".equals(movePage)) {
					movePage = "/ux/main/login.xml";
				}
			} else {
				if (userDetails.isAsMember()) {
					w2xIsAs = "true";
				} else {
					w2xIsAs = "false";
				}
			}
			if ("/ux/main/login.xml".equals(movePage)) {
				if ("true".equals(w2xIsAs)) {
					movePage = "/ux/main/login-as.xml";
				}
			} else if ("/ux/main/login-as.xml".equals(movePage)) {
				w2xIsAs = "true";
			}
			ObjectMapper mapper = new ObjectMapper();
			String jsonInString = "";
			try {
				jsonInString = mapper.writeValueAsString(userDetails);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}

			mv.setViewName(jspUrl);
			mv.addObject("w2xPath", movePage);
			mv.addObject("w2xIsAs", w2xIsAs);

			mv.addObject("userDetails", jsonInString);
			mv.addObject("ozServerUrl", properties.getReport().getServerUrl());
			mv.addObject("smartbillRelayUrl", "");
		}
		return mv;
	}

	/**
	 * websequare default controller
	 *
	 * @param request
	 *            the request
	 * @return model and view
	 * @throws IOException
	 * @ex http://localhost:9099/websquare/insa_login?cmpnCd=TEC&lginId=admin
	 */
	@RequestMapping(value = "/insa_login")
	@Secured(policy = SecurityPolicy.NONE)
	public ModelAndView WebSquareInsa(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView mv = new ModelAndView();
		String cmpnCd = request.getParameter("cmpnCd");
		String lginId = request.getParameter("lginId");
		String jspUrl = "websquare/websquare";
        
		// ---------------------------------------------------------------------------------------------------------------
		// 지정된 url에서 접근 가능하도록 추가
		// ---------------------------------------------------------------------------------------------------------------
		String referer = StringUtils.trimToNull(request.getHeader("Referer"));
		
		if (StringUtils.isEmpty(referer)) {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		
		referer = referer + "insa_login";
		
		if (referer.substring(0, 21).equals("http://gw.usegw.co.kr")
				|| referer.substring(0, 22).equals("https://211.38.140.252")
				|| referer.substring(0, 22).equals("https://gw.usegw.co.kr")
				|| referer.substring(0, 24).equals("https://gw.sgcetec.co.kr")
				|| referer.substring(0, 23).equals("https://gw.sgcenc.co.kr")
				|| referer.substring(0, 26).equals("https://gw.sgcenergy.co.kr")
				|| referer.substring(0, 29).equals("https://gw.sgcsolutions.co.kr")
				|| referer.substring(0, 30).equals("https://gw.sgcgreenpower.co.kr")
				|| referer.substring(0, 24).equals("https://portal.sgc.co.kr")
				) {

			UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
			String movePage = request.getParameter("w2xPath");
			if (movePage == null || movePage.equals("")) {
				movePage = "/ux/main/main.xml";
			}

			// session이 없을 경우 login
			if (userDetails == null || "".equals(userDetails.getUserId())) {

				Login login = new Login();

				if ("TEC".equals(cmpnCd)) {
					cmpnCd = "S0001";
				} else if ("GJE".equals(cmpnCd)) {
					cmpnCd = "S0002";
				} else if ("SMG".equals(cmpnCd)) {
					cmpnCd = "S0003";
				} else if ("SKG".equals(cmpnCd)) {
					cmpnCd = "S0004";
				}

				login.setCmpnCd(cmpnCd);
				login.setLginId(lginId);
				loginService.loginInsa(login, request);

			}

			ObjectMapper mapper = new ObjectMapper();
			String jsonInString = "";
			try {
				jsonInString = mapper.writeValueAsString(userDetails);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}

			mv.setViewName(jspUrl);
			mv.addObject("w2xPath", movePage);
			mv.addObject("userDetails", jsonInString);

		} else {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		// ---------------------------------------------------------------------------------------------------------------
		return mv;

	}

	/**
	 * websequare default controller
	 *
	 * @param request
	 *            the request
	 * @return model and view
	 * @throws IOException
	 * @ex http://localhost:9099/websquare/room_login?cmpnCd=TEC&lginId=admin
	 */
	@RequestMapping(value = "/room_login")
	@Secured(policy = SecurityPolicy.NONE)
	public ModelAndView WebSquarePopupRom(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView mv = new ModelAndView();
		String cmpnCd = request.getParameter("cmpnCd");
		String lginId = request.getParameter("lginId");
		String jspUrl = "websquare/popupRom";

		// ---------------------------------------------------------------------------------------------------------------
		// 지정된 url에서 접근 가능하도록 추가
		// ---------------------------------------------------------------------------------------------------------------
		String referer = StringUtils.trimToNull(request.getHeader("Referer"));

		if (StringUtils.isEmpty(referer)) {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		
		referer = referer + "room_login";

		if (referer.substring(0, 21).equals("http://gw.usegw.co.kr")
				|| referer.substring(0, 22).equals("https://211.38.140.252")
				|| referer.substring(0, 22).equals("https://gw.usegw.co.kr")
				|| referer.substring(0, 24).equals("https://gw.sgcetec.co.kr")
				|| referer.substring(0, 23).equals("https://gw.sgcenc.co.kr")
				|| referer.substring(0, 26).equals("https://gw.sgcenergy.co.kr")
				|| referer.substring(0, 29).equals("https://gw.sgcsolutions.co.kr")
				|| referer.substring(0, 30).equals("https://gw.sgcgreenpower.co.kr")
				) {

			UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
			String movePage = request.getParameter("w2xPath");
			movePage = "/ux/cw/cw0804/CW08040001U.xml";

			// session이 없을 경우 login
			if (userDetails == null || "".equals(userDetails.getUserId())) {

				Login login = new Login();

				if ("TEC".equals(cmpnCd)) {
					cmpnCd = "S0001";
				} else if ("GJE".equals(cmpnCd)) {
					cmpnCd = "S0002";
				} else if ("SMG".equals(cmpnCd)) {
					cmpnCd = "S0003";
				} else if ("SKG".equals(cmpnCd)) {
					cmpnCd = "S0004";
				}

				login.setCmpnCd(cmpnCd);
				login.setLginId(lginId);
				loginService.loginInsa(login, request);

			}

			ObjectMapper mapper = new ObjectMapper();
			String jsonInString = "";
			try {
				jsonInString = mapper.writeValueAsString(userDetails);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}

			mv.setViewName(jspUrl);
			mv.addObject("w2xPath", movePage);
			mv.addObject("userDetails", jsonInString);

		} else {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		// ---------------------------------------------------------------------------------------------------------------
		return mv;

	}

	/**
	 * websequare default controller
	 *
	 * @param request
	 *            the request
	 * @return model and view
	 * @throws IOException
	 * @ex http://localhost:9099/websquare/doc_login?cmpnCd=TEC&lginId=admin
	 */
	@RequestMapping(value = "/doc_login")
	@Secured(policy = SecurityPolicy.NONE)
	public ModelAndView WebSquarePopupDoc(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView mv = new ModelAndView();
		String cmpnCd = request.getParameter("cmpnCd");
		String lginId = request.getParameter("lginId");
		String jspUrl = "websquare/popupDoc";

		// ---------------------------------------------------------------------------------------------------------------
		// 지정된 url에서 접근 가능하도록 추가
		// ---------------------------------------------------------------------------------------------------------------
		String referer = StringUtils.trimToNull(request.getHeader("Referer"));

		if (StringUtils.isEmpty(referer)) {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		
		referer = referer + "doc_login";

		if (referer.substring(0, 21).equals("http://gw.usegw.co.kr")
				|| referer.substring(0, 22).equals("https://211.38.140.252")
				|| referer.substring(0, 22).equals("https://gw.usegw.co.kr")
				|| referer.substring(0, 24).equals("https://gw.sgcetec.co.kr")
				|| referer.substring(0, 23).equals("https://gw.sgcenc.co.kr")
				|| referer.substring(0, 26).equals("https://gw.sgcenergy.co.kr")
				|| referer.substring(0, 29).equals("https://gw.sgcsolutions.co.kr")
				|| referer.substring(0, 30).equals("https://gw.sgcgreenpower.co.kr")
				) {

			UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
			String movePage = request.getParameter("w2xPath");
			movePage = "/ux/qc/qc0102/QC01020001U.xml";

			// session이 없을 경우 login
			if (userDetails == null || "".equals(userDetails.getUserId())) {

				Login login = new Login();

				if ("TEC".equals(cmpnCd)) {
					cmpnCd = "S0001";
				} else if ("GJE".equals(cmpnCd)) {
					cmpnCd = "S0002";
				} else if ("SMG".equals(cmpnCd)) {
					cmpnCd = "S0003";
				} else if ("SKG".equals(cmpnCd)) {
					cmpnCd = "S0004";
				}

				login.setCmpnCd(cmpnCd);
				login.setLginId(lginId);
				loginService.loginInsa(login, request);

			}

			ObjectMapper mapper = new ObjectMapper();
			String jsonInString = "";
			try {
				jsonInString = mapper.writeValueAsString(userDetails);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}

			mv.setViewName(jspUrl);
			mv.addObject("w2xPath", movePage);
			mv.addObject("userDetails", jsonInString);

		} else {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		// ---------------------------------------------------------------------------------------------------------------
		return mv;

	}

	/**
	 * websequare default controller
	 *
	 * @param request
	 *            the request
	 * @return model and view
	 * @throws IOException
	 * @ex http://localhost:9099/websquare/map_login?cmpnCd=TEC&lginId=admin
	 */
	@RequestMapping(value = "/map_login")
	@Secured(policy = SecurityPolicy.NONE)
	public ModelAndView WebSquarePopupMap(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView mv = new ModelAndView();
		String cmpnCd = request.getParameter("cmpnCd");
		String lginId = request.getParameter("lginId");
		String jspUrl = "websquare/popupMap";
		
		// ---------------------------------------------------------------------------------------------------------------
		// 지정된 url에서 접근 가능하도록 추가
		// ---------------------------------------------------------------------------------------------------------------
		String referer = StringUtils.trimToNull(request.getHeader("Referer"));

		if (StringUtils.isEmpty(referer)) {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		
		referer = referer + "map_login";

		if (referer.substring(0, 21).equals("http://gw.usegw.co.kr")
				|| referer.substring(0, 22).equals("https://211.38.140.252")
				|| referer.substring(0, 22).equals("https://gw.usegw.co.kr")
				|| referer.substring(0, 24).equals("https://gw.sgcetec.co.kr")
				|| referer.substring(0, 23).equals("https://gw.sgcenc.co.kr")
				) {

			UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
			String movePage = request.getParameter("w2xPath");
			movePage = "/ux/cw/cw1002/CW10020001U.xml";

			// session이 없을 경우 login
			if (userDetails == null || "".equals(userDetails.getUserId())) {

				Login login = new Login();

				if ("TEC".equals(cmpnCd)) {
					cmpnCd = "S0001";
				} else if ("GJE".equals(cmpnCd)) {
					cmpnCd = "S0002";
				} else if ("SMG".equals(cmpnCd)) {
					cmpnCd = "S0003";
				} else if ("SKG".equals(cmpnCd)) {
					cmpnCd = "S0004";
				}

				login.setCmpnCd(cmpnCd);
				login.setLginId(lginId);
				loginService.loginInsa(login, request);

			}

			ObjectMapper mapper = new ObjectMapper();
			String jsonInString = "";
			try {
				jsonInString = mapper.writeValueAsString(userDetails);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}

			mv.setViewName(jspUrl);
			mv.addObject("w2xPath", movePage);
			mv.addObject("userDetails", jsonInString);

		} else {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		// ---------------------------------------------------------------------------------------------------------------
		return mv;

	}
	
	/**
	 * websequare default controller
	 *
	 * @param request
	 *            the request
	 * @return model and view
	 * @throws IOException
	 * @ex http://localhost:9099/websquare/risk_login?cmpnCd=TEC&lginId=admin
	 */
	@RequestMapping(value = "/risk_login")
	@Secured(policy = SecurityPolicy.NONE)
	public ModelAndView WebSquarePopupRisk(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView mv = new ModelAndView();
		String cmpnCd = request.getParameter("cmpnCd");
		String lginId = request.getParameter("lginId");
		String jspUrl = "websquare/popupRisk";

		// ---------------------------------------------------------------------------------------------------------------
		// 지정된 url에서 접근 가능하도록 추가
		// ---------------------------------------------------------------------------------------------------------------
		String referer = StringUtils.trimToNull(request.getHeader("Referer"));

		if (StringUtils.isEmpty(referer)) {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		
		referer = referer + "risk_login";
		
		if (referer.substring(0, 21).equals("http://gw.usegw.co.kr")
				|| referer.substring(0, 22).equals("https://211.38.140.252")
				|| referer.substring(0, 22).equals("https://gw.usegw.co.kr")
				|| referer.substring(0, 24).equals("https://gw.sgcetec.co.kr")
				|| referer.substring(0, 23).equals("https://gw.sgcenc.co.kr")
				) {

			UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
			String movePage = request.getParameter("w2xPath");
			//movePage = "/ux/co/co0105/CO01050002U.xml";
			movePage = "/ux/cw/cw1400/CW14000001U.xml";

			// session이 없을 경우 login
			if (userDetails == null || "".equals(userDetails.getUserId())) {

				Login login = new Login();

				if ("TEC".equals(cmpnCd)) {
					cmpnCd = "S0001";
				} else if ("GJE".equals(cmpnCd)) {
					cmpnCd = "S0002";
				} else if ("SMG".equals(cmpnCd)) {
					cmpnCd = "S0003";
				} else if ("SKG".equals(cmpnCd)) {
					cmpnCd = "S0004";
				}

				login.setCmpnCd(cmpnCd);
				login.setLginId(lginId);
				loginService.loginInsa(login, request);

			}

			ObjectMapper mapper = new ObjectMapper();
			String jsonInString = "";
			try {
				jsonInString = mapper.writeValueAsString(userDetails);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}

			mv.setViewName(jspUrl);
			mv.addObject("w2xPath", movePage);
			mv.addObject("userDetails", jsonInString);

		} else {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		// ---------------------------------------------------------------------------------------------------------------
		return mv;

	}
    /**
     * websequare default controller
     *
     * @param request  the request
     * @return model and view
     * @ex http://localhost:9099/websquare/risk_docu
     */
    @RequestMapping(value = "/risk_docu")
    @Secured(policy = SecurityPolicy.NONE)
    public ModelAndView WebSquareRiskDocu(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        String cmpnCd = "";
    	String lginId = "";
		String jspUrl = "websquare/popupRisk";
        
        UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
        String movePage = request.getParameter("w2xPath");
        movePage = "/ux/co/co0105/CO01050002U.xml";
        
        //session이 없을 경우 login
        if(userDetails == null || "".equals(userDetails.getUserId())){
        	
        	Login login = new Login();
        	
			cmpnCd = "S0001";
			lginId = "Z99999";
			
        	login.setCmpnCd(cmpnCd);
        	login.setLginId(lginId);
        	loginService.loginInsa(login,  request);
        	
        }
        
        ObjectMapper mapper = new ObjectMapper();
        String jsonInString = "";
        try {
            jsonInString = mapper.writeValueAsString(userDetails);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        
        mv.setViewName(jspUrl);
        mv.addObject("w2xPath", movePage);
        mv.addObject("userDetails", jsonInString);
        return mv;
    }
	
	/**
	 * websequare default controller
	 *
	 * @param request
	 *            the request
	 * @return model and view
	 * @throws IOException
	 * @ex http://localhost:9099/websquare/welfare_login?cmpnCd=TEC&lginId=admin
	 */
	@RequestMapping(value = "/welfare_login")
	@Secured(policy = SecurityPolicy.NONE)
	public ModelAndView WebSquarePopupWelfare(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView mv = new ModelAndView();
		String cmpnCd = request.getParameter("cmpnCd");
		String lginId = request.getParameter("lginId");
		String jspUrl = "websquare/popupWelfare";

		// ---------------------------------------------------------------------------------------------------------------
		// 지정된 url에서 접근 가능하도록 추가
		// ---------------------------------------------------------------------------------------------------------------
		String referer = StringUtils.trimToNull(request.getHeader("Referer"));

		if (StringUtils.isEmpty(referer)) {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		
		referer = referer + "welfare_login";
		
		if (referer.substring(0, 21).equals("http://gw.usegw.co.kr")
				|| referer.substring(0, 22).equals("https://211.38.140.252")
				|| referer.substring(0, 22).equals("https://gw.usegw.co.kr")
				|| referer.substring(0, 24).equals("https://gw.sgcetec.co.kr")
				|| referer.substring(0, 23).equals("https://gw.sgcenc.co.kr")
				) {

			UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
			String movePage = request.getParameter("w2xPath");
			movePage = "/ux/cw/cw1400/CW14000001U.xml";

			// session이 없을 경우 login
			if (userDetails == null || "".equals(userDetails.getUserId())) {

				Login login = new Login();

				if ("TEC".equals(cmpnCd)) {
					cmpnCd = "S0001";
				} else if ("GJE".equals(cmpnCd)) {
					cmpnCd = "S0002";
				} else if ("SMG".equals(cmpnCd)) {
					cmpnCd = "S0003";
				} else if ("SKG".equals(cmpnCd)) {
					cmpnCd = "S0004";
				}

				login.setCmpnCd(cmpnCd);
				login.setLginId(lginId);
				loginService.loginInsa(login, request);

			}

			ObjectMapper mapper = new ObjectMapper();
			String jsonInString = "";
			try {
				jsonInString = mapper.writeValueAsString(userDetails);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}

			mv.setViewName(jspUrl);
			mv.addObject("w2xPath", movePage);
			mv.addObject("userDetails", jsonInString);

		} else {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		// ---------------------------------------------------------------------------------------------------------------
		return mv;

	}

	
	/**
	 * websequare default controller
	 *
	 * @param request
	 *            the request
	 * @return model and view
	 * @throws IOException
	 * @ex http://localhost:9099/websquare/lessonlearn_login?cmpnCd=TEC&lginId=admin
	 */
	@RequestMapping(value = "/lessonlearn_login")
	@Secured(policy = SecurityPolicy.NONE)
	public ModelAndView WebSquarePopupLessonlearn(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView mv = new ModelAndView();
		String cmpnCd = request.getParameter("cmpnCd");
		String lginId = request.getParameter("lginId");
		String jspUrl = "websquare/popupLessonlearn";

		// ---------------------------------------------------------------------------------------------------------------
		// 지정된 url에서 접근 가능하도록 추가
		// ---------------------------------------------------------------------------------------------------------------
		String referer = StringUtils.trimToNull(request.getHeader("Referer"));

		if (StringUtils.isEmpty(referer)) {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		
		referer = referer + "lessonlearn_login";
		
		if (referer.substring(0, 21).equals("http://gw.usegw.co.kr")
				|| referer.substring(0, 22).equals("https://211.38.140.252")
				|| referer.substring(0, 22).equals("https://gw.usegw.co.kr")
				|| referer.substring(0, 24).equals("https://gw.sgcetec.co.kr")
				|| referer.substring(0, 23).equals("https://gw.sgcenc.co.kr")
				) {

			UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
			String movePage = request.getParameter("w2xPath");
			movePage = "/ux/ll/ll0101/LL01010001U.xml";

			// session이 없을 경우 login
			if (userDetails == null || "".equals(userDetails.getUserId())) {

				Login login = new Login();

				if ("TEC".equals(cmpnCd)) {
					cmpnCd = "S0001";
				} else if ("GJE".equals(cmpnCd)) {
					cmpnCd = "S0002";
				} else if ("SMG".equals(cmpnCd)) {
					cmpnCd = "S0003";
				} else if ("SKG".equals(cmpnCd)) {
					cmpnCd = "S0004";
				}

				login.setCmpnCd(cmpnCd);
				login.setLginId(lginId);
				loginService.loginInsa(login, request);

			}

			ObjectMapper mapper = new ObjectMapper();
			String jsonInString = "";
			try {
				jsonInString = mapper.writeValueAsString(userDetails);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}

			mv.setViewName(jspUrl);
			mv.addObject("w2xPath", movePage);
			mv.addObject("userDetails", jsonInString);

		} else {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>location.href='referer.html';</script>");
			out.flush();
		}
		// ---------------------------------------------------------------------------------------------------------------
		return mv;

	}
	/**
	 * Web square popup model and view.
	 *
	 * @param param
	 *            the param
	 * @param request
	 *            the request
	 * @param response
	 *            the response
	 * @return the model and view
	 */
	@RequestMapping(value = "/popup")
	@Secured(policy = SecurityPolicy.NONE)
	public ModelAndView WebSquarePopup(Map param, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		String jspUrl = "websquare/popup";
		UserDetails userDetails = (UserDetails) SessionUtil.getAttribute(SgcPortalAttributes.AUTHENTICATION);
		String movePage = request.getParameter("w2xPath");
		movePage = "/ux/main/main.xml";
		// session이 없을 경우 login 화면으로 이동.
		if (userDetails == null || "".equals(userDetails.getUserId())) {
			movePage = "/ux/main/login.xml";
		}

		mv.setViewName(jspUrl);
		mv.addObject("w2xPath", movePage);
		mv.addObject("ozServerUrl", properties.getReport().getServerUrl());
		return mv;
	}
}
