package com.huatek.frame.base.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.huatek.frame.base.vo.MenusVO;
import com.huatek.frame.base.vo.PermissionVO;

/**
 * @author key_dong
 * @date 2017年12月1日 下午4:00:29
 */
public class KerMenuTool {

	public static String allMenus(String permiss) {
		List<PermissionVO> permissions = JSONArray.parseArray(permiss, PermissionVO.class);
		StringBuffer menu = new StringBuffer();
		menu.append(
				" <li class=\"layui-nav-item\"><a href=\"javascript:;\" onclick=\"goHome()\" target=\"mainFrame\">首页</a></li>\r\n");
		for (PermissionVO permission : permissions) {
			if (permission.getLevel() == 1) {
				String parentId = permission.getId();
				String url = permission.getUrl();
				if (StringUtils.isEmpty(url)) {
					url = "javascript:;";
				}
				menu.append(" <li class=\"layui-nav-item\"><a href=\"" + url + "\" >"
						+ permission.getPname() + "</a><ul>");
				for (PermissionVO subpermission : permissions) {
					String scoundParentId = subpermission.getId();
					if (subpermission.getParentId().equals(parentId)) {
						url = subpermission.getUrl();
						if (StringUtils.isEmpty(url)) {
							url = "javascript:;";
						}
						menu.append(" <li class=\"layui-nav-item\"><a href=\"" + url
								+ "\" target=\"mainFrame\">" + subpermission.getPname()
								+ "</a><ul>");
						for (PermissionVO lastpermission : permissions) {
							if (lastpermission.getParentId().equals(scoundParentId) && lastpermission.getLevel() == 3) {
								menu.append(" <li class=\"layui-nav-item\"><a href=\"" + lastpermission.getUrl()
										+ "\" target=\"mainFrame\">"
										+ lastpermission.getPname() + "</a></li>\r\n");
							}
						}
						menu.append("</ul></li>\r\n");
					}
				}
				menu.append("</ul></li>\r\n");
			}
		}

		return menu.toString().replace("<ul></ul>", "");
	}
	
	
	public static List<MenusVO> menus(String permiss) {
		List<PermissionVO> permissions = JSONArray.parseArray(permiss, PermissionVO.class);
		List<MenusVO> menus=new ArrayList<>();
//		for (PermissionVO permission : permissions) { 
//			if (permission.getLevel() == 1) { 
//				String parentId = permission.getId();
//				MenusVO vo = new MenusVO();
//				vo.setCode(permission.getPermission());
//				vo.setName(permission.getPname());
//				vo.setResourceType(permission.getResourceType());
//				vo.setPath(permission.getUrl());
//				vo.setIFrame(permission.getIFrame());
//				List<MenusVO> submenus=new ArrayList<>();
//				for (PermissionVO subpermission : permissions) {  
//					if (subpermission.getParentId().equals(parentId)) {
//						String scoundParentId = subpermission.getId();
//						MenusVO subvo = new MenusVO();
//						subvo.setCode(subpermission.getPermission());
//						subvo.setName(subpermission.getPname());
//						subvo.setResourceType(subpermission.getResourceType());
//						subvo.setPath(subpermission.getUrl());
//						subvo.setIFrame(subpermission.getIFrame());
//						List<MenusVO> lastmenus=new ArrayList<>();
//						for (PermissionVO lastpermission : permissions) { 
//							if (lastpermission.getParentId().equals(scoundParentId) && lastpermission.getLevel() == 3) { 
//								MenusVO lastvo = new MenusVO();
//								lastvo.setCode(lastpermission.getPermission());
//								lastvo.setName(lastpermission.getPname());
//								lastvo.setResourceType(lastpermission.getResourceType());
//								lastvo.setPath(lastpermission.getUrl());
//								lastvo.setIFrame(lastpermission.getIFrame());
//								lastmenus.add(lastvo);
//							}
//						}
//						subvo.setChildren(lastmenus);
//						submenus.add(subvo);
//					}
//				}
//				vo.setChildren(submenus);
//				menus.add(vo);
//			}
//		}
		
		
		
		for (PermissionVO permission : permissions) { 
			if (permission.getLevel() == 1) { 
				String parentId = permission.getId();
				MenusVO vo = new MenusVO();
				vo.setCode(permission.getPermission());
				vo.setName(permission.getPname());
				vo.setResourceType(permission.getResourceType());
				vo.setPath(permission.getUrl());
				vo.setIFrame(permission.getIFrame());
				List<MenusVO> submenus=new ArrayList<>();
				for (PermissionVO subpermission : permissions) {  
					if (subpermission.getParentId().equals(parentId)) {
						String scoundParentId = subpermission.getId();
						MenusVO subvo = new MenusVO();
						subvo.setCode(subpermission.getPermission());
						subvo.setName(subpermission.getPname());
						subvo.setResourceType(subpermission.getResourceType());
						subvo.setPath(subpermission.getUrl());
						subvo.setIFrame(subpermission.getIFrame());
						List<MenusVO> threemenus = new ArrayList<>();
						for (PermissionVO threepermission : permissions) { 
							if (threepermission.getParentId().equals(scoundParentId) && threepermission.getLevel() == 3) {
								String threeParentId = threepermission.getId();
								MenusVO threevo = new MenusVO();
								threevo.setCode(threepermission.getPermission());
								threevo.setName(threepermission.getPname());
								threevo.setResourceType(threepermission.getResourceType());
								threevo.setPath(threepermission.getUrl());
								threevo.setIFrame(threepermission.getIFrame());
								List<MenusVO> lastmenus = new ArrayList<>();
								for (PermissionVO lastpermission : permissions) { 
									if (lastpermission.getParentId().equals(threeParentId) && lastpermission.getLevel() == 4) {
										MenusVO lastvo = new MenusVO();
										lastvo.setCode(lastpermission.getPermission());
										lastvo.setName(lastpermission.getPname());
										lastvo.setResourceType(lastpermission.getResourceType());
										lastvo.setPath(lastpermission.getUrl());
										lastvo.setIFrame(lastpermission.getIFrame());
										lastmenus.add(lastvo);
									}
								}
								threevo.setChildren(lastmenus);
								threemenus.add(threevo);
							}
						}
						subvo.setChildren(threemenus);
						submenus.add(subvo);
					}
				}
				vo.setChildren(submenus);
				menus.add(vo);
			}
		}
		return menus;
	}
}
