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

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.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
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.CommonUtil;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.eaimsg.EAIMessageUISearch;
import com.eactive.eai.rms.onl.manage.serviceinfo.eaimsg.ui.EAIMessageUI;

@Controller
public class EaiMsgDevController extends BaseController {

	/** The service info service. */
	@Autowired
	private EaiMsgManService service;

	@Autowired
	@Qualifier("comboService")
	private ComboService comboService;

	@RequestMapping(value = "/onl/admin/service/eaiMsgDevMan.view")
	private String viewList(HttpServletRequest request, @RequestParam(value = "cmd", required = false) String cmd)
			throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())) {
			cmd = "";
		}
		String path = request.getServletPath();
		return path.split("[.]")[0] + cmd;
	}

	@RequestMapping(value = "/onl/admin/service/devTranServiceMan.view")
	private String viewList2(HttpServletRequest request, @RequestParam(value = "cmd", required = false) String cmd)
			throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())) {
			cmd = "";
		}
		String path = request.getServletPath();
		return path.split("[.]")[0] + cmd;
	}

	/**
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/onl/admin/service/eaiMsgDevMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<EAIMessageUI>> selectList(EAIMessageUISearch eaiMessageUISearch,
			Pageable pageable, String sortname,  String sortorder) {
		Page<EAIMessageUI> page = service.selectList(eaiMessageUISearch, pageable, sortname, sortorder);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

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

		String message = "";
		int resultCnt = 0;

		String tranType = StringUtils.defaultIfBlank((String)map.get("tranType"), "");
		String eaiSvcNameParam = (String) map.get("eaiSvcName");
		String[] eaiSvcNameArr = eaiSvcNameParam.split(",");

		for (String eaiSvcName : eaiSvcNameArr) {
			resultCnt++;
			map.put("eaiSvcName", eaiSvcName);
			service.updateTranType(eaiSvcName, tranType);
		}

		// Reload
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.eaimessage.ReloadEAIMessageCommand",
				eaiSvcNameParam);
		agentUtilService.broadcast(command);

		message = "총" + resultCnt + "건의 서비스를 수정하였습니다";
		Map resultMap = new HashMap();
		resultMap.put("message", message);
		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;

	}

	@RequestMapping(value = "/onl/admin/service/eaiMsgDevMan.json", params = "cmd=LIST_INIT_COMBO")
	public ModelAndView initCombo(HttpServletRequest request, HttpServletResponse response) throws Exception {

		List<ComboVo> listTranType = comboService.getFromCode("RS_TRAN_TYPE");// 거래타입
		Map resultMap = new HashMap();
		resultMap.put("tranTypeRows", listTranType);

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

	@RequestMapping(value = "/onl/admin/service/eaiMsgDevMan.json", params = "cmd=LIST_INIT")
	private ModelAndView listInitCombo(HttpServletRequest request, HttpServletResponse response, String codeGroupArr)
			throws Exception {

		List<String> codeGroupList = new ArrayList<String>();
		if (codeGroupArr != null) { // 취약점소스 수정 2020.08.27
			codeGroupArr.split(",");

			for (String codeName : codeGroupArr.split(",")) {
				codeGroupList.add(codeName);
			}
		}

		List<Map<String, Object>> listCombo = comboService.getFromCodeByGroups(codeGroupList);
		List<ComboVo> listFlowControl = comboService.getFromRouting("F"); // Flow Control 라우팅명

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

		resultMap.put("list", listCombo);
		resultMap.put("listFlowControl", listFlowControl);

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

	@RequestMapping(value = "/onl/admin/service/eaiMsgDevMan.json", params = "cmd=INITIALIZES")
	public ModelAndView initializes(HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.eaimessage.ReloadEAIMessageCommand", "ALL");

		HashMap returnMap = agentUtilService.broadcast(command);

		HashMap map = new HashMap();
		map.put("message", CommonUtil.getMapString(returnMap, true));

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

	}
}
