package cn.com.capcloud.core.util;

import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class UserAgentUtil {
    private static UASparser uasParser = null;

    public static UserAgentInfo getBrowserName(){

        return null;
    }

    public static UASparser getUasParser() {
        if (uasParser == null) {
            try {
                uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return uasParser;
    }

    //识别常见游览器
    public static String identifyBrowser(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null) {
            if (userAgent.contains("Edg")) {
                return "Edge";
            } else if (userAgent.contains("Chrome")) {
                return "Chrome";
            } else if (userAgent.contains("Firefox")) {
                return "Firefox";
            } else if (userAgent.contains("Safari")) {
                return "Safari";
            } else {
                return "";
            }
        } else {
            return "";
        }
    }


    /**
     * 是否移动设备
     */
    public static boolean CheckAgent(String agent) {
        boolean flag = false;

        String[] keywords = {"Android", "iPhone", "iPod", "iPad",
                "Windows Phone", "MQQBrowser"};

        // 排除 Windows 桌面系统
        if (!agent.contains("Windows NT")
                || (agent.contains("Windows NT") && agent
                .contains("compatible; MSIE 9.0;"))) {
            // 排除 苹果桌面系统
            if (!agent.contains("Windows NT") && !agent.contains("Macintosh")) {
                for (String item : keywords) {
                    if (agent.contains(item)) {
                        flag = true;
                        break;
                    }
                }
            }
        }

        return flag;
    }

}
