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

import java.time.LocalDateTime;
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 CircuitBreakerInterfaceManController extends OnlBaseAnnotationController {

	@Autowired
	private CircuitBreakerManService service;

	@Autowired
	private ComboService comboService;

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

	@RequestMapping(value = "/onl/admin/circuitbreaker/circuitBreakerInterfaceMan.view", params = "cmd=DETAIL")
	public String viewDetail() {
		return "/onl/admin/circuitbreaker/circuitBreakerInterfaceManDetail";
	}

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

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

	@RequestMapping(value = "/onl/admin/circuitbreaker/circuitBreakerInterfaceMan.json", params = "cmd=UPDATE")
	public String save(HttpServletRequest request, HttpServletResponse response, CircuitBreakerManUI ui)
			throws Exception {
		String modifiedBy = getSessionUserId(request);
		ui.setModifiedBy(modifiedBy);
		ui.setModifiedAt(LocalDateTime.now());
		service.merge(ui);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.circuitBreaker.type.ReloadTypedCircuitBreakerCommand",
				ui.getCbreakerId());
		agentUtilService.broadcast(command);
		return null;
	}

	@RequestMapping(value = "/onl/admin/circuitbreaker/circuitBreakerInterfaceMan.json", params = "cmd=DELETE")
	public String delete(HttpServletRequest request, HttpServletResponse response, CircuitBreakerManUI ui) throws Exception {
		service.delete(ui.getCbreakerId());
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.circuitBreaker.type.ReloadTypedCircuitBreakerCommand",
				ui.getCbreakerId());
		agentUtilService.broadcast(command);
		return null;
	}

	@RequestMapping(value = "/onl/admin/circuitbreaker/circuitBreakerInterfaceMan.json", params = "cmd=LIST_INIT_COMBO")
	public ModelAndView initCombo(HttpServletRequest request, HttpServletResponse response) {
		List<ComboVo> useYnRows = comboService.getFromCode("USE_YN");
		Map<String, List<ComboVo>> resultMap = new HashMap<>();
		resultMap.put("useYnRows", useYnRows);
		return new ModelAndView("jsonView", resultMap);
	}
	
	/**
	 * 세션에서 사용자 ID 조회
	 */
	private String getSessionUserId(HttpServletRequest request) {
		Object userId = request.getSession().getAttribute("userId");
		return userId != null ? userId.toString() : "SYSTEM";
	}
}