package com.ssm.web;

import java.lang.reflect.Method;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.ssm.model.system.SysMenu;
import com.ssm.service.system.SysMenuService;

public class PageNavigation {
	
	@Autowired
	private SysMenuService service;
	
	public Object aroundMethod2(ProceedingJoinPoint pjp) throws Throwable {
		
		Signature signature = pjp.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		Method method = methodSignature.getMethod();
		if(method.isAnnotationPresent(WithoutUserLogin.class))
			return pjp.proceed();
		
		
		HashMap<String, String> map = new HashMap<String,String>();
		// 获取request和session
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();
		ServletRequestAttributes sra = (ServletRequestAttributes) ra;
		HttpServletRequest request = sra.getRequest();
		HttpSession session = request.getSession();
//		String path = request.getContextPath();
//		String url = request.getRequestURL().toString();
		String url = "";
		
		RequestMapping uri1 = AnnotationUtils.findAnnotation(methodSignature.getMethod().getDeclaringClass(), RequestMapping.class);
		if(uri1 != null){
			String[] a = uri1.value();
			if(a != null && a.length > 0 ){
				for(String u : a){
					url += u + "/";
				}
			}
		}
		
		if(method.isAnnotationPresent(RequestMapping.class)){
			RequestMapping uri2 = method.getAnnotation(RequestMapping.class);
			String[] b = uri2.value();
			if(b != null && b.length > 0){
				for(String u : b){
					url += u + "/";
				}
			}
		}
		
		
//		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//		
//		if(url.indexOf(basePath) > -1)
//			url = url.replace(basePath, "");
//		
		if(url.startsWith("/"))
			url = url.substring(1);
		if(url.endsWith("/"))
			url = url.substring(0, url.length() - 1);
		SysMenu menu = service.selectMenuByUrl(url);
		System.out.println(url);
		if(menu != null && !"login".equals(url)) {
			if(menu.getLevel() == 2 ) {
				SysMenu parent = service.selectByMenuId(menu.getParentid());
				session.setAttribute("pitchOn1", parent.getMenuid());
				session.setAttribute("pitchOn2", menu.getMenuid());
				session.removeAttribute("pitchOn3");
				session.removeAttribute("pitchOn4");
				map.put("pname1", parent.getMenucnname());
				map.put("pmenuurl1", parent.getMenuurl());
				
				map.put("pname2", menu.getMenucnname());
				map.put("pmenuurl2", menu.getMenuurl());
				
				map.remove("pname3");
				map.remove("pmenuurl3");
				
				map.remove("pname4");
				map.remove("pmenuurl4");
			} 
			if (menu.getLevel() == 3 ) {
				SysMenu parent = service.selectByMenuId(menu.getParentid());
				SysMenu supParent = service.selectByMenuId(parent.getParentid());
				session.setAttribute("pitchOn1", supParent.getMenuid());
				session.setAttribute("pitchOn2", parent.getMenuid());
				session.setAttribute("pitchOn3", menu.getMenuid());
				session.removeAttribute("pitchOn4");
				map.put("pname1", supParent.getMenucnname());
				map.put("pmenuurl1", supParent.getMenuurl());
				
				map.put("pname2", parent.getMenucnname());
				map.put("pmenuurl2", parent.getMenuurl());
				
				map.put("pname3", menu.getMenucnname());
				map.put("pmenuurl3", menu.getMenuurl());
				
				map.remove("pname4");
				map.remove("pmenuurl4");
				
			} 
			
			if(menu.getLevel() == 4){
				//三级菜单
				SysMenu parent_3 = service.selectByMenuId(menu.getParentid());
				//二级菜单
				SysMenu parent_2 = service.selectByMenuId(parent_3.getParentid());
				//一级菜单
				SysMenu parent_1 = service.selectByMenuId(parent_2.getParentid());
				session.setAttribute("pitchOn1", parent_1.getMenuid());
				session.setAttribute("pitchOn2", parent_2.getMenuid());
				session.setAttribute("pitchOn3", parent_3.getMenuid());
				session.setAttribute("pitchOn4", menu.getMenuid());
				
				map.put("pname1", parent_1.getMenucnname());
				map.put("pmenuurl1", parent_1.getMenuurl());
				
				map.put("pname2", parent_2.getMenucnname());
				map.put("pmenuurl2", parent_2.getMenuurl());
				
				map.put("pname3", parent_3.getMenucnname());
				map.put("pmenuurl3", parent_3.getMenuurl());
				
				map.put("pname4", menu.getMenucnname());
				map.put("pmenuurl4", menu.getMenuurl());
				
			}
			
		} else {
			if(url.equals("home/homeIndex")) {
				session.removeAttribute("pitchOn1");
				session.removeAttribute("pitchOn2");
				session.removeAttribute("pitchOn3");
				session.removeAttribute("pitchOn4");
			}
			map.remove("pname3");
			map.remove("pmenuurl3");
			
			map.remove("pname4");
			map.remove("pmenuurl4");
		}
		
		session.setAttribute("pageNav", map);
		Object retVal = pjp.proceed();
		return retVal;
	}
	

}
