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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

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.util.ControllerValidationHelper;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.flowcontrol.jms.QueueMonitorSearch;
import com.google.gson.Gson;

/**
 * JMS Queue 관리 컨트롤러
 */
@Controller
public class JmsQueueManController extends OnlBaseAnnotationController {

	@Autowired
	private JmsQueueManService service;

	@Autowired
	private ComboService comboService;

	@RequestMapping(value = "/onl/admin/service/jmsQueueMan.view")
	public void viewList() {
	}

	@RequestMapping(value = "/onl/admin/service/jmsQueueMan.view", params = "cmd=DETAIL")
	public String detaiView() {
		return "/onl/admin/service/jmsQueueManDetail";
	}

	@RequestMapping(value = "/onl/admin/service/jmsQueueMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<JmsQueueManUI>> selectList(HttpServletRequest request,
			@SortDefault.SortDefaults({ @SortDefault(sort = "id.nonsyncztranquename", direction = Sort.Direction.ASC),
					@SortDefault(sort = "id.eaisevrinstncname", direction = Sort.Direction.ASC) }) Pageable pageable,
			QueueMonitorSearch uiSearch) throws Exception {
		Page<JmsQueueManUI> pageUI = service.selectList(pageable, uiSearch);
		return ResponseEntity.ok(new GridResponse<JmsQueueManUI>(pageUI));
	}

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

		List<ComboVo> listUseYn = comboService.getFromCode("USE_YN");
		List<ComboVo> listSendReceiveSts = comboService.getFromCode("SEND_RECEIVE_STS_CD");

		Map<String, List<ComboVo>> resultMap = new HashMap<>();
		resultMap.put("useYnRows", listUseYn);
		resultMap.put("sendReceiveStsRows", listSendReceiveSts);

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

	@RequestMapping(value = "/onl/admin/service/jmsQueueMan.json", params = "cmd=DETAIL")
	private ResponseEntity<?> selectDetail(HttpServletRequest request, HttpServletResponse response,
			String nonSynczTranQueName, String sevrInstncName) throws Exception {

		JmsQueueManUI ui = service.selectDetail(nonSynczTranQueName, sevrInstncName);
		return ResponseEntity.ok(ui);
	}

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

		List<ComboVo> listBiz = comboService.getFromBusinessType();
		List<ComboVo> listInstance = comboService.getFromEAIServerWithoutAll();

		List<ComboVo> listUseYn = comboService.getFromCode("USE_YN");
		List<ComboVo> listSendReceiveSts = comboService.getFromCode("SEND_RECEIVE_STS_CD");

		Map<String, List<ComboVo>> resultMap = new HashMap<>();
		resultMap.put("bizRows", listBiz);
		resultMap.put("instanceRows", listInstance);
		resultMap.put("useYnRows", listUseYn);
		resultMap.put("sendReceiveStsRows", listSendReceiveSts);

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

	@RequestMapping(value = "/onl/admin/service/jmsQueueMan.json", params = "cmd=INSERT")
	public ResponseEntity<?> insert(HttpServletRequest request, HttpServletResponse response, @Valid JmsQueueManUI ui,
			BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors())
			return ControllerValidationHelper.checkValidation(bindingResult);

		service.insert(ui);
		return ResponseEntity.ok().build();
	}

	@RequestMapping(value = "/onl/admin/service/jmsQueueMan.json", params = "cmd=UPDATE")
	public ResponseEntity<?> update(HttpServletRequest request, HttpServletResponse response, @Valid JmsQueueManUI ui,
			BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors())
			return ControllerValidationHelper.checkValidation(bindingResult);

		service.update(ui);
		return ResponseEntity.ok().build();

	}

	@RequestMapping(value = "/onl/admin/service/jmsQueueMan.json", params = "cmd=DELETE")
	public ResponseEntity<?> delete(HttpServletRequest request, HttpServletResponse response, JmsQueueManUI ui)
			throws Exception {

		service.delete(ui.getNonSynczTranQueName(), ui.getSevrInstncName());
		return ResponseEntity.ok().build();
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/onl/admin/service/jmsQueueMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(HttpServletRequest request, HttpServletResponse response,
			QueueMonitorSearch uiSearch, @RequestParam HashMap<String, Object> paramMap) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();

		String headersStr = (String) paramMap.get("headersStr");
		headersStr = headersStr.substring(0, headersStr.length() - 1);
		paramMap.put("headersStr", headersStr);

		Gson gson = new Gson();
		Map<String, List<?>> list = service.selectListToExcel(uiSearch);

		String jsonStr = gson.toJson(list.get("list"));
		HashMap<String, Object>[] list2 = gson.fromJson(jsonStr, HashMap[].class);

		String[] headers = gson.fromJson((String) paramMap.get("header"), String[].class);
		String[] headerTitles = gson.fromJson((String) paramMap.get("headerTitle"), String[].class);

		HashMap<String, Object>[] lmerge = null;
		String merge = (String) paramMap.get("merge");
		if (merge != null) {
			lmerge = gson.fromJson(merge, HashMap[].class);
		}

		resultMap.put("headers", headers);
		resultMap.put("headerTitles", headerTitles);
		resultMap.put("datas", list2);
		resultMap.put("fileName", paramMap.get("fileName"));
		resultMap.put("merge", lmerge);
		return new ModelAndView("excelView", resultMap);
	}

}
