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

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;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.SortDefault;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RoleController extends BaseAnnotationController {

	@Autowired
	private RoleManService roleManService;

	@Autowired
	private ComboService comboService;

   private final ObjectMapper menuReadMapper = new ObjectMapper();

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

	@GetMapping(value = "/common/acl/role/roleMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/common/acl/role/roleManDetail";
	}

	@GetMapping(value = "/common/acl/role/roleMan.view", params = "cmd=POPUP")
	public String popuplView() { // 역할변경
		return "/common/acl/role/roleManPopup";
	}

	@GetMapping(value = "/common/acl/role/roleMan.view", params = "cmd=POPUP2")
	public String popuplSearchView() { // 초기화면 지정 검색
		return "/common/acl/role/roleManPopup2";
	}

	@PostMapping(value = "/common/acl/role/roleMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<RoleUI>> excute(@SortDefault("roleId") Pageable pageable, RoleUI roleUI) {
		Page<RoleUI> page = roleManService.selectList(pageable, roleUI);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/common/acl/role/roleMan.json", params = "cmd=DETAIL")
	public ResponseEntity<RoleUI> selectDetail(String roleId) {
		RoleUI roleUI = roleManService.selectDetail(roleId);
		return ResponseEntity.ok(roleUI);
	}

	@PostMapping(value = "/common/acl/role/roleMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		List<ComboVo> listUseYnType = comboService.getFromCode("USE_YN_TYPE");
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("useYnTypeRows", listUseYnType);
		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/common/acl/role/roleMan.json", params = "cmd=INSERT")
	public ResponseEntity<String> insert(RoleUI roleUI) {
		roleManService.insert(roleUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/role/roleMan.json", params = "cmd=UPDATE")
	public ResponseEntity<String> update(RoleUI roleUI) {
		roleManService.update(roleUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/common/acl/role/roleMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String roleId) {
		roleManService.delete(roleId);
		return ResponseEntity.ok().build();
	}

   @RequestMapping(value = "/common/acl/role/roleMan.json", params = "cmd=LIST_INIT_POPUP")
   public ResponseEntity<Map<String, List<String>>> popUpInit() {
      List<String> columnList = roleManService.getServiceTypesForMenuRendering();
      Map<String, List<String>> map = new HashMap<>();
      map.put("columnList", columnList);
      return ResponseEntity.ok(map);
   }

	@PostMapping(value = "/common/acl/role/roleMan.json", params = "cmd=LIST_TREE")
	public ResponseEntity<GridResponse<RoleMenuTreeUI>> selectTree(
			@RequestParam(name = "roleId", required = false) String roleId) {
		List<RoleMenuTreeUI> list = roleManService.selectRoleMenuTree(roleId);
		return ResponseEntity.ok(new GridResponse<>(list));
	}

	@PostMapping(value = "/common/acl/role/roleMan.json", params = "cmd=TRANSACTION_ROLE_MENU")
	public ResponseEntity<String> update(String roleId, String gridData) throws Exception {
      List<Map<String, String>> roleMenuList = menuReadMapper.readValue(gridData, new TypeReference<List<Map<String, String>>>() {});
		roleManService.transactionRoleMenu(roleId, roleMenuList);
		return ResponseEntity.ok().build();
	}
	
//	@RequestMapping(value= "/common/acl/role/roleMan.json",params = "cmd=CLONETOSTG")
//	private ModelAndView clone(HttpServletRequest request,HttpServletResponse response, @RequestParam HashMap<String,Object> map ) throws Exception {
//		
//		String message = "스테이징에 복사 완료하였습니다.";
//		String roleId = (String) map.get("roleId");
//	
//		//스테이징에 프라퍼티그룹이 존재하는지 체크
//		HashMap paramMap = new HashMap();
//		paramMap.put("roleId", roleId);
//		int stgChkCnt = roleService.selectRoleIdChkCount(paramMap);
//		
//		if(stgChkCnt > 0){
//			//스테이징  역할ID 수정
//			roleService.updateRoleId(map);
//			
//		}else{
//			//스테이징  역할ID 등록
//			roleService.insertRoleId(map);
//			
//		}
//		
//		//role menu
//		List<HashMap<String, Object>> detailMap = roleService.selectRoleMenu(roleId);
//		
//		roleService.transactionStgRoleMenu(roleId, detailMap);
//		
//		Map resultMap = new HashMap();
//		resultMap.put("message"   , message);
//		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
//		return modelAndView;
//		
//	}	

}
