package com.eactive.eai.rms.bap.tansaction.messageProcReq;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.rms.bap.manage.work.message.BapMessageProcSqlService;
import com.eactive.eai.rms.common.base.BapBaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.combo.IbatisComboService;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
import com.eactive.fep.batch.client.FEPClient;
@Controller
public class BapMessageReqController extends BapBaseAnnotationController  {

	@Autowired
	@Qualifier("bapMessageReqService")
	private BapMessageReqService service;
	
	@Autowired
	private BapMessageProcSqlService sqlService;

	@Autowired
	private ComboService comboService;
	
    @Autowired
    private IbatisComboService ibatisComboService;  

	@RequestMapping(value= "/bap/transaction/messageReqMan.view")
	public String viewList(HttpServletRequest request, @RequestParam(value = "cmd", required = false ) String cmd ) throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())){
			cmd = "";
		}else {
			cmd = CamelCaseUtil.converCamelCase(cmd);
		}
		String path = request.getServletPath();
		return path.split("[.]")[0]+cmd;
	}
	
	/**
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value= "/bap/transaction/messageReqMan.json", params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request,
			JsonPageVo pageVo , @RequestParam HashMap<String, Object> paramMap) throws Exception {
		
		HashMap<String, Object> map = service.selectList( pageVo.getStartNum(), pageVo.getEndNum(), paramMap);
		
		pageVo.setTotalCount((Integer)map.get("totalCount"));
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("total"  , pageVo.getTotal());  //전체 페이지수
		resultMap.put("page"   , pageVo.getPage());   //현재 페이지수
		resultMap.put("records", pageVo.getTotalCount());//전체 레코드건수	
		resultMap.put("rows"   , map.get("list"));
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
		
	}

	/**
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value= "/bap/transaction/messageReqMan.json", params = "cmd=TRANSACTION_RESEND")
	public ModelAndView resend(HttpServletRequest request, String bjobDmndMsgID, String sndRecvFileName) throws Exception
	{
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String systemMode = System.getProperty("eai.systemmode");
		String message="";
		FEPClient fepClient;
		switch(systemMode) {
		case "D":
			message="개발계에서는 사용할수 없습니다.";
			break;
		
		case "S":
			fepClient = new FEPClient("http://tfepbt-vip.pobis.kr:20080");
			message = fepClient.fepBatchCall("T", bjobDmndMsgID, sndRecvFileName);
			break;
			
		case "P":
			fepClient = new FEPClient("http://pfepbt-vip.pobis.kr:20080");
			message = fepClient.fepBatchCall("P", bjobDmndMsgID, sndRecvFileName);
			break;	
		}
		
		returnMap.put("result_resend", message);
		ModelAndView modelAndView = new ModelAndView("jsonView",returnMap);		
		return modelAndView;
	}
		
	@SuppressWarnings("unchecked")
	@RequestMapping(value= "/bap/transaction/messageReqMan.json",params = "cmd=LIST_INIT_COMBO")
    public ModelAndView initGroupCoCdCombo( HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	
		List<ComboVo> groupCoCd      = comboService.getFromCode("EAIGROUPCOCD");		
		List<Map<String, Object>> bjobBzwkDstcd  = ibatisComboService.selectListComboForTableWithCode("TSEAIBJ02", "BJOBBZWKDSTCD", "BJOBBZWKNAME");		
		List<Map<String, Object>> sysCd          = ibatisComboService.selectListComboForTableWithCode("TSEAIBJ11", "SYSCD", "SYSNAME");		
		List<Map<String, Object>> uapplCd        = ibatisComboService.selectListComboForTableWithCode("TSEAICM01", "TRIM(EAIBZWKDSTCD)", "BZWKDSTICNAME");
		List<ComboVo> useYn     = comboService.getFromCode("USE_YN");
		List<Map<String, String>> sndScriptName  = new ArrayList<Map<String, String>>();
		
//		CommonCommand command = new CommonCommand("com.eactive.eai.agent.makeDir.GetScriptListCommand","");
//		Map<String, Object> returnMap = agentUtilService.broadcast(command);
//		Iterator<String> iter = returnMap.keySet().iterator();
//		String svrName = iter.next();
//
//		ArrayList<String> sndScriptNameList = new ArrayList<String>();
//		if (returnMap != null && returnMap.get(svrName) instanceof List) {
//			sndScriptNameList = (ArrayList<String>)returnMap.get(svrName);
//			for (String sndScriptNameItem : sndScriptNameList) {
//				HashMap<String, String> map = new HashMap<String, String>();
//				String item = sndScriptNameItem.replaceAll("\\\\", "/");
//				map.put( "CODE", item);
//				map.put( "NAME", item);
//				sndScriptName.add(map);
//			}
//		}

		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put("bjobBzwkDstcd" , bjobBzwkDstcd);
		resultMap.put("groupCoCd"     , groupCoCd);
		resultMap.put("sysCd"         , sysCd);
		resultMap.put("uapplCd"       , uapplCd);
		resultMap.put("sndScriptName" , sndScriptName);
		resultMap.put("useYn"         	  , useYn);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;                
    }     

	@RequestMapping(value= "/bap/transaction/messageReqMan.json",params = "cmd=LIST_PROP")
    public ModelAndView getProps( HttpServletRequest request,
            /*HttpServletResponse response, @RequestParam  String bjobBzwkDstcd) throws Exception {*/
		HttpServletResponse response, @RequestParam  String bjobBzwkDstcd, String bjobTranDstcdName) throws Exception {
	    	
		logger.debug("bjobBzwkDstcd--->" + bjobBzwkDstcd);
		logger.debug("bjobTranDstcdName--->" + bjobTranDstcdName);
		
		String propGroupName = "";
		if (bjobBzwkDstcd != null && bjobTranDstcdName != null) {
			propGroupName = bjobTranDstcdName.isEmpty() ? "TelegramInfo{" + bjobBzwkDstcd + "}" : "TelegramInfo{" + bjobBzwkDstcd + "_" + bjobTranDstcdName + "}";
		}
		HashMap<String, Object> map = service.getProps(bjobBzwkDstcd, propGroupName);		

		ModelAndView modelAndView = new ModelAndView("jsonView", map);	
		
		return modelAndView;
    }     
}