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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.vo.GridResponse;

@Controller
public class ServerManController extends OnlBaseAnnotationController {

	private ServerManService serverManService;

	private ComboService comboService;

	@Autowired
	public ServerManController(ServerManService serverManService, ComboService comboService) {
		this.serverManService = serverManService;
		this.comboService = comboService;
	}

	@RequestMapping(value = "/onl/admin/common/serverMan.view")
	public String viewList() {
		return "/onl/admin/common/serverMan";
	}

	@RequestMapping(value = "/onl/admin/common/serverMan.view", params = "cmd=DETAIL")
	public String viewDetail() {
		return "/onl/admin/common/serverManDetail";
	}

	@RequestMapping(value = "/onl/admin/common/serverMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<EAIServerUI>> selectList(
			@PageableDefault(sort = "eaisevrinstncname") Pageable pageable) {
		Page<EAIServerUI> page = serverManService.selectList(pageable);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@RequestMapping(value = "/onl/admin/common/serverMan.json", params = "cmd=DETAIL")
	public ResponseEntity<EAIServerUI> selectDetail(String eaiSevrInstncName) {
		EAIServerUI eaiServerUI = serverManService.selectDetail(eaiSevrInstncName);
		return ResponseEntity.ok(eaiServerUI);
	}

	@RequestMapping(value = "/onl/admin/common/serverMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(EAIServerUI eaiServerUI) {

		serverManService.insert(eaiServerUI);

		CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_EAI_SERVER_COMMAND)
				.args(eaiServerUI.getEaiSevrInstncName())
				.build()
				.broadcast(agentUtilService);

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

	@RequestMapping(value = "/onl/admin/common/serverMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(EAIServerUI eaiServerUI) {

		serverManService.update(eaiServerUI);

		CommonCommand
				.builder()
				.name(CommonCommand.RELOAD_EAI_SERVER_COMMAND)
				.args(eaiServerUI.getEaiSevrInstncName())
				.build()
				.broadcast(agentUtilService);

		return ResponseEntity.ok().build();

	}

	@RequestMapping(value = "/onl/admin/common/serverMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String eaiSevrInstncName) {
		serverManService.delete(eaiSevrInstncName);
		CommonCommand
				.builder()
				.name(CommonCommand.REMOVE_EAI_SERVER_COMMAND)
				.args(eaiSevrInstncName)
				.build()
				.broadcast(agentUtilService);
		return ResponseEntity.ok().build();

	}

	@RequestMapping(value = "/onl/admin/common/serverMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo(String svrInstName) {

		List<ComboVo> listServer = comboService.getFromEAIServerForFailOver(svrInstName);

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("serverRows", listServer);

		return ResponseEntity.ok(resultMap);
	}

	@RequestMapping(value = "/onl/admin/common/serverMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel() throws Exception {

		HashMap<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> datas = serverManService.selectListToExcel();
		resultMap.put("datas", datas);

		return new ModelAndView("excelView", resultMap);
	}

}