package com.eactive.eai.rms.onl.manage.comm.error;

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

import org.apache.commons.lang3.StringUtils;
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.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.eactive.eai.rms.common.base.BaseAnnotationController;
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.data.entity.onl.inbound.error.InboundErrorInfoSearch;

@Controller
public class InboundErrorInfoManController extends BaseAnnotationController {

	private InboundErrorInfoManService service;

	private ComboService comboService;

	@Autowired
	public InboundErrorInfoManController(InboundErrorInfoManService service, ComboService comboService) {
		this.service = service;
		this.comboService = comboService;
	}

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

	@GetMapping(value = "/onl/admin/common/inboundErrorInfoMan.view", params = "cmd=DETAIL")
	public String viewDetail() {
		return "/onl/admin/common/inboundErrorInfoManDetail";
	}

	@PostMapping(value = "/onl/admin/common/inboundErrorInfoMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<InboundErrorInfoUI>> selectList(Pageable pageable,
			InboundErrorInfoSearch inboundErrorInfoSearch) {
		Page<InboundErrorInfoUI> page = service.selectList(pageable, inboundErrorInfoSearch);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/common/inboundErrorInfoMan.json", params = "cmd=DETAIL")
	public ResponseEntity<InboundErrorInfoUI> selectDetail(String eaiSvcSerno) {
		InboundErrorInfoUI inboundErrorInfoUI = service.selectDetail(eaiSvcSerno);
		return ResponseEntity.ok(inboundErrorInfoUI);
	}

	@GetMapping(value = "/onl/admin/common/inboundErrorInfoMan.json", params = "cmd=DETAIL_XML")
	public String selectDetailXml(String showType, String eaiSvcSerno, ModelMap model) {
		InboundErrorInfoUI data = service.selectDetail(eaiSvcSerno);

		String popup = "forward:/common/jsp/popupXml.jsp";
		if ("TEXT".equals(showType)) {
			popup = "forward:/common/jsp/popupText.jsp";
		}

		if (data == null) {
			model.addAttribute("param", "데이터 조회 실패");
			return popup;
		}

		String xmlData = data.getBzwkDataCtnt();

		if (StringUtils.isNotBlank(xmlData)) {
			model.addAttribute("param", xmlData);
		}
		return popup;
	}

	@PostMapping(value = "/onl/admin/common/inboundErrorInfoMan.json", params = "cmd=LIST_COMBO")
	public ResponseEntity<Map<String, Object>> selectCombo() {
		List<ComboVo> adapterList = comboService.getFromAdapterGroup(true);

		Map<String, Object> map = new HashMap<>();
		map.put("adapterList", adapterList);

		return ResponseEntity.ok(map);

	}

}