package com.eactive.eai.rms.bat.monitoring.count;

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

import javax.servlet.http.HttpServletRequest;

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.bat.monitoring.count.BatCountMonitorService.CountMapType;
import com.eactive.eai.rms.common.base.BatBaseAnnotationController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;

@Controller
public class BatCountMonitorController extends BatBaseAnnotationController  {

	@Autowired
	@Qualifier("batCountMonitorService")
	private BatCountMonitorService service;
	
	@RequestMapping(value= "/bat/transaction/count/countMonitorMan.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;
	}
	
	@RequestMapping(value = "/bat/transaction/count/countMonitorMan.json", params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request, @RequestParam HashMap<String, Object> paramap) throws Exception {

		Map<String, Integer> ffCountMap = service.selectCountMap(paramap, CountMapType.FF);
		Map<String, Integer> ddCountMap = service.selectCountMap(paramap, CountMapType.DD);
		Map<String, Object> allCountMap = service.combineCountMaps(new ArrayList() {{ add(ffCountMap); add(ddCountMap); }});

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("ffCountMap", ffCountMap);
		resultMap.put("ddCountMap", ddCountMap);
		resultMap.put("allCountMap", allCountMap);

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