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

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

import javax.servlet.http.HttpServletRequest;

import com.eactive.eai.rms.common.datasource.DataSourceType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import com.eactive.eai.rms.common.acl.menu.ui.LeftMenuUI;
import com.eactive.eai.rms.common.acl.role.RoleManService;
import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.data.entity.man.menu.MenuService;

@Controller
public class MenuRenderController extends BaseAnnotationController {

	@Autowired
	private RoleManService roleManService;

	@Autowired
	private MenuService menuService;

	@Autowired
	private MenuRenderService menuRenderService;

	@GetMapping("/left_top.do")
	public String left_top(HttpServletRequest request, ModelMap modelMap) {
		return "/common/screen/left_top";
	}

	@GetMapping("/leftMenu.do")
	public String leftMenu(HttpServletRequest request, ModelMap modelMap, String menuId) throws Exception {

		String lv3MenuId = menuId.replaceAll("...$", "000");
		List<String> roleIds = SessionManager.getRoleId(request);
		List<String> appCodes = DataSourceContextHolder.getRender(request);
      String serviceType = request.getParameter("serviceType");

		if (StringUtils.isNotBlank(lv3MenuId)) {
			// 메뉴명 가져오기
         LeftMenuUI leftMenuUI = menuRenderService.leftMenuList(lv3MenuId, roleIds, appCodes, serviceType);

			modelMap.addAttribute("menuId", menuId);
			modelMap.addAttribute("menuName", leftMenuUI.getMenuName());
			modelMap.addAttribute("leftMenuList", leftMenuUI.getMenuList());
		}
		return "/common/screen/leftMenu";
	}

	@GetMapping("/top_title.do")
	public String top_title(HttpServletRequest request, ModelMap modelMap) {
		return "/common/screen/top_title";
	}

	@GetMapping("/top_logo.do")
	public String top_logo(HttpServletRequest request, ModelMap modelMap) {
		return "/common/screen/top_logo";
	}

	/**
	 * 권한에 따른 읽기 가능한 메뉴 가져오기.
	 * 
	 * @return 메뉴리스트
	 */
	@GetMapping("/top_04.do")
   public String getMenuList(HttpServletRequest request, String serviceType, ModelMap modelMap) throws Exception {

		List<String> roleIds = SessionManager.getRoleId(request);
		List<String> appCodes = DataSourceContextHolder.getRender(request);

      List<Map<String, Object>> dataList = menuService.getMenuList(roleIds, appCodes, serviceType);
		modelMap.addAttribute("topMenuList", dataList);

		// 파라미터로 초기페이지 를 지정한 경우
		String mainPage = request.getParameter("mainPage");
		String menuId = request.getParameter("menuId");
		String id = null;

      List<DataSourceType> roleServiceTypeList = roleManService.selectRoleServiceTypeList(roleIds);
      modelMap.addAttribute("roleServiceTypeList", roleServiceTypeList);

		if (StringUtils.isNotBlank(mainPage)) {
			for (Map<String, Object> map : dataList) {
				String url = (String) map.get("LEV4_URL");
				id = (String) map.get("LEV4_ID");
				if (url != null && url.contains(mainPage)) {
					// 상세대시보드화면에서 선택하여 지정한 초기화면
					modelMap.addAttribute("roleScreenId", id);
					modelMap.addAttribute("mainPage", mainPage);
					break;
				}
			}
		} else if (StringUtils.isNotBlank(menuId)) {
			if (menuService.isMenuExistInService(appCodes, menuId)) {
				for (Map<String, Object> map : dataList) {
					id = (String) map.get("LEV4_ID");
					if (id != null && id.contains(menuId)) {
						// 상세대시보드화면에서 선택하여 지정한 초기화면
						modelMap.addAttribute("roleScreenId", id);
						modelMap.addAttribute("menuId", menuId);
						break;
					}
				}
			}
			if (id == null) {
				modelMap.addAttribute("roleScreenId", roleManService.getRoleScreenId(roleIds));
			}
		}

		if (id == null && StringUtils.isBlank(mainPage) && StringUtils.isBlank(menuId)) {
			// 역할권한 관리 화면 에서 지정한 초기화면
			modelMap.addAttribute("roleScreenId", roleManService.getRoleScreenId(roleIds));
		}

		return "/common/screen/top_04";
	}
	
	/**
	 *  메뉴권한 미존재 유저 접속 시 빈 화면 frame 노출  20251126
	 * 
	 * @return 빈 화면 (blank_screen)
	 */
	@GetMapping("/blankScreen.do")
	public String getBlankScreen(HttpServletRequest request, String serviceType, ModelMap modelMap) throws Exception {
		return "/common/screen/blank_screen";
	}
	

}
