package com.eactive.eai.rms.onl.manage.rule.transformfunc;

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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
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.util.CommonUtil;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.onl.manage.rule.transformfunc.ui.TransformFuncUI;

/**
 * Rule 관련 Controller 클래스입니다.
 * 
 * @author : $Author: 김정옥 $
 * @version : $Revision: 1.4 $
 */

@Controller
public class TransformFuncController extends OnlBaseAnnotationController {

	@Autowired
	private TransformFuncManService service;

	@Autowired
	private ComboService comboService;

	@RequestMapping(value = "/onl/admin/rule/transformFuncMan.view")
	private void viewList() {
	}

	@GetMapping(value = "/onl/admin/rule/transformFuncMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/rule/transformFuncManDetail";
	}

	/**
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/onl/admin/rule/transformFuncMan.json", params = "cmd=LIST")
	private ResponseEntity<GridResponse<TransformFuncUI>> selectList(Pageable pageable, String searchCnvsnFuntnName)
			throws Exception {
		Page<TransformFuncUI> uiPage = service.selectList(pageable, searchCnvsnFuntnName);
		return ResponseEntity.ok(new GridResponse<>(uiPage));
	}

	@RequestMapping(value = "/onl/admin/rule/transformFuncMan.json", params = "cmd=DETAIL")
	private ResponseEntity<TransformFuncUI> selectDetail(String cnvsnFuntnName) {
		TransformFuncUI ui = service.selectDetail(cnvsnFuntnName);
		return ResponseEntity.ok(ui);
	}

	@RequestMapping(value = "/onl/admin/rule/transformFuncMan.json", params = "cmd=INSERT")
	private ResponseEntity<?> insert(@Valid TransformFuncUI ui, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return checkValidation(bindingResult);
		}
		service.insert(ui);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.transformer.ReloadTransformFuncCommand",
				ui.getCnvsnFuntnName());
		agentUtilService.broadcast(command);
		return ResponseEntity.ok().build();
	}

	@RequestMapping(value = "/onl/admin/rule/transformFuncMan.json", params = "cmd=UPDATE")
	private ResponseEntity<?> update(@Valid TransformFuncUI ui, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return checkValidation(bindingResult);
		}
		service.update(ui);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.transformer.ReloadTransformFuncCommand",
				ui.getCnvsnFuntnName());
		agentUtilService.broadcast(command);
		return ResponseEntity.ok().build();
	}

	private ResponseEntity<?> checkValidation(BindingResult bindingResult) {
		Map<String, String> errors = new HashMap<>();
		for (FieldError error : bindingResult.getFieldErrors()) {
			errors.put(error.getField(), error.getDefaultMessage());
		}
		return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);
	}

	@RequestMapping(value = "/onl/admin/rule/transformFuncMan.json", params = "cmd=DELETE")
	private ResponseEntity<?> delete(String cnvsnFuntnName) throws Exception {
		service.delete(cnvsnFuntnName);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.transformer.RemoveTransformFuncCommand",
				cnvsnFuntnName);
		agentUtilService.broadcast(command);
		return ResponseEntity.ok().build();
	}

	@RequestMapping(value = "/onl/admin/rule/transformFuncMan.json", params = "cmd=INITIALIZES")
	public ModelAndView initializes(HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.transformer.ReloadTransformFuncCommand",
				"ALL");
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);

		HashMap<String, Object> map = new HashMap<>();
		map.put("message", CommonUtil.getMapString(returnMap, true));

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

	@RequestMapping(value = "/onl/admin/rule/transformFuncMan.json", params = "cmd=INITIALIZE")
	public ModelAndView initialize(HttpServletRequest request, HttpServletResponse response, String cnvsnFuntnName)
			throws Exception {
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.transformer.ReloadTransformFuncCommand",
				cnvsnFuntnName);
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);

		HashMap<String, Object> map = new HashMap<>();
		map.put("message", CommonUtil.getMapString(returnMap, true));

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

	@RequestMapping(value = "/onl/admin/rule/transformFuncMan.json", params = "cmd=LIST_COMBO")
	public ResponseEntity<Map<String, Object>> selectCombo() {
		// 변환함수변환유형 ID
		List<ComboVo> listReturnType = comboService.selectTransform("CNVSNRETUNID");
		// 변환함수 유형ID
		List<ComboVo> listType = comboService.selectTransform("CNVSNTYPEID");
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("returnTypeList", listReturnType);
		resultMap.put("typeList", listType);

		return ResponseEntity.ok(resultMap);
	}

}
