package com.eactive.eai.rms.onl.common.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.util.CommonConstants;

/**
 * The Class AdapterMainController.
 * <ul>
 * <li>1. Function :</li>
 * <li>2. Summary :</li>
 * <li>3. Notification :</li>
 * </ul>
 * 
 */
@Controller
@RequestMapping("/main/adapter.do")
public class AdapterMainController {
    private static final Logger logger = Logger.getLogger(AdapterMainController.class);

    @ModelAttribute
    public void initServiceKey(HttpServletRequest request, String service) {
        request.getSession().setAttribute(CommonConstants.SERVICE_TYPE_KEY,
            service);        
    }

    /**
     * Sna.
     * 
     * @return the string
     */
    @RequestMapping(params = { "adapterType=SNA" })
    public String sna(HttpServletRequest request,
            @RequestParam(required = true) String service, ModelMap model) {
        model.addAttribute("mainPage", "/gfm/adapter/sna/snaDetailStatus.gfm");
        model.addAttribute("service", service);
        return "redirect:/main.do";
    }

    /**
     * Socket.
     * 
     * @return the string
     */
    @RequestMapping(params = { "adapterType=SOC" })
    public String socket(HttpServletRequest request,
            @RequestParam(required = true) String service, ModelMap model) {
        model.addAttribute("mainPage",
            "/gfm/adapter/socket/socketDetailStatus.gfm");
        model.addAttribute("service", service);
        return "redirect:/main.do";
    }

    /**
     * Socket2.
     * 
     * @return the string
     */
    @RequestMapping(params = { "adapterType=SO2" })
    public String socket2(HttpServletRequest request,
            @RequestParam(required = true) String service, ModelMap model) {
        model.addAttribute("mainPage",
            "/gfm/adapter/socket2/socketDetailStatus2.gfm");
        model.addAttribute("service", service);
        return "redirect:/main.do";
    }

    /**
     * Http.
     * 
     * @return the string
     */
    @RequestMapping(params = { "adapterType=HTT" })
    public String http(HttpServletRequest request,
            @RequestParam(required = true) String service, ModelMap model) {
        model.addAttribute("mainPage", "/gfm/adapter/http/httpDetailStatus.gfm");
        model.addAttribute("service", service);
        return "redirect:/main.do";
    }

    /**
     * Ejb.
     * 
     * @return the string
     */
    @RequestMapping(params = { "adapterType=EJB" })
    public String ejb(HttpServletRequest request,
            @RequestParam(required = true) String service, ModelMap model) {
        model.addAttribute("mainPage", "/gfm/adapter/ejb/ejbDetailStatus.gfm");
        model.addAttribute("service", service);
        return "redirect:/main.do";
    }

    /**
     * Tuxedo.
     * 
     * @return the string
     */
    @RequestMapping(params = { "adapterType=TUX" })
    public String tuxedo(HttpServletRequest request,
            @RequestParam(required = true) String service, ModelMap model) {
        model.addAttribute("mainPage",
            "/gfm/adapter/tuxedo/tuxedoDetailStatus.gfm");
        model.addAttribute("service", service);
        return "redirect:/main.do";
    }

    /**
     * Mq.
     * 
     * @return the string
     */
    @RequestMapping(params = { "adapterType=MQ" })
    public String mq(HttpServletRequest request,
            @RequestParam(required = true) String service, ModelMap model) {
        model.addAttribute("mainPage", "/gfm/adapter/mq/mqDetailStatus.gfm");
        model.addAttribute("service", service);
        return "redirect:/main.do";
    }

    /**
     * Mq.
     * 
     * @return the string
     */
    @RequestMapping(params = { "adapterType=TRANSACTION" })
    public String transaction(HttpServletRequest request,
            @RequestParam(required = true) String serviceType, ModelMap model) {
        model.addAttribute("mainPage", request.getContextPath() + 
    		"/onl/transaction/online/transactionStatus.view");
        model.addAttribute("serviceType", serviceType);
        return "redirect:/main.do"; 
    }

    
    /**
     * 룰 등록 가이드 화면으로 이동한다.
     * @param request
     * @param service
     * @param model
     * @return
     */
    @RequestMapping(params = { "adapterType=RULE" })
    public String rule(HttpServletRequest request,
            @RequestParam(required = true) String service, ModelMap model) {
        model.addAttribute("mainPage", request.getContextPath() + 
            "/gfm/admin/rule/transformManual.gfm");
        model.addAttribute("service", service);
        return "redirect:/main.do";
    }

    /**
     * SOCKET 상태 현황 가이드 화면으로 이동한다.
     * @param request
     * @param service
     * @param model
     * @return
     */
    @RequestMapping(params = { "adapterType=STATUS" })
    public String status(HttpServletRequest request,
            @RequestParam(required = true) String serviceType, 
            String mod, ModelMap model) {
        model.addAttribute("mainPage", request.getContextPath() + 
        		"/onl/adapter/socket/socketStatus.view");
        if(mod != null)
        {
        	model.addAttribute("mod", mod);
        }
        model.addAttribute("serviceType", serviceType);
        return "redirect:/main.do";
    }
    /**
     * 거래추적 화면으로 이동한다.
     * @param request
     * @param bwk
     * @param svcName
     * @param model
     * @return
     */
    @RequestMapping(params = { "adapterType=TRACKING" })
    public String tracking(HttpServletRequest request,
            @RequestParam(required = true) String bwk, String svcName,
            ModelMap model) {

    	String serviceType = DataSourceContextHolder.getDataSourceType().getName();
        
        // 거래처리 화면에서 더블클릭시 연결된 거래로그 화면을 db로그 화면으로 교체함.
        // 온라인 적용은 F/W 이 선적용 되어야 함.
        String mainPage    = request.getContextPath() + "/gfm/transaction/tracking/transactionTrackingDB.gfm";
        
        // 온라인 상에서 개발자들에게 조회 화면을 열어주기 위해서 아이디 체크 로직 추가함.
        // ( 역할그룹명 에 deve 가 포함되어 있으면, 거래추적 화면 대신  에러추적 화면으로 이동한다.
       // List roleIds = SessionManager.getRoleId(request);
        
        //TODO JUN's Role 관련 쓰이는데가 없음 
//        if( roleIds == null || roleIds.size() ==0 ) {
//        	roleId = "" ;
//        }


        if (logger.isDebugEnabled()) {
            logger.debug("Go to TRACKING Screen !" );
            logger.debug("serviceType=" + serviceType );
            logger.debug("bwk=" + bwk );
            logger.debug("svcName=" + svcName );
            logger.debug("mainPage=" + mainPage );
        }
        
        // 파라미터 를 세션에 저장해 두고 거래추적에서 이를 읽어 default 로 사용한다.
        request.getSession().setAttribute("tracking.default.bwk", bwk);
        request.getSession().setAttribute("tracking.default.svcName", svcName);

        model.addAttribute("mainPage", mainPage );
        model.addAttribute("service", serviceType);
        return "redirect:/main.do";
    }

}
