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

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

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.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.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.eaimsg.RoutingEaiMsgSearch;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class RoutingEaiMsgController extends BaseController {

	private RoutingEaiMsgManService routingEaiMsgManService;

	private ComboService comboService;

	private ObjectMapper objectMapper;

	@Autowired
	public RoutingEaiMsgController(RoutingEaiMsgManService routingEaiMsgManService, ComboService comboService,
			ObjectMapper objectMapper) {
		this.routingEaiMsgManService = routingEaiMsgManService;
		this.comboService = comboService;
		this.objectMapper = objectMapper;
	}

	@GetMapping(value = "/onl/admin/service/routingEaiMsgMan.view")
	public void view() {
		// view
	}

	@PostMapping(value = "/onl/admin/service/routingEaiMsgMan.json", params = "cmd=LIST_INIT")
	public ModelAndView listInitCombo(String codeGroupArr) { // RESPONSE_TYPE

		List<ComboVo> listInstance = comboService.getFromEAIServerWithoutAll();
		List<ComboVo> listCombo = comboService.getFromCode(codeGroupArr);
		List<ComboVo> listFlowControl = comboService.getFromRouting("F"); // Flow Control 라우팅명

		Map<String, List<ComboVo>> resultMap = new HashMap<>();
		resultMap.put("instanceRows", listInstance);
		resultMap.put("list", listCombo);
		resultMap.put("listFlowControl", listFlowControl);

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

	@GetMapping(value = "/onl/admin/service/routingEaiMsgMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<RoutingEaiMsgUI>> selectList(Pageable pageable,
			RoutingEaiMsgSearch routingEaiMsgSearch) {
		Page<RoutingEaiMsgUI> page = routingEaiMsgManService.selectList(pageable, routingEaiMsgSearch);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/service/routingEaiMsgMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(@RequestParam Map<String, Object> paramMap) throws JsonProcessingException {
		Map<String, Object> resultMap = new HashMap<>();

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

		List<Map<String, Object>> dataList = objectMapper.readValue((String) paramMap.get("gridData"),
				new TypeReference<List<Map<String, Object>>>() {
				});

		resultMap.put("headers", headers);
		resultMap.put("headerTitles", headerTitles);
		resultMap.put("datas", dataList);
		resultMap.put("fileName", paramMap.get("fileName"));

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

}
