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

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.logging.UserAccessLogger;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
import com.eactive.eai.rms.onl.manage.serviceinfo.eaimsg.ui.EAIMessageUI;

/**
 * 인터페이스 간편삭제 컨트롤러
 * 
 * @author sunwook
 */
@Controller
public class EaiMsgEasyDeleteController extends BaseController {

	@Autowired
	private EaiMsgEasyDeleteService service;

	@Autowired
	private EaiMsgManService eaiMsgManService;

	@RequestMapping(value = "/onl/admin/service/eaiMsgEasyDelete.view")
	public String viewList(HttpServletRequest request, @RequestParam(value = "cmd", required = false) String cmd)
			throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())) {
			cmd = "";
		} else {
			cmd = CamelCaseUtil.converCamelCase(cmd);
		}
		String path = request.getServletPath();
		return path.split("[.]")[0] + cmd;
	}

	@RequestMapping(value = "/onl/admin/service/eaiMsgEasyDelete.json", params = "cmd=LIST")
	private ModelAndView selectList(JsonPageVo pageVo, @RequestParam Map<String, Object> paramMap) throws Exception {

		Map<String, Object> map = service.selectList(pageVo.getStartNum(), pageVo.getEndNum(), paramMap);

		pageVo.setTotalCount((Integer) map.get("totalCount"));

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("total", pageVo.getTotal()); // 전체 페이지수
		resultMap.put("page", pageVo.getPage()); // 현재 페이지수
		resultMap.put("records", pageVo.getTotalCount());// 전체 레코드건수
		resultMap.put("rows", map.get("list"));

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

	@RequestMapping(value = "/onl/admin/service/eaiMsgEasyDelete.json", params = "cmd=DETAIL_INIT")
	public ResponseEntity<EAIMessageUI> detailInit(String eaiSvcName) {
		EAIMessageUI eaiMessageUI = eaiMsgManService.selectDetail(eaiSvcName);
		return ResponseEntity.ok(eaiMessageUI);
	}

	@RequestMapping(value = "/onl/admin/service/eaiMsgEasyDelete.json", params = "cmd=DETAIL_B2BEXT")
	private ModelAndView selectB2bExt(@RequestParam Map<String, String> paramMap) throws Exception {

		List<Map<String, Object>> list = service.selectB2bExtList(paramMap);

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("rows", list);

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

	@RequestMapping(value = "/onl/admin/service/eaiMsgEasyDelete.json", params = "cmd=DETAIL_BZWKFLD")
	private ModelAndView selectBzwkFld(@RequestParam Map<String, String> paramMap) throws Exception {

		List<Map<String, Object>> list = service.selectBzwkFldList(paramMap);

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("rows", list);

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

	@RequestMapping(value = "/onl/admin/service/eaiMsgEasyDelete.json", params = "cmd=DETAIL_LIST_LAYOUT")
	private ModelAndView detailListLayout(@RequestParam Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> list = service.selectLayoutListByTransform(paramMap);

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("rows", list);

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

	@RequestMapping(value = "/onl/admin/service/eaiMsgEasyDelete.json", params = "cmd=DETAIL_LAYOUT_EFFECT_VALIDATION")
	private ModelAndView layoutEffectValidation(@RequestParam Map<String, Object> paramMap) throws Exception {

		ModelAndView modelAndView = new ModelAndView("jsonView", service.selectLayoutEffects(paramMap));

		return modelAndView;
	}

	@RequestMapping(value = "/onl/admin/service/eaiMsgEasyDelete.json", params = "cmd=DELETE")
	private ResponseEntity<String> delete(HttpServletRequest request, @RequestParam Map<String, String> paramMap)
			throws Exception {

		try {

			service.delete(paramMap);

			UserAccessLogger
					.log(request, "InterfaceEasyDelete", "S",
							String.valueOf(paramMap.get("eaiSvcName")) + " 인터페이스를 간편삭제 성공하였습니다.");

		} catch (Exception e) {

			logger.error("EaiMsgEasyDeleteController] " + e.getMessage());
			UserAccessLogger
					.log(request, "InterfaceEasyDelete", "F",
							String.valueOf(paramMap.get("eaiSvcName")) + " 인터페이스를 간편삭제 실패하였습니다.");

			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("delete fail : " + e.getMessage());
		}

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

}
