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

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.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;

@Controller
public class BapClassMapController extends BapBaseAnnotationController  {

	@Autowired
	@Qualifier("bapClassMapService")
	private BapClassMapService service;
	
	@RequestMapping(value= "/bap/admin/message/classMapMan.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/classMapMan.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/classMapMan.json",params = "cmd=DETAIL")
	public ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response, String key) throws Exception {
		// key = telgmClsID
		
		HashMap<String, Object> map = service.selectDetail(key);
		
		ModelAndView modelAndView = new ModelAndView("jsonView", map);	
		
		return modelAndView;
	}
	@RequestMapping(value= "/bap/admin/message/classMapMan.json",params = "cmd=LIST_DETAIL_COMBO")
	public ModelAndView selectDetailCombo(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> paramMap) throws Exception {
		// key = TelgmClsName
		HashMap<String, Object> map = service.selectDetailCombo(paramMap);
		
		ModelAndView modelAndView = new ModelAndView("jsonView", map);	
		
		return modelAndView;
	}	
	@RequestMapping(value= "/bap/admin/message/classMapMan.json",params = "cmd=INSERT")
	public String insert(HttpServletRequest request,
			HttpServletResponse response,@RequestParam HashMap<String,Object> map ) throws Exception {

		// 전문클래스ID 공백 제거
//		map.replace("telgmClsID",map.get("telgmClsID").toString().trim());
				
		service.insert(map);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.telegraminfo.UpdateTelegramInfoCommand", map.get("telgmClsID"));
		agentUtilService.broadcast(command);
    	return null;
		
	}	
	@RequestMapping(value= "/bap/admin/message/classMapMan.json",params = "cmd=UPDATE")
	public String update(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map ) throws Exception {
		
		service.update(map);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.telegraminfo.UpdateTelegramInfoCommand", map.get("telgmClsID"));
		agentUtilService.broadcast(command);

		return null;
		
	}
	@RequestMapping(value= "/bap/admin/message/classMapMan.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.telegraminfo.RemoveTelegramInfoCommand", map.get("telgmClsID"));
		agentUtilService.broadcast(command);
		return null;
		
	}
	@RequestMapping(value= "/bap/admin/message/classMapMan.json",params = "cmd=LIST_DELETE")
	public String deleteList(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> paramMap ) throws Exception {
		for (String telgmClsId : paramMap.get("telgmClsIdList").toString().split(",")) {
			HashMap<String, Object> paramDetail = new HashMap<String,Object>();
			paramDetail.put("telgmClsID", telgmClsId);			
			service.delete(paramDetail);
			CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.telegraminfo.RemoveTelegramInfoCommand", telgmClsId);
			agentUtilService.broadcast(command);
		}
		
		return null;
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value= "/bap/admin/message/classMapMan.json",params = "cmd=LIST_DOWNLOAD_SQL")
	public ModelAndView downloadSql(HttpServletRequest request,
		HttpServletResponse response,@RequestParam HashMap<String,Object> map) throws Exception {		

		List<Map<String, Object>> telgmClsIdList = new ArrayList<Map<String, Object>>();
		for (String searchTelgmClsId : map.get("searchTelgmClsIdList").toString().split(",")) {
			HashMap<String, Object> telgmClsMap = service.selectDetail(searchTelgmClsId);
			telgmClsIdList.add((Map<String, Object>)telgmClsMap.get("list"));
		}

		HashMap<String,Object> telgmClassMap = new HashMap<String,Object>();
		telgmClassMap.put("list", telgmClsIdList);
		telgmClassMap.put("fileName", map.get("fileName"));
		telgmClassMap.put("svrType", map.get("svrType"));
		
    	ModelAndView modelAndView = new ModelAndView("jsonView",service.mkInserSqlForClassMap(telgmClassMap));	
		
		return modelAndView;
	}
	@RequestMapping(value= "/bap/admin/message/classMapMan.view",params = "cmd=LIST_FILE_DOWNLOAD")
	public String fileDownload(HttpServletRequest request) throws Exception {
		
		return "/bap/admin/common/filedownload";
	}
}