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

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.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.CommonCommand;
import com.eactive.eai.common.util.DatetimeUtil;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.common.vo.SimpleResponse;
import com.eactive.eai.rms.onl.manage.serviceinfo.subdefault.SubExtendedColumnDefinitionUI;

@Controller
public class SubMessageManController extends BaseController {

	private SubMessageService subMessageService;

	@Autowired
	public SubMessageManController(SubMessageService service) {
		this.subMessageService = service;
	}

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

	@GetMapping(value = "/onl/admin/service/subMessageMan.view", params = "cmd=DETAIL")
	public String viewDetail() {
		return "/onl/admin/service/subMessageManDetail";
	}

	@PostMapping(value = "/onl/admin/service/subMessageMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<SubMessageUI>> selectList(Pageable pageable, String searchBzwkSvcKeyName) {
		Page<SubMessageUI> page = subMessageService.selectList(pageable, searchBzwkSvcKeyName);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/service/subMessageMan.json", params = "cmd=DETAIL")
	public ResponseEntity<SubMessageUI> selectDetail(String bzwkSvcKeyName) {
		SubMessageUI subMessageUI = subMessageService.selectDetail(bzwkSvcKeyName);
		List<SubExtendedColumnDefinitionUI> keyList = subMessageService.selectDetailForKey();
		subMessageUI.setSubExtendedColumnDefinitionUIs(keyList);
		return ResponseEntity.ok(subMessageUI);
	}

	@PostMapping(value = "/onl/admin/service/subMessageMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(@RequestParam Map<String, Object> map, SubMessageUI subMessageUI) {
		subMessageService.insert(map, subMessageUI);

		// 메모리 정보 Reload
		CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_SUB_MESSAGE_COMMAND)
				.args(subMessageUI.getBzwkSvcKeyName())
				.build()
				.broadcast(agentUtilService);

		return ResponseEntity.ok().build();

	}

	@PostMapping(value = "/onl/admin/service/subMessageMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(@RequestParam Map<String, Object> map, SubMessageUI subMessageUI) {

		map.put("modfimgtstusdstcd", "1");
		map.put("eailastamndyms", DatetimeUtil.getCurrentTimeMillis());

		subMessageService.update(map, subMessageUI);

		// 메모리 정보 Reload
		CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_SUB_MESSAGE_COMMAND)
				.args(map.get("bzwkSvcKeyName"))
				.build()
				.broadcast(agentUtilService);

		return ResponseEntity.ok().build();

	}

	@PostMapping(value = "/onl/admin/service/subMessageMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String bzwkSvcKeyName) {

		subMessageService.delete(bzwkSvcKeyName);

		// 메모리 정보 Reload(삭제 후)
		CommonCommand
				.builder()
				.name(CommonCommand.REMOVE_SUB_MESSAGE_COMMAND)
				.args(bzwkSvcKeyName)
				.build()
				.broadcast(agentUtilService);

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

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

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

	@PostMapping(value = "/onl/admin/service/subMessageMan.json", params = "cmd=CLONE")
	public String clone(HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> map,
			String bzwkSvcKeyNameClone) throws Exception {
		subMessageService.clone(map, bzwkSvcKeyNameClone);
		// 메모리 정보 Reload

		CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_SUB_MESSAGE_COMMAND)
				.args(bzwkSvcKeyNameClone)
				.build()
				.broadcast(agentUtilService);

		return null;
	}

	@PostMapping(value = "/onl/admin/service/subMessageMan.json", params = "cmd=INITIALIZES")
	public ResponseEntity<SimpleResponse> initializes() {

		// 메모리 정보 Reload
		String message = CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_SUB_MESSAGE_COMMAND)
				.args("ALL")
				.build()
				.broadcast(agentUtilService)
				.getMessage(true);

		return ResponseEntity.ok(SimpleResponse.of(message));
	}

	@PostMapping(value = "/onl/admin/service/subMessageMan.json", params = "cmd=INITIALIZE")
	public ResponseEntity<SimpleResponse> initialize(String bzwkSvcKeyName) {

		// 메모리 정보 Reload
		String message = CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_SUB_MESSAGE_COMMAND)
				.args(bzwkSvcKeyName)
				.build()
				.broadcast(agentUtilService)
				.getMessage(true);

		return ResponseEntity.ok(SimpleResponse.of(message));
	}

	@PostMapping(value = "/onl/admin/service/subMessageMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(String bzwkSvcKeyName) {
		HashMap<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> list = subMessageService.selectListToExcel(bzwkSvcKeyName);

		resultMap.put("datas", list);
		return new ModelAndView("excelView", resultMap);
	}

	@PostMapping(value = "/onl/admin/service/subMessageMan.json", params = "cmd=LIST_COLUMN")
	public ModelAndView selectColumnList() {

		Map<String, Object> map = new HashMap<>();
		List list = subMessageService.selectColumnList();
		map.put("list", list);
		ModelAndView modelAndView = new ModelAndView("jsonView", map);
		return modelAndView;
	}
}
