package com.eactive.eai.rms.onl.manage.crypto;

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

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
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.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;

import com.eactive.eai.agent.command.CommandResult;
import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.vo.GridResponse;

@Controller
public class CryptoModuleConfigManController extends OnlBaseAnnotationController {

	private static final String RELOAD_CRYPTO_MODULE_COMMAND = "com.eactive.eai.agent.security.ReloadCryptoModuleCommand";

	private final CryptoModuleConfigManService service;

	@Autowired
	public CryptoModuleConfigManController(CryptoModuleConfigManService service) {
		this.service = service;
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));
	}

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

	@GetMapping(value = "/onl/admin/security/cryptoModuleMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/security/cryptoModuleManDetail";
	}

	@PostMapping(value = "/onl/admin/security/cryptoModuleMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<CryptoModuleConfigUI>> selectList(
			@SortDefault("cryptoName") Pageable pageable,
			String searchName, String algType, String keySourceType, String useYn) {
		Page<CryptoModuleConfigUI> page = service.selectList(pageable, searchName, algType, keySourceType, useYn);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/security/cryptoModuleMan.json", params = "cmd=DETAIL")
	public ResponseEntity<CryptoModuleConfigUI> selectDetail(String cryptoId) {
		return ResponseEntity.ok(service.selectDetail(cryptoId));
	}

	@PostMapping(value = "/onl/admin/security/cryptoModuleMan.json", params = "cmd=INSERT")
	public ResponseEntity<Map<String, String>> insert(@Valid CryptoModuleConfigUI ui, BindingResult bindingResult,
			HttpServletRequest request) {
		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);
		}
		String cryptoId = service.insert(ui, SessionManager.getUserId(request));
		CommandResult broadcastResult = CommonCommand.builder()
				.name(RELOAD_CRYPTO_MODULE_COMMAND)
				.args(cryptoId)
				.build()
				.broadcast(agentUtilService);
		Map<String, String> body = new HashMap<>();
		body.put("broadcastResult", broadcastResult.getMessage(true));
		return ResponseEntity.ok(body);
	}

	@PostMapping(value = "/onl/admin/security/cryptoModuleMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Map<String, String>> update(@Valid CryptoModuleConfigUI ui, BindingResult bindingResult,
			HttpServletRequest request) {
		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(ui, SessionManager.getUserId(request));
		CommandResult broadcastResult = CommonCommand.builder()
				.name(RELOAD_CRYPTO_MODULE_COMMAND)
				.args(ui.getCryptoId())
				.build()
				.broadcast(agentUtilService);
		Map<String, String> body = new HashMap<>();
		body.put("broadcastResult", broadcastResult.getMessage(true));
		return ResponseEntity.ok(body);
	}

	@PostMapping(value = "/onl/admin/security/cryptoModuleMan.json", params = "cmd=DELETE")
	public ResponseEntity<Map<String, String>> delete(String cryptoId) {
		service.delete(cryptoId);
		CommandResult broadcastResult = CommonCommand.builder()
				.name(RELOAD_CRYPTO_MODULE_COMMAND)
				.args(cryptoId)
				.build()
				.broadcast(agentUtilService);
		Map<String, String> body = new HashMap<>();
		body.put("broadcastResult", broadcastResult.getMessage(true));
		return ResponseEntity.ok(body);
	}

}
