package org.onetwo.boot.core.web.view;

import java.io.File;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.onetwo.common.web.utils.UserAgentUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

public class JspResourceViewResolver extends InternalResourceViewResolver {
	
	private String pcPostfix = ".pc";
	private String mobilePostfix = ".wap";

	public View resolveViewName(String viewName, Locale locale) throws Exception {
		String themeViewName = viewName;
		if(themeViewName.endsWith(pcPostfix) || themeViewName.endsWith(mobilePostfix)){
			themeViewName = viewName;
		}else{
			themeViewName = getThemeViewName(viewName);
		}
		return super.resolveViewName(themeViewName, locale);
	}
	
	protected String getThemeViewName(final String viewName){
		HttpServletRequest request = getRequest();
		String newView = viewName;
		boolean isPcView = true;
		if(UserAgentUtils.isMobileRequest(request)){
			newView = viewName + mobilePostfix;
			isPcView = false;
		}else{
			newView = viewName + pcPostfix;
		}
		if(!tryToCheckJspResource(request, newView)){
			newView = viewName;
			if(!tryToCheckJspResource(request, newView)){
				//反转
				newView = isPcView?(viewName + mobilePostfix):(viewName + pcPostfix);
			}
		}
		return newView;
	}
	
	protected boolean tryToCheckJspResource(HttpServletRequest request, String viewName){
		ServletContext sc = request.getServletContext();
		String jsp = getPrefix() + viewName + getSuffix();
		ServletContextResource scr = new ServletContextResource(sc, jsp);
		if(scr.exists()){
			return true;
		}
		String path = sc.getRealPath(jsp);
		if(StringUtils.isBlank(path)){
			return false;
		}
		File jspFile = new File(path);
		return jspFile.exists();
	}

	public static HttpServletRequest getRequest(){
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs==null?null:attrs.getRequest();
	}

	public void setPcPostfix(String pcPostfix) {
		this.pcPostfix = pcPostfix;
	}

	public void setMobilePostfix(String mobilePostfix) {
		this.mobilePostfix = mobilePostfix;
	}
	
}

@@ -205,24 +205,91 @@
 tringUtils;%0A
+import org.onetwo.boot.core.web.controller.AbstractBaseController;%0A
 import org.o
@@ -812,16 +812,64 @@
  %22.wap%22;
+%0A%09private boolean enableMobileDispatche = false;
 %0A%0A%09publi
@@ -942,16 +942,216 @@
 ption %7B%0A
+%09%09if(!enableMobileDispatche)%7B%0A%09%09%09return super.resolveViewName(viewName, locale);%0A%09%09%7D%0A%09%09if(viewName.startsWith(AbstractBaseController.REDIRECT))%7B%0A%09%09%09return super.resolveViewName(viewName, locale);%0A%09%09%7D%0A
 %09%09String
@@ -2791,13 +2791,142 @@
 tfix;%0A%09%7D
+%0A%0A%09public void setEnableMobileDispatche(boolean enableMobileDispatche) %7B%0A%09%09this.enableMobileDispatche = enableMobileDispatche;%0A%09%7D
 %0A%09%0A%7D%0A

fix jsp mobile dispatcher