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

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.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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.common.util.StringUtil;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.spring.LocaleMessage;
import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.onl.common.exception.BizException;
import com.ext.eai.agent.stdmessage.ReloadSTDMessageCommand;

@Controller
public class StdMessageManController extends BaseController {

	@Autowired
	@Qualifier("localeMessage")
	private LocaleMessage localeMessage;

	@Autowired
	private StdMessageManService service;

	@GetMapping(value = "/onl/admin/service/stdMessageMan.view")
	public String view() {
		return "/onl/admin/service/stdMessageMan";
	}

	@GetMapping(value = "/onl/admin/service/stdMessageMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/service/stdMessageManDetail";
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<StdMessageUI>> selectList(Pageable pageable, String searchBzwkSvcKeyName,
			String searchEaiSvcCode, String searchEaiSendRecv, String searchEaiDirection) {
		Page<StdMessageUI> map = service
				.selectList(pageable, searchBzwkSvcKeyName, searchEaiSvcCode, searchEaiSendRecv, searchEaiDirection);

		return ResponseEntity.ok(new GridResponse<>(map));
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=DETAIL")
	public ResponseEntity<Map<String, Object>> selectDetail(String bzwkSvcKeyName) {

		StdMessageUI stdMessageUI = service.selectDetail(bzwkSvcKeyName);
		List<ExtendedColumnDefinitionUI> keyList = service.selectDetailForKey();

		HashMap<String, Object> map = new HashMap<>();
		map.put("stdMessage", stdMessageUI);
		map.put("keys", keyList);

		return ResponseEntity.ok(map);
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> map, StdMessageUI stdMessageUI) throws Exception {

		service.insert(map, stdMessageUI);

		// 메모리 정보 Reload
		Command command = new CommonCommand(ReloadSTDMessageCommand.class.getName(), stdMessageUI.getBzwkSvcKeyName());
		agentUtilService.broadcast(command);

		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> map, StdMessageUI stdMessageUI) throws Exception {

		service.update(map, stdMessageUI);

		// 메모리 정보 Reload
		Command command = new CommonCommand(ReloadSTDMessageCommand.class.getName(), stdMessageUI.getBzwkSvcKeyName());
		agentUtilService.broadcast(command);

		return ResponseEntity.ok().build();

	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(HttpServletRequest request, HttpServletResponse response, String bzwkSvcKeyName)
			throws Exception {

		service.delete(bzwkSvcKeyName);

		// 메모리 정보 Reload(삭제 후)
		Command command = new CommonCommand("com.eactive.eai.agent.stdmessage.RemoveSTDMessageCommand", bzwkSvcKeyName);
		agentUtilService.broadcast(command);

		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=LIST_COMBO")
	public ResponseEntity<Map<String, List<ComboVo>>> selectCombo() {
		Map<String, List<ComboVo>> map = service.selectCombo();
		return ResponseEntity.ok(map);
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=LIST_DETAIL_COMBO")
	public ResponseEntity<Map<String, List<ComboVo>>> selectDetailCombo() {
		Map<String, List<ComboVo>> map = service.selectDetailCombo();
		return ResponseEntity.ok(map);
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=INITIALIZES")
	public ResponseEntity<Map<String, Object>> initializes() throws Exception {

		// 메모리 정보 Reload
		Command command = new CommonCommand(ReloadSTDMessageCommand.class.getName(), "ALL");
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);
		Map<String, Object> map = new HashMap<>();
		map.put("message", CommonUtil.getMapString(returnMap, true));
		return ResponseEntity.ok(map);
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=INITIALIZE")
	public ResponseEntity<Map<String, Object>> initialize(String bzwkSvcKeyName) throws Exception {

		// 메모리 정보 Reload
		Command command = new CommonCommand(ReloadSTDMessageCommand.class.getName(), bzwkSvcKeyName);
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);
		Map<String, Object> map = new HashMap<>();
		map.put("message", CommonUtil.getMapString(returnMap, true));
		return ResponseEntity.ok(map);
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(String searchBzwkSvcKeyName, String searchEaiSvcCode, String searchEaiSendRecv,
			String searchEaiDirection) {
		HashMap<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> datas = service
				.selectListToExcel(searchBzwkSvcKeyName, searchEaiSvcCode, searchEaiSendRecv, searchEaiDirection);
		resultMap.put("datas", datas);
		return new ModelAndView("excelView", resultMap);
	}

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=CLONETOPROD")
	public ModelAndView cloneToProd(HttpServletRequest request, @RequestParam HashMap<String, Object> paramMap)
			throws Exception {
		HashMap<String, Object> resultMap = new HashMap<>();
		String userId = SessionManager.getUserId(request);
		String cmDate = StringUtil.getNullStr(paramMap.get("CmDate"));
		String cmTime = StringUtil.getNullStr(paramMap.get("CmTime"));
		String listMsgName = StringUtil.getNullStr(paramMap.get("ListMsgName"));

		try {
			boolean ret = service.migrationToProdKbMessageService(userId, listMsgName, cmDate, cmTime);
			if (!ret) {
				throw new Exception("CM대상 자원의 운영적용에 있어 에러가 발생했습니다.");
			}
			resultMap.put("message", "적용이 완료되었습니다.");
		} catch (BizException be) {
			throw new Exception(be.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new Exception(localeMessage.getString("errors.checklog"));
		}

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

	@PostMapping(value = "/onl/admin/service/stdMessageMan.json", params = "cmd=LIST_COLUMN")
	public ResponseEntity<Map<String, Object>> selectColumnList(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		List<ExtendedColumnDefinitionUI> list = service.selectColumnList();
		map.put("list", list);
		return ResponseEntity.ok(map);
	}
}
