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

import java.util.HashMap;
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.web.SortDefault;
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.util.CommonUtil;
import com.eactive.eai.rms.common.vo.GridResponse;

/**
 * Rule 관련 Controller 클래스입니다.
 */
@Controller
public class LayoutTypeController extends OnlBaseAnnotationController {

	private static final String RELOAD_LAYOUT_TYPE_COMMAND = "com.eactive.eai.agent.transformer.ReloadLayoutTypeCommand";
	@Autowired
	private LayoutTypeManService service;

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

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

	@RequestMapping(value = "/onl/admin/rule/layoutTypeMan.json", params = "cmd=LIST")
	private ResponseEntity<GridResponse<LayoutTypeUI>> selectList(@SortDefault("loutptrnname") Pageable pageable)
			throws Exception {
		Page<LayoutTypeUI> uiPage = service.selectList(pageable);
		return ResponseEntity.ok(new GridResponse<>(uiPage));
	}

	@RequestMapping(value = "/onl/admin/rule/layoutTypeMan.json", params = "cmd=DETAIL")
	private ResponseEntity<LayoutTypeUI> selectDetail(String loutPtrnName) throws Exception {

		LayoutTypeUI ui = service.selectDetail(loutPtrnName);
		return ResponseEntity.ok(ui);
	}

	@RequestMapping(value = "/onl/admin/rule/layoutTypeMan.json", params = "cmd=INSERT")
	private ResponseEntity<?> insert(@Valid LayoutTypeUI ui, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return checkValidation(bindingResult);
		}
		service.insert(ui);
		CommonCommand command = new CommonCommand(RELOAD_LAYOUT_TYPE_COMMAND, ui.getLoutPtrnName());
		agentUtilService.broadcast(command);
		return ResponseEntity.ok().build();
	}

	@RequestMapping(value = "/onl/admin/rule/layoutTypeMan.json", params = "cmd=UPDATE")
	private ResponseEntity<?> update(@Valid LayoutTypeUI ui, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return checkValidation(bindingResult);
		}
		service.update(ui);
		CommonCommand command = new CommonCommand(RELOAD_LAYOUT_TYPE_COMMAND, ui.getLoutPtrnName());
		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/layoutTypeMan.json", params = "cmd=DELETE")
	private ResponseEntity<?> delete(String loutPtrnName) throws Exception {
		service.delete(loutPtrnName);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.transformer.RemoveLayoutTypeCommand",
				loutPtrnName);
		agentUtilService.broadcast(command);
		return ResponseEntity.ok().build();
	}

	@RequestMapping(value = "/onl/admin/rule/layoutTypeMan.json", params = "cmd=INITIALIZES")
	public ModelAndView initializes(HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonCommand command = new CommonCommand(RELOAD_LAYOUT_TYPE_COMMAND, "ALL");
		agentUtilService.broadcast(command);

		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);

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

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

	@RequestMapping(value = "/onl/admin/rule/layoutTypeMan.json", params = "cmd=INITIALIZE")
	public ModelAndView initialize(HttpServletRequest request, HttpServletResponse response, String loutTypeName)
			throws Exception {
		CommonCommand command = new CommonCommand(RELOAD_LAYOUT_TYPE_COMMAND, loutTypeName);
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);

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

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