package com.lemon.lifecenter.common;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

public class LifeCenterController {
    public ModelAndView setMV(String viewPath) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        HttpServletRequest request = getRequest();
        ModelAndView mv = new ModelAndView();

        String url = request.getRequestURI().toString();
        String[] split = url.split("/");
        String pathType = "";

        if (split.length > 0) {
            pathType = split[1].toLowerCase();
            System.out.println("pathType : " + pathType);
        }

        hashMap.put("_TITLE", "생활치료센터 의료진 시스템");
        hashMap.put("_INCLUDE", "/WEB-INF/jsp/include");
        hashMap.put("_MENUPATH", pathType);

        try {
          String sesId         = LifeCenterSessionController.getSession( request, "sesId" );
          String sesName       = LifeCenterSessionController.getSession( request, "sesName" );
          String sesCenterCode = LifeCenterSessionController.getSession( request, "sesCenterCode" );
          String sesGroupIdx   = LifeCenterSessionController.getSession( request, "sesGroupIdx" );

          hashMap.put( "_SES_ID", sesId );
          hashMap.put( "_SES_NAME", sesName );
          hashMap.put( "_SES_CENTER_CODE", sesCenterCode );
          hashMap.put( "_SES_GROUP_IDX", sesGroupIdx );
          
        } catch (NullPointerException e) {
          e.printStackTrace();
          
        } catch (Exception e) {
          e.printStackTrace();
          
        }

        mv.addObject( "data", hashMap );
        mv.setViewName( viewPath );

        return mv;
    }

    public HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttribute = (ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes();
        return servletRequestAttribute.getRequest();
    }
}
