package kcg.imis.cmmn.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import kcg.imis.cmmn.MenuPosition;
import kcg.imis.cmmn.mapper.MenuMapper;
import kcg.imis.cmmn.vo.MenuVO;
import kcg.imis.sec.LoginUserVO;
import kcg.imis.sec.UserUtil;

import org.springframework.stereotype.Service;

import egovframework.rte.psl.dataaccess.util.EgovMap;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

	/**
	 * MenuMapper - 메뉴 데이터처리에 관한 Mapper 클래스.
	 **/
    @Resource(name = "menuMapper")
    private MenuMapper menuMapper;
	
	@Override
	public List<MenuVO> selectMainMenu(final MenuVO menuVO) {
		
		menuVO.setDepth(1);
		menuVO.setMenuPosition(MenuPosition.TOP);
		List<MenuVO> list = menuMapper.selectMemuList(menuVO);
		
		menuVO.setDepth(2);
		menuVO.setMenuPosition(MenuPosition.TOP);
		List<MenuVO> list2 = menuMapper.selectMemuList(menuVO);
		for(MenuVO menuVo2 : list2){
			
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("roleLevel", menuVO.getRoleLevel()); 
			paramMap.put("id", menuVo2.getId()); 
					
			if(menuVo2.getUrl() == null ||"".equals(menuVo2.getUri())){
				EgovMap map = menuMapper.getMenuInfoByParentId(paramMap);
				if(map != null){
					menuVo2.setUrl(String.valueOf(map.get("url")));
				}
			}
		}
		for(MenuVO menuVo : list){
			List<MenuVO> dept2List = new ArrayList<MenuVO>();
			for(MenuVO menuVo2 : list2){
				if(menuVo.getId() == menuVo2.getParent()){
					dept2List.add(menuVo2);
				}
			}
			menuVo.setMenuList(dept2List);
		}	
		
		return list; 
	}  

/*	@Override
	public List<MenuVO> selectTopSubMenuList(final MenuVO menuVO) {
		menuVO.setDepth(0);
		menuVO.setMenuPosition(MenuPosition.TOP_SUB);
		return menuMapper.selectListWithConnectBy(menuVO);
	}
*/
	/**
	 * 게시판 메뉴에 대한 정보를 가져온다.
	 * 
	 * @return 데이터 맵
	 */
	@Override
	public EgovMap getMenuInfo(final HashMap<String, Object> menu) {
		return menuMapper.getMenuInfo(menu);
	}

	@Override
	public List<EgovMap> selectListWithHigher(final String menuId) {
		
		List<EgovMap> resultList = new ArrayList<EgovMap>();
		EgovMap currentMenu = menuMapper.getMenuInfoById(menuId);
		resultList.add(currentMenu);
		
		int depth = ((BigDecimal)currentMenu.get("depth")).intValue();
		String parentId = String.valueOf(currentMenu.get("parent"));
		for(int i = 1 ; i < depth ; i ++){
			EgovMap parentMenu = menuMapper.getMenuInfoById(parentId);
			parentId = String.valueOf(parentMenu.get("parent"));
			resultList.add(parentMenu);
		}
		Collections.reverse(resultList);
		return resultList;
	}
	
	/**
	 * 서브 메뉴에 대한 정보를 가져온다.
	 * 
	 * @return 데이터 맵
	 */
	@Override
	public List<MenuVO> getSubMenu(EgovMap map) {
		
		LoginUserVO loginUserVO = UserUtil.getMemberInfo();
		map.put("roleLevel", loginUserVO.getRolelevel());
		
		List<MenuVO> submenu = menuMapper.getSubMenu(map);
		return submenu;
	}

	@Override
	public List<MenuVO> getTopMenu() {
		EgovMap map = new EgovMap();
		LoginUserVO loginUserVO = UserUtil.getMemberInfo();
		map.put("roleLevel", loginUserVO.getRolelevel());
		
		List<MenuVO> topmenu = menuMapper.getTopMenu(map);
		return topmenu;
	}
}
