package cn.hmsoft.frame.util;

import javax.servlet.http.HttpServletRequest;

import cn.hmsoft.application.web.BrowserAgent;
import cn.hmsoft.helper.StringHelper;

/**********************************
 * 终端浏览器辅助类
 * 
 * @author: 张小庆
 * @date: 2016-02-22 21:42:19
 * @version: 2.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public abstract class FrameAgentUtil {
	public static String getUserAddr(HttpServletRequest request) {
		String ip = request.getHeader("X-Real-IP");  
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("X-Forwarded-For");  
	    }  
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("Proxy-Client-IP");  
	    }  
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("WL-Proxy-Client-IP"); 
	    }  
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getRemoteAddr();  
	    }  
		return ip;
	}

	public static BrowserAgent getBrowserAgent(HttpServletRequest request) {
		if (request == null)
			return null;
		BrowserAgent agent = getBrowserAgent(request.getHeader("User-Agent"));
		if (agent != null)
			agent.setUserAddr(getUserAddr(request));
		return agent;

	}

	public static BrowserAgent getBrowserAgent(String BrowserAgent) {
		if (StringHelper.isEmpty(BrowserAgent)) {
			return null;
		}

		String type = "x86";
		if (BrowserAgent.contains("WOW64") || BrowserAgent.contains("x64"))
			type = "x64";
		if (BrowserAgent.contains("Windows")) {// 主流应用靠前
			if (BrowserAgent.contains("Windows NT 10")) {// Windows 10
				return judgeBrowser(BrowserAgent, "Windows", "10", type);// 判断浏览器
			} else if (BrowserAgent.contains("Windows NT 6.3")) {// Windows 8.1
				return judgeBrowser(BrowserAgent, "Windows", "8.1", type);// 判断浏览器
			} else if (BrowserAgent.contains("Windows NT 6.2")) {// Windows 8
				return judgeBrowser(BrowserAgent, "Windows", "8", type);// 判断浏览器
			} else if (BrowserAgent.contains("Windows NT 6.1")) {// Windows 7
				return judgeBrowser(BrowserAgent, "Windows", "7", type);
			} else if (BrowserAgent.contains("Windows NT 6.0")) {// Windows Vista
				return judgeBrowser(BrowserAgent, "Windows", "Vista", type);
			} else if (BrowserAgent.contains("Windows NT 5.2")) {// Windows XP x64
																// Edition
				return judgeBrowser(BrowserAgent, "Windows", "XP", "x64 Edition");
			} else if (BrowserAgent.contains("Windows NT 5.1")) {// Windows XP
				return judgeBrowser(BrowserAgent, "Windows", "XP", type);
			} else if (BrowserAgent.contains("Windows NT 5.01")) {// Windows 2000,
																// Service Pack
																// 1 (SP1)
				return judgeBrowser(BrowserAgent, "Windows", "2000", "SP1");
			} else if (BrowserAgent.contains("Windows NT 5.0")) {// Windows 2000
				return judgeBrowser(BrowserAgent, "Windows", "2000", type);
			} else if (BrowserAgent.contains("Windows NT 4.0")) {// Microsoft
																// Windows NT
																// 4.0
				return judgeBrowser(BrowserAgent, "Windows", "NT 4.0", type);
			} else if (BrowserAgent.contains("Windows 98; Win 9x 4.90")) {// Windows
																		// Millennium
																		// Edition
																		// (Windows
																		// Me)
				return judgeBrowser(BrowserAgent, "Windows", "ME", type);
			} else if (BrowserAgent.contains("Windows 98")) {// Windows 98
				return judgeBrowser(BrowserAgent, "Windows", "98", type);
			} else if (BrowserAgent.contains("Windows 95")) {// Windows 95
				return judgeBrowser(BrowserAgent, "Windows", "95", type);
			} else if (BrowserAgent.contains("Windows CE")) {// Windows CE
				return judgeBrowser(BrowserAgent, "Windows", "CE", type);
			} else
				return judgeBrowser(BrowserAgent, "Windows", "Unknow", type);
		} else if (BrowserAgent.contains(" Mac OS X")) {
			// Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46
			// (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1
			if (BrowserAgent.contains("iPod")) {
				return judgeBrowser(BrowserAgent, "iPod", null, null);// 判断浏览器
			} else if (BrowserAgent.contains("iPad")) {
				return judgeBrowser(BrowserAgent, "iPad", null, null);// 判断浏览器
			} else
				return judgeBrowser(BrowserAgent, "iPhone", null, null);// 判断浏览器
		} else if (BrowserAgent.contains(" Linux"))
			return judgeBrowser(BrowserAgent, "Linux", null, null);// 判断浏览器
		else
			return judgeBrowser(BrowserAgent, "Unknow", type, null);
		// return null;
	}

	/**
	 * 用途：根据客户端 User Agent Strings 判断其浏览器 if 判断的先后次序：
	 * 根据浏览器的用户使用量降序排列，这样对于大多数用户来说可以少判断几次即可拿到结果： >>Browser:Chrome > FF > IE >
	 * ...
	 * 
	 * @param BrowserAgent
	 *            :user agent
	 * @param platformType
	 *            :平台
	 * @param platformSeries
	 *            :系列
	 * @param platformVersion
	 *            :版本
	 * @return
	 */
	private static BrowserAgent judgeBrowser(String BrowserAgent, String platformType, String platformSeries, String platformVersion) {
		// Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
		// like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240
		if (BrowserAgent.contains("Chrome")) {
			String temp = BrowserAgent.substring(BrowserAgent.indexOf("Chrome/") + 7);// 拿到User
																				// Agent
																				// String
																				// "Chrome/"
																				// 之后的字符串,结果形如"24.0.1295.0 Safari/537.15"或"24.0.1295.0"
			String chromeVersion = null;
			if (temp.indexOf(" ") < 0) {// temp形如"24.0.1295.0"
				chromeVersion = temp;
			} else {// temp形如"24.0.1295.0 Safari/537.15"
				chromeVersion = temp.substring(0, temp.indexOf(" "));
			}
			return new BrowserAgent("Chrome", chromeVersion, platformType, platformSeries, platformVersion);
		} else if (BrowserAgent.contains("Firefox")) {
			/**
			 * ******* FF 系列 ******* Firefox 16.0.1 - Mozilla/5.0 (Windows NT
			 * 6.2; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1 Firefox
			 * 15.0a2 - Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716
			 * Firefox/15.0a2 Firefox 15.0.2 - Mozilla/5.0 (Windows NT 6.2;
			 * WOW64; rv:15.0) Gecko/20120910144328 Firefox/15.0.2
			 * 判断依据:http://www.BrowserAgentstring.com/pages/Firefox/
			 */
			String temp = BrowserAgent.substring(BrowserAgent.indexOf("Firefox/") + 8);// 拿到User
																					// Agent
																					// String
																					// "Firefox/"
																					// 之后的字符串,结果形如"16.0.1 Gecko/20121011"或"16.0.1"
			String ffVersion = null;
			if (temp.indexOf(" ") < 0) {// temp形如"16.0.1"
				ffVersion = temp;
			} else {// temp形如"16.0.1 Gecko/20121011"
				ffVersion = temp.substring(0, temp.indexOf(" "));
			}
			return new BrowserAgent("Firefox", ffVersion, platformType, platformSeries, platformVersion);
		} else if (BrowserAgent.contains("MSIE")) {
			/**
			 * ******* IE 系列 ******* MSIE 10.0 - Internet Explorer 10 MSIE 9.0 -
			 * Internet Explorer 9 MSIE 8.0 - Internet Explorer 8 or IE8
			 * Compatibility View/Browser Mode MSIE 7.0 - Windows Internet
			 * Explorer 7 or IE7 Compatibility View/Browser Mode MSIE 6.0 -
			 * Microsoft Internet Explorer 6
			 * 判断依据:http://msdn.microsoft.com/en-us
			 * /library/ms537503(v=vs.85).aspx
			 */
			if (BrowserAgent.contains("MSIE 11.0")) {// Internet Explorer 11
				return new BrowserAgent("Internet Explorer", "11", platformType, platformSeries, platformVersion);
			} else if (BrowserAgent.contains("MSIE 10.0")) {// Internet Explorer 10
				return new BrowserAgent("Internet Explorer", "10", platformType, platformSeries, platformVersion);
			} else if (BrowserAgent.contains("MSIE 9.0")) {// Internet Explorer 9
				return new BrowserAgent("Internet Explorer", "9", platformType, platformSeries, platformVersion);
			} else if (BrowserAgent.contains("MSIE 8.0")) {// Internet Explorer 8
				return new BrowserAgent("Internet Explorer", "8", platformType, platformSeries, platformVersion);
			} else if (BrowserAgent.contains("MSIE 7.0")) {// Internet Explorer 7
				return new BrowserAgent("Internet Explorer", "7", platformType, platformSeries, platformVersion);
			} else if (BrowserAgent.contains("MSIE 6.0")) {// Internet Explorer 6
				return new BrowserAgent("Internet Explorer", "6", platformType, platformSeries, platformVersion);
			}
		} else if (BrowserAgent.contains("Trident")) {
			// Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like
			// Gecko
			if (BrowserAgent.contains("Trident/7.0; rv:11.0")) {// Internet
																// Explorer 11
				return new BrowserAgent("Internet Explorer", "11", platformType, platformSeries, platformVersion);
			}
		} else if (BrowserAgent.contains("Safari")) {
			return new BrowserAgent("Safari", "", platformType, platformSeries, platformVersion);

		} else {// 暂时支持以上三个主流.其它浏览器,待续...
			return new BrowserAgent("Unknow", "", platformType, platformSeries, platformVersion);
		}
		// Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46
		// (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1
		return null;
	}
}
