package com.eactive.eai.rms.onl.manage.adapter.messagekey;

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.data.domain.Sort;
import org.springframework.data.web.SortDefault;
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.servlet.ModelAndView;

import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.common.vo.SimpleResponse;

import lombok.NonNull;

@Controller
public class MessageKeyController extends OnlBaseAnnotationController {

	private MessageKeyManService messageKeyManService;

	private ComboService comboService;

	@Autowired
	public MessageKeyController(@NonNull MessageKeyManService messageKeyManService,
			@NonNull ComboService comboService) {
		this.messageKeyManService = messageKeyManService;
		this.comboService = comboService;
	}

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

	@GetMapping(value = "/onl/admin/adapter/messageKeyMan.view", params = "cmd=DETAIL")
	public String viewDetail() {
		return "/onl/admin/adapter/messageKeyManDetail";
	}

	@GetMapping(value = "/onl/admin/adapter/messageKeyMan.view", params = "cmd=NEW")
	public String viewNew() {
		return "/onl/admin/adapter/messageKeyManNew";
	}

	@PostMapping(value = "/onl/admin/adapter/messageKeyMan.json", params = "cmd=LIST_GROUP")
	public ResponseEntity<GridResponse<MessageKeyUI>> selectListNew(@SortDefault("id") Pageable pageable,
			String searchAdptrBzwkGroupName, String searchIoDstcd) {

		Page<MessageKeyUI> page = messageKeyManService.selectList(pageable, searchAdptrBzwkGroupName, searchIoDstcd);
		return ResponseEntity.ok(new GridResponse<>(page));

	}

	@PostMapping(value = "/onl/admin/adapter/messageKeyMan.json", params = "cmd=DETAIL")
	public ResponseEntity<GridResponse<MessageKeyUI>> selectDetail(String adptrBzwkGroupName, String msgDstcd,
			String ioDstcd, Integer groupInorNo) {

		List<MessageKeyUI> list = messageKeyManService.selectDetail(adptrBzwkGroupName, msgDstcd, ioDstcd, groupInorNo);
		return ResponseEntity.ok(new GridResponse<>(list));
	}

	@PostMapping(value = "/onl/admin/adapter/messageKeyMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(MessageKeyUI messageKeyUI) {
		long cnt = messageKeyManService.delete(messageKeyUI);
		String key = messageKeyUI.getAdptrBzwkGroupName() + "," + messageKeyUI.getIoDstcd();

		if (cnt == 0) {
			CommonCommand
					.builder()
					.name(CommonCommand.REMOVE_MESSAGE_KEY_COMMAND)
					.args(key)
					.build()
					.broadcast(agentUtilService);
		} else if (cnt > 0) {
			CommonCommand
					.builder()
					.name(CommonCommand.RELOAD_MESSAGE_KEY_COMMAND)
					.args(key)
					.build()
					.broadcast(agentUtilService);
		}
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/adapter/messageKeyMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(MessageKeyUI messageKeyUI) {
		messageKeyManService.insert(messageKeyUI);
		String key = messageKeyUI.getAdptrBzwkGroupName() + "," + messageKeyUI.getIoDstcd();
		CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_MESSAGE_KEY_COMMAND)
				.args(key)
				.build()
				.broadcast(agentUtilService);

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

	@PostMapping(value = "/onl/admin/adapter/messageKeyMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(MessageKeyUI messageKeyUI) {

		messageKeyManService.update(messageKeyUI);
		String key = messageKeyUI.getAdptrBzwkGroupName() + "," + messageKeyUI.getIoDstcd();

		CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_MESSAGE_KEY_COMMAND)
				.args(key)
				.build()
				.broadcast(agentUtilService);

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

	@PostMapping(value = "/onl/admin/adapter/messageKeyMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo(HttpServletRequest request, HttpServletResponse response,
			String eaiSvcName) {

		List<ComboVo> listAdapter = comboService.getFromAdapterGroup();
		List<ComboVo> listMsgTypes = comboService.getFromCode("MSG_TYPES");
		List<ComboVo> listExrTypes = comboService.getFromCode("EXR_TYPES");
		List<ComboVo> listSevrTypes = comboService.getFromCode("INOUT_TYPE");

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("adapterRows", listAdapter);
		resultMap.put("msgTypeRows", listMsgTypes);
		resultMap.put("exrTypeRows", listExrTypes);
		resultMap.put("sevrTypeRows", listSevrTypes);

		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/admin/adapter/messageKeyMan.json", params = "cmd=INITIALIZES")
	public ResponseEntity<SimpleResponse> initializes(HttpServletRequest request, HttpServletResponse response) {
		String message = CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_MESSAGE_KEY_COMMAND)
				.args("ALL")
				.build()
				.broadcast(agentUtilService)
				.getMessage(true);

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

	@PostMapping(value = "/onl/admin/adapter/messageKeyMan.json", params = "cmd=INITIALIZE")
	public ResponseEntity<SimpleResponse> initialize(HttpServletRequest request, HttpServletResponse response,
			String adptrBzwkGroupName, String ioDstcd) {
		String key = adptrBzwkGroupName + "," + ioDstcd;
		String message = CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_MESSAGE_KEY_COMMAND)
				.args(key)
				.build()
				.broadcast(agentUtilService)
				.getMessage(true);

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

	@PostMapping(value = "/onl/admin/adapter/messageKeyMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(@SortDefault("id") Sort pageable, String searchAdptrBzwkGroupName,
			String searchIoDstcd) {

		HashMap<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> datas = messageKeyManService
				.selectListToExcel(searchAdptrBzwkGroupName, searchIoDstcd);
		resultMap.put("datas", datas);

		return new ModelAndView("excelView", resultMap);

	}

}
