package com.zyeeda.framework.managers.internal;

import java.io.IOException;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.xml.xpath.XPathExpressionException;

import com.zyeeda.framework.managers.MenuManager;
import com.zyeeda.framework.managers.PermissionManager;
import com.zyeeda.framework.viewmodels.MenuVo;
import com.zyeeda.framework.viewmodels.PermissionVo;

public class MenuManagerImpl implements MenuManager {

	public List<MenuVo> getMenuListByPermissionAuth(List<String> authList)
			throws XPathExpressionException, IOException {
		
		PermissionManager permissionMgr = new PermissionManagerImpl();
		List<MenuVo> listMenu = new ArrayList<MenuVo>();
		Map<String, MenuVo> menuMap = new LinkedHashMap<String, MenuVo>();
		String	root = null;
		for (String auth : authList) {
			System.out.println("*****************555" + auth);
			PermissionVo childPermission = permissionMgr.getPermissionByPath(auth);
			MenuVo childMenu = null;
			if (childPermission != null) {
				childMenu = this.convertPermission2Menu(childPermission);
				if (!(menuMap.containsKey(childMenu.getAuth()))) {
					menuMap.put(childMenu.getAuth(), childMenu);
				}else{
					continue;
				}
			} 
			PermissionVo parentPermission = permissionMgr.getParentPermissionByPath(auth);
			if (parentPermission == null) {
				if(!(menuMap.containsKey(childMenu.getAuth()))){
					listMenu.add(menuMap.get(childMenu.getAuth()));
				}
				continue;
			}
			if (menuMap.containsKey(parentPermission.getValue())) {
				MenuVo menuKey = menuMap.get(parentPermission.getValue());
				menuKey.getPermissionSet().add(childMenu);
				continue;
			}
			MenuVo parentMenu = this.convertPermission2Menu(parentPermission);
			parentMenu.getPermissionSet().add(childMenu);
			menuMap.put(parentPermission.getValue(), parentMenu);
			while (parentPermission != null) {
				parentMenu = new MenuVo();
			    String authKey = parentPermission.getValue();
				parentPermission = permissionMgr.getParentPermissionByPath(parentPermission.getValue());
					if(parentPermission != null){
						parentMenu.setAuth(parentPermission.getValue());
						parentMenu.setId(parentPermission.getId());
						parentMenu.setName(parentPermission.getName());
							if (!(menuMap.containsKey(parentMenu.getAuth()))) {
								parentMenu.getPermissionSet().add(menuMap.get(authKey));
								menuMap.put(parentMenu.getAuth(), parentMenu);
							} else {
								MenuVo menuKey = menuMap.get(parentMenu.getAuth());
								menuKey.getPermissionSet().add(menuMap.get(authKey));
								break;
							}
					} else {
						root = authKey;
						listMenu.add(menuMap.get(root));
					} 
			}
		}
		return listMenu;
	}
	
	private MenuVo convertPermission2Menu(PermissionVo permission) {
		MenuVo menu = new MenuVo();
		menu.setAuth(permission.getValue());
		menu.setId(permission.getId());
		menu.setName(permission.getName());
		return menu;
	}
	
	public static void main(String[] args) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		for (int i = 0; i < 10; i ++) {
			map.put(new Integer(i).toString(), new Integer(i).toString());
		}
		String v = map.get("3");
		map.put("3", "XX");
		System.out.println(v);
		for (String key : map.keySet()) {
			System.out.println(map.get(key));
		}
	}
}

@@ -1412,56 +1412,8 @@
 %09%09%09%09
-if(!(menuMap.containsKey(childMenu.getAuth())))%7B
 %0D%0A%09%09
@@ -1464,25 +1464,24 @@
 h()));%0D%0A%09%09%09%09
-%7D
 %0D%0A%09%09%09%09contin

update menu permission.