package com.eactive.eai.rms.bap.manage.message.flowrule;

import java.util.HashMap;
import java.util.Map;

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

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.agent.command.CommonCommand;
import com.eactive.eai.rms.common.base.BapBaseAnnotationController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
import com.google.gson.Gson;

@Controller
public class BapFlowRuleController extends BapBaseAnnotationController  {

	@Autowired
	@Qualifier("bapFlowRuleService")
	private BapFlowRuleService service;
	
	@RequestMapping(value= "/bap/admin/message/flowRuleMan.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/admin/message/flowRuleMan.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;
		
	}

	@RequestMapping(value= "/bap/admin/message/flowRuleMan.json",params = "cmd=DETAIL")
	public ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response, String key) throws Exception {

		HashMap<String, Object> map = service.selectDetail(key);
		ModelAndView modelAndView = new ModelAndView("jsonView", map);	
		
		return modelAndView;
	}
	@RequestMapping(value= "/bap/admin/message/flowRuleMan.json",params = "cmd=INSERT")
	public String insert(HttpServletRequest request,
			HttpServletResponse response,@RequestParam HashMap<String,Object> map,String gridData ) throws Exception {

		String userid = com.eactive.eai.rms.common.login.SessionManager.getUserId(request);
		map.put("userid", userid );
    	
		Gson gson = new Gson();
    	@SuppressWarnings("unchecked")
		HashMap<String, Object>[] list = (HashMap<String, Object>[])gson.fromJson(gridData, HashMap[].class);
    	service.insert(map,list);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.phaseinfo.UpdatePhaseInfoCommand", map.get("bjobBzwkPrcssDstcd"));
		agentUtilService.broadcast(command);

    	return null;
		
	}	
	@RequestMapping(value= "/bap/admin/message/flowRuleMan.json",params = "cmd=UPDATE")
	public String update(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map, String gridData ) throws Exception {
		
		String userid = com.eactive.eai.rms.common.login.SessionManager.getUserId(request);
		map.put("userid", userid );

		Gson gson = new Gson();
		@SuppressWarnings("unchecked")
		HashMap<String, Object>[] list = (HashMap<String, Object>[])gson.fromJson(gridData, HashMap[].class);

    	service.update(map,list);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.phaseinfo.UpdatePhaseInfoCommand", map.get("bjobBzwkPrcssDstcd"));
		agentUtilService.broadcast(command);

		return null;
		
	}
	@RequestMapping(value= "/bap/admin/message/flowRuleMan.json",params = "cmd=DELETE")
	public String delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map ) throws Exception {
		
		service.delete(map);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.phaseinfo.RemovePhaseInfoCommand", map.get("bjobBzwkPrcssDstcd"));
		agentUtilService.broadcast(command);
		return null;	
	}
	
	@RequestMapping(value= "/bap/admin/message/flowRuleMan.json",params = "cmd=LIST_POP_COMBO")
	public ModelAndView selectDetailCombo(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> paramMap) throws Exception {

		HashMap<String, Object> map = service.selectPopCombo(paramMap);
		ModelAndView modelAndView = new ModelAndView("jsonView", map);	
		
		return modelAndView;
	}
	
	@RequestMapping(value= "/bap/admin/message/flowRuleMan.json", params = "cmd=LIST_SESSION")
	public String putInSession(HttpServletRequest request,
			JsonPageVo pageVo , @RequestParam HashMap<String, Object> paramMap) throws Exception {
		
		HttpSession session = request.getSession();
		String searchbjobBzwkPrcssDstcd = (String)paramMap.get("searchbjobBzwkPrcssDstcd");
		if (searchbjobBzwkPrcssDstcd != null) {
			session.setAttribute("searchbjobBzwkPrcssDstcd", searchbjobBzwkPrcssDstcd);
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value= "/bap/admin/message/flowRuleMan.excel",params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(HttpServletRequest request, HttpServletResponse response, String gridData, String header, String headerTitle, String fileName, String merge) throws Exception {
		HashMap<String,Object> resultMap = new HashMap<String,Object>();

    	Gson gson = new Gson();
		HashMap<String, Object>[] list = gson.fromJson(gridData, HashMap[].class);
		
		String[] headers = gson.fromJson(header, String[].class);
		String[] headerTitles = gson.fromJson(headerTitle, String[].class);
		
		HashMap<String, Object>[] lmerge = null; 
		if (merge !=null){
			lmerge = gson.fromJson(merge, HashMap[].class);
		}
    	
    	resultMap.put("headers", headers);
    	resultMap.put("headerTitles", headerTitles);
    	resultMap.put("datas", list);
    	resultMap.put("fileName", fileName);
    	resultMap.put("merge", lmerge);
		ModelAndView modelAndView = new ModelAndView("excelView",resultMap);	
        
		return modelAndView;
    }

	@RequestMapping(value= "/bap/admin/message/flowRuleMan.json",params = "cmd=LIST_DOWNLOAD_SQL")
	public ModelAndView downloadSql(HttpServletRequest request,
		HttpServletResponse response,@RequestParam HashMap<String,Object> map) throws Exception {		

		String key = map.get("bjobBzwkPrcssDstcd").toString(); 
		HashMap<String,Object> flowStageInfo = service.selectDetail(key);
		flowStageInfo.putAll(map);

    	ModelAndView modelAndView = new ModelAndView("jsonView",service.mkInserSqlForFlowRule(flowStageInfo));	
		
		return modelAndView;
	}
	
	@RequestMapping(value= "/bap/admin/message/flowRuleMan.view",params = "cmd=LIST_FILE_DOWNLOAD")
	public String fileDownload(HttpServletRequest request) throws Exception {
		
		return "/bap/admin/common/filedownload";
	}
}