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

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

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.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
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.vo.GridResponse;

@Controller
public class ErrorCodeManController extends OnlBaseAnnotationController {

	private ErrorCodeManService service;

	@Autowired
	public ErrorCodeManController(ErrorCodeManService service) {
		this.service = service;
	}

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

	@GetMapping(value = "/onl/admin/common/errorCodeMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/common/errorCodeManDetail";
	}

	@PostMapping(value = "/onl/admin/common/errorCodeMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<ErrorCodeUI>> selectList(@SortDefault("msg") Pageable pageable, String searchMsg,
			String searchMsgDesc) {
		Page<ErrorCodeUI> page = service.selectList(pageable, searchMsg, searchMsgDesc);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/common/errorCodeMan.json", params = "cmd=DETAIL")
	public ResponseEntity<ErrorCodeUI> selectDetail(String msg) {
		ErrorCodeUI errorCodeUI = service.selectDetail(msg);
		return ResponseEntity.ok(errorCodeUI);
	}

	@PostMapping(value = "/onl/admin/common/errorCodeMan.json", params = "cmd=INSERT")
	public ResponseEntity<Map<String, String>> insert(@Valid ErrorCodeUI errorCodeUI, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			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);
		}
		service.insert(errorCodeUI);

		CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_ERROR_CODE_COMMAND)
				.args(errorCodeUI.getMsg())
				.build()
				.broadcast(agentUtilService);

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

	@PostMapping(value = "/onl/admin/common/errorCodeMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Map<String, String>> update(ErrorCodeUI errorCodeUI, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			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);
		}

		service.update(errorCodeUI);

		CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_ERROR_CODE_COMMAND)
				.args(errorCodeUI.getMsg())
				.build()
				.broadcast(agentUtilService);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/common/errorCodeMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(ErrorCodeUI errorCodeUI) {
		service.delete(errorCodeUI.getMsg());

		CommonCommand
				.builder()
				.name(CommonCommand.REMOVE_ERROR_CODE_COMMAND)
				.args(errorCodeUI.getMsg())
				.build()
				.broadcast(agentUtilService);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/common/errorCodeMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(@RequestParam Map<String, Object> paramMap) {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object>[] datas = service.selectListToExcel();
		resultMap.put("datas", datas);
		return new ModelAndView("excelView", resultMap);
	}

}