package com.eactive.eai.rms.onl.manage.serviceinfo.header;

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.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;

@Controller
public class HostHeaderController extends BaseController {

	@Autowired
	@Qualifier("hostHeaderService")
	private HostHeaderService service;

	@Autowired
	private ComboService comboService;

	@RequestMapping(value = "/onl/admin/service/hostHeaderMan.view")
	private 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 = "/onl/admin/service/hostHeaderMan.json", params = "cmd=LIST")
	private ModelAndView selectList(HttpServletRequest request, JsonPageVo pageVo, String searchBizCode,
			String searchUbmugign) throws Exception {

		HashMap map = service.selectList(pageVo.getStartNum(), pageVo.getEndNum(), searchBizCode, searchUbmugign);

		pageVo.setTotalCount((Integer) map.get("totalCount"));

		Map resultMap = new HashMap();
		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 = "/onl/admin/service/hostHeaderMan.json", params = "cmd=LIST_DETAIL_COMBO")
	public ModelAndView selectDetailCombo() {

		List<ComboVo> listYn = comboService.getFromCode("HOST_SYS_CODE");
		List<ComboVo> listBiz = comboService.getFromBusinessType();

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("ynList", listYn);
		resultMap.put("bizRows", listBiz);

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

	@RequestMapping(value = "/onl/admin/service/hostHeaderMan.json", params = "cmd=DETAIL")
	private ModelAndView selectDetail(HttpServletRequest request, HttpServletResponse response, String columnName)
			throws Exception {

		HashMap<String, Object> map = service.selectDetail(columnName);
		ModelAndView modelAndView = new ModelAndView("jsonView", map);

		return modelAndView;
	}

	@RequestMapping(value = "/onl/admin/service/hostHeaderMan.json", params = "cmd=INSERT")
	private String insert(HttpServletRequest request, HttpServletResponse response,
			@RequestParam HashMap<String, Object> map) throws Exception {

		service.insert(map);

		CommonCommand command = new CommonCommand("com.eactive.eai.agent.header.ReloadHostHeaderCommand",
				(String) map.get("eaiBizCode"));
		agentUtilService.broadcast(command);

		return null;
	}

	@RequestMapping(value = "/onl/admin/service/hostHeaderMan.json", params = "cmd=UPDATE")
	private String update(HttpServletRequest request, HttpServletResponse response,
			@RequestParam HashMap<String, Object> map) throws Exception {

		service.update(map);

		CommonCommand command = new CommonCommand("com.eactive.eai.agent.header.ReloadHostHeaderCommand",
				(String) map.get("eaiBizCode"));
		agentUtilService.broadcast(command);

		return null;
	}

	@RequestMapping(value = "/onl/admin/service/hostHeaderMan.json", params = "cmd=DELETE")
	private String delete(HttpServletRequest request, HttpServletResponse response, String eaiBizCode)
			throws Exception {

		HashMap paramMap = new HashMap();
		paramMap.put("eaiBizCode", eaiBizCode);
		service.delete(paramMap);

		CommonCommand command = new CommonCommand("com.eactive.eai.agent.header..RemoveHostHeaderCommand", eaiBizCode);
		agentUtilService.broadcast(command);

		return null;
	}
}
