package com.eactive.eai.rms.common.acl.menu;

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

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eactive.eai.rms.common.acl.menu.ui.MenuUI;
import com.eactive.eai.rms.common.acl.menu.ui.MenuUISearch;
import com.eactive.eai.rms.common.base.BaseAnnotationController;
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
@RequiredArgsConstructor
public class MenuController extends BaseAnnotationController {

	private final MenuManService menuManService;
	private final ComboService comboService;

	@GetMapping(value = "/common/acl/menu/menuMan.view")
	public void view() {
		// view
	}

	@GetMapping(value = "/common/acl/menu/menuMan.view", params = "cmd=POPUP")
	public String viewList() {
		return "/common/acl/menu/menuManPopup";
	}

	@ResponseBody
	@RequestMapping(value = "/common/acl/menu/menuMan.json", params = "cmd=LIST_TREE")
	public ResponseEntity<GridResponse<MenuUI>> selectTree(
			@RequestParam(name = "nodeid", required = false) String menuId, MenuUISearch menuUISearch) {
		if (StringUtils.isNotBlank(menuId)) {
			return ResponseEntity.ok(new GridResponse<>(new ArrayList<>()));
		}
		List<MenuUI> list = menuManService.selectTree(menuUISearch);
		return ResponseEntity.ok(new GridResponse<>(list));
	}

	@PostMapping(value = "/common/acl/menu/menuMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {

		List<ComboVo> listUseYnType = comboService.getMonitoringCodeSortedByIdCode("USE_YN_TYPE");
		List<ComboVo> listDisplayYn = comboService.getMonitoringCodeSortedByIdCode("DISPLAY_YN");
		List<ComboVo> listMenuAppCd = comboService.getMonitoringCodeSortedByIdCode("MENU_APP_CD");

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("displayYnRows", listDisplayYn);
		resultMap.put("useYnTypeRows", listUseYnType);
		resultMap.put("menuAppCdRows", listMenuAppCd);

		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/common/acl/menu/menuMan.json", params = "cmd=INSERT")
	public ResponseEntity<String> insert(MenuUI menuUI) {
		menuManService.insert(menuUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/menu/menuMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(MenuUI menuUI) {
		menuManService.update(menuUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/menu/menuMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String menuId) {
		menuManService.delete(menuId);
		return ResponseEntity.ok().build();
	}
}
