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.vo.GridResponse;

@Controller
public class InflowAdapterControlManController extends OnlBaseAnnotationController {

	@Autowired
	private InflowControlManService service;

	@Autowired
	private ComboService comboService;

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

	@RequestMapping(value = "/onl/admin/inflow/inflowAdapterControlMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/inflow/inflowAdapterControlManDetail";
	}

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

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

	@RequestMapping(value = "/onl/admin/inflow/inflowAdapterControlMan.json", params = "cmd=UPDATE")
	public String save(HttpServletRequest request, HttpServletResponse response, InflowControlManUI ui)
			throws Exception {
		service.mergeAdapter(ui);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.inflow.ReloadInflowAdapterControlCommand",
				ui.getName());
		agentUtilService.broadcast(command);
		return null;
	}

	@RequestMapping(value = "/onl/admin/inflow/inflowAdapterControlMan.json", params = "cmd=DELETE")
	public String delete(HttpServletRequest request, HttpServletResponse response, String name) throws Exception {
		service.deleteAdater(name);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.inflow.RemoveInflowAdapterControlCommand",
				name);
		agentUtilService.broadcast(command);
		return null;
	}

	@RequestMapping(value = "/onl/admin/inflow/inflowAdapterControlMan.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/inflowAdapterControlMan.json", params = "cmd=LIST_BUCKET_STATUS")
	public ModelAndView getBucketStatus(HttpServletRequest request, HttpServletResponse response, String adapterId) {
		List<Map<String, Object>> statusList = service.getAdapterBucketStatus(adapterId);
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("servers", statusList);
		return new ModelAndView("jsonView", resultMap);
	}

}