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

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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.util.CommonUtil;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.common.vo.SimpleResponse;

@Controller
public class InflowClientControlManController extends OnlBaseAnnotationController {

	@Autowired
	private InflowControlManService service;

	@Autowired
	private ComboService comboService;

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

	@RequestMapping(value = "/onl/admin/inflow/inflowClientControlMan.view", params = "cmd=DETAIL")
	public String viewDetail() {
		return "/onl/admin/inflow/inflowClientControlManDetail";
	}

	@RequestMapping(value = "/onl/admin/inflow/inflowClientControlMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<InflowControlManUI>> selectList(HttpServletRequest request, Pageable pageVo,
			String searchName, String searchUseYn) {
		Page<InflowControlManUI> uiPage = service.selectClientList(pageVo, searchName, searchUseYn);
		return ResponseEntity.ok(new GridResponse<>(uiPage));
	}

	@RequestMapping(value = "/onl/admin/inflow/inflowClientControlMan.json", params = "cmd=DETAIL")
	public ResponseEntity<InflowControlManUI> selectDetail(HttpServletRequest request, HttpServletResponse response,
			String name) {
		InflowControlManUI ui = service.selectClientDetail(name);
		return ResponseEntity.ok(ui);
	}

	@RequestMapping(value = "/onl/admin/inflow/inflowClientControlMan.json", params = "cmd=UPDATE")
	public ResponseEntity<SimpleResponse> save(HttpServletRequest request, HttpServletResponse response, InflowControlManUI ui)
			throws Exception {
		service.mergeClient(ui);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.inflow.ReloadInflowClientControlCommand", ui.getName());
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);
		SimpleResponse simpleResponse = SimpleResponse.of(CommonUtil.getMapString(returnMap, true));
		return ResponseEntity.ok(simpleResponse);
	}

	@RequestMapping(value = "/onl/admin/inflow/inflowClientControlMan.json", params = "cmd=DELETE")
	public ResponseEntity<SimpleResponse> delete(HttpServletRequest request, HttpServletResponse response, String name) throws Exception {
		service.deleteClient(name);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.inflow.RemoveInflowClientControlCommand", name);
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);
		SimpleResponse simpleResponse = SimpleResponse.of(CommonUtil.getMapString(returnMap, true));
		return ResponseEntity.ok(simpleResponse);
	}

	@RequestMapping(value = "/onl/admin/inflow/inflowClientControlMan.json", params = "cmd=LIST_INIT_COMBO")
	public ModelAndView initCombo(HttpServletRequest request, HttpServletResponse response) {
		List<ComboVo> useYnRows = comboService.getFromCode("USE_YN");
		List<ComboVo> timeUnitRows = comboService.getFromCode("INFLOW_CTRL_TIME_UNIT");
		Map<String, List<ComboVo>> resultMap = new HashMap<>();
		resultMap.put("useYnRows", useYnRows);
		resultMap.put("timeUnitRows", timeUnitRows);
		return new ModelAndView("jsonView", resultMap);
	}

	@RequestMapping(value = "/onl/admin/inflow/inflowClientControlMan.json", params = "cmd=LIST_BUCKET_STATUS")
	public ModelAndView getBucketStatus(HttpServletRequest request, HttpServletResponse response, String clientId) {
		List<Map<String, Object>> statusList = service.getClientBucketStatus(clientId);
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("servers", statusList);
		return new ModelAndView("jsonView", resultMap);
	}

}