package kcg.faics.cmmn.service.impl;

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

import javax.annotation.Resource;

import kcg.faics.cmmn.MenuPosition;
import kcg.faics.cmmn.service.MenuService;
import kcg.faics.cmmn.vo.MenuVO;
import kcg.faics.sec.LoginUserVO;
import kcg.faics.sec.UserUtil;

import org.springframework.stereotype.Service;

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

	/**
	 * MenuMapper - 메뉴 데이터처리에 관한 Mapper 클래스.
	 **/
    @Resource(name = "menuMapper")
    private MenuMapper menuMapper;
	
	@Override
	public List<MenuVO> selectListWithHigher(final MenuVO menuVO) {
		return menuMapper.selectListWithHigher(menuVO);
	}

	@Override
	public List<MenuVO> selectMainMenuList(final MenuVO menuVO) {
		LoginUserVO loginUserVO = UserUtil.getMemberInfo();
		
		menuVO.setDepth(1);
    menuVO.setMaxLevel(2);
		menuVO.setMenuPosition(MenuPosition.TOP);
		List<MenuVO> list = menuMapper.selectListWithConnectBy(menuVO);
		List<MenuVO> menuList = new ArrayList<MenuVO>();

		for (MenuVO currVO: list) {
			if (currVO.getDepth() == 0) {
				menuList.add(currVO);
			} else if (currVO.getDepth() == 1) {

				// 견문 메뉴 권한
				if (loginUserVO.getAuthSearch() <= 0 && "견문검색".equals(currVO.getName())) {
					continue;
				}
				if (loginUserVO.getAuthStatistics() <= 0 && "견문통계".equals(currVO.getName())) {
					continue;
				}
				if (loginUserVO.getAuthManage() <= 0 && !UserUtil.isAdmin() && "관리기능".equals(currVO.getName())) {
					continue;
				}
				for(MenuVO menu: menuList){
          if(menu.getId()==currVO.getParent()){
            menu.getSubMenu().add(currVO);
            break;
          }
        }

			}
		}
		
		return menuList; 
	}  

	@Override
	public List<MenuVO> selectTopSubMenuList(final MenuVO menuVO) {
		menuVO.setDepth(0);
    menuVO.setMaxLevel(1);
		menuVO.setMenuPosition(MenuPosition.TOP_SUB);
		return menuMapper.selectListWithConnectBy(menuVO);
	}

	/**
	 * 게시판 메뉴에 대한 정보를 가져온다.
	 * 
	 * @return 데이터 맵
	 */
	@Override
	public HashMap<String, Object> getMenuInfo(final HashMap<String, Object> menu) {
		
		HashMap<String, Object> paramMenu = menuMapper.getMenuInfo(menu);

    if ((Double)paramMenu.get("depth") < 3.0d) {
			return paramMenu;
		} else {
			menu.put("id", ((BigDecimal) paramMenu.get("parent")).intValue());
			return menuMapper.getMenuInfoById(menu);
		}
	}
	
	/**
	 * 서브 메뉴에 대한 정보를 가져온다.
	 * 
	 * @return 데이터 맵
	 */
	@Override
	public List<HashMap<String, Object>> getSubmenu(final HashMap<String, Object> menu) {
		List<HashMap<String, Object>> submenu = menuMapper.getSubmenu(menu);
		List<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
		LoginUserVO loginUserVO = UserUtil.getMemberInfo();
		
		// 견문인 경우 권한에 따라 설정해야함.
		if (submenu.size() > 0 && "report".equals(submenu.get(0).get("URL_GROUP"))) {
			for (HashMap<String, Object> map : submenu) {
				if (loginUserVO.getAuthApprove() <= 0 && "/approveList.do".equals(map.get("URL"))) {
					continue;
				}
				if ((loginUserVO.getAuthApprove() <= 0 && loginUserVO.getAuthWaitList() <= 0) && "/waitList.do".equals(map.get("URL"))) {
					continue;
				}
				result.add(map);
			}
		} else {
			result = submenu;
		}
		
		return result;
	}
}
