package com.eactive.eai.rms.onl.httpouttlsinfo;

import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.agent.httpouttlsinfo.ReloadHttpOutTlsInfoCommand;
import com.eactive.eai.agent.transformer.RemoveLayoutCommand;
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.common.vo.SimpleResponse;
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.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

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

@Controller
public class HttpOutTlsInfoManController extends OnlBaseAnnotationController {

	private final HttpOutTlsInfoManService httpOutTlsInfoManService;
    private final ComboService comboService;

	@Autowired
	public HttpOutTlsInfoManController(HttpOutTlsInfoManService httpOutTlsInfoManService,
									   ComboService comboService) {
		this.httpOutTlsInfoManService = httpOutTlsInfoManService;
        this.comboService = comboService;
	}

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

	@GetMapping(value = "/onl/admin/httpouttlsinfo/httpOutTlsInfoMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/httpouttlsinfo/httpOutTlsInfoManDetail";
	}

	@PostMapping(value = "/onl/admin/httpouttlsinfo/httpOutTlsInfoMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<HttpOutTlsInfoUI>> selectList(@SortDefault("clientId") Pageable pageable) {
		Page<HttpOutTlsInfoUI> page = httpOutTlsInfoManService.selectList(pageable);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/httpouttlsinfo/httpOutTlsInfoMan.json", params = "cmd=DETAIL")
	public ResponseEntity<HttpOutTlsInfoUI> selectDetail(String clientId) {
		HttpOutTlsInfoUI httpOutTlsInfoUI = httpOutTlsInfoManService.selectDetail(clientId);
		return ResponseEntity.ok(httpOutTlsInfoUI);
	}

	@PostMapping(value = "/onl/admin/httpouttlsinfo/httpOutTlsInfoMan.json", params = "cmd=INSERT")
	public ResponseEntity<SimpleResponse> insert(HttpOutTlsInfoUI httpOutTlsInfoUI) throws Exception {
		httpOutTlsInfoManService.insert(httpOutTlsInfoUI);

		CommonCommand command = new CommonCommand(ReloadHttpOutTlsInfoCommand.class.getName(), httpOutTlsInfoUI.getClientId());
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);

		SimpleResponse simpleResponse = SimpleResponse.of(CommonUtil.getMapString(returnMap, true));
		return ResponseEntity.ok(simpleResponse);
	}

	@PostMapping(value = "/onl/admin/httpouttlsinfo/httpOutTlsInfoMan.json", params = "cmd=UPDATE")
	public ResponseEntity<SimpleResponse> update(HttpOutTlsInfoUI httpOutTlsInfoUI) throws Exception {
		httpOutTlsInfoManService.update(httpOutTlsInfoUI);

		CommonCommand command = new CommonCommand(ReloadHttpOutTlsInfoCommand.class.getName(), httpOutTlsInfoUI.getClientId());
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);

		SimpleResponse simpleResponse = SimpleResponse.of(CommonUtil.getMapString(returnMap, true));
		return ResponseEntity.ok(simpleResponse);
	}

	@PostMapping(value = "/onl/admin/httpouttlsinfo/httpOutTlsInfoMan.json", params = "cmd=DELETE")
	public ResponseEntity<SimpleResponse> delete(String clientId) throws Exception {
		httpOutTlsInfoManService.delete(clientId);

		CommonCommand command = new CommonCommand(RemoveLayoutCommand.class.getName(), clientId);
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);

		SimpleResponse simpleResponse = SimpleResponse.of(CommonUtil.getMapString(returnMap, true));
		return ResponseEntity.ok(simpleResponse);
	}

	@PostMapping(value = "/onl/admin/httpouttlsinfo/httpOutTlsInfoMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		List<ComboVo> clientList = comboService.getFromClientId();
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("clientIdRows", clientList);
		return ResponseEntity.ok(resultMap);
	}


}