package com.kingdee.eas.custom.integration.feishu.util;

import javax.servlet.http.HttpServletRequest;

/**
 * 飞书用户代理工具类
 */
public class FeishuUserAgentUtil {

    /**
     * 判断请求是否来自飞书客户端
     */
    public static boolean isFeishu(String userAgent) {
        if (userAgent == null) return false;
        // 飞书国内版 UA 通常包含 "Feishu"，国际版包含 "Lark"
        return userAgent.contains("Feishu") || userAgent.contains("Lark");
    }

    /**
     * 判断是否为移动设备（包含手机/平板）
     */
    public static boolean isMobileDevice(String userAgent) {
        if (userAgent == null) return false;
        String ua = userAgent.toLowerCase();
        // 移动端常见关键词
        return ua.contains("mobile")
                || ua.contains("android")
                || ua.contains("iphone")
                || ua.contains("ipad")
                || ua.contains("ipod");
    }

    /**
     * 判断是否为飞书 PC 端
     */
    public static boolean isFeishuPC(String userAgent) {
        return isFeishu(userAgent) && !isMobileDevice(userAgent);
    }

    /**
     * 判断是否为飞书移动端
     */
    public static boolean isFeishuMobile(String userAgent) {
        return isFeishu(userAgent) && isMobileDevice(userAgent);
    }

    /**
     * 从 HttpServletRequest 中获取客户端类型
     *
     * @return "feishu_pc", "feishu_mobile", 或 "other"
     */
    public static String getClientType(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (isFeishuMobile(userAgent)) {
            return "feishu_mobile";
        } else if (isFeishuPC(userAgent)) {
            return "feishu_pc";
        } else {
            return "other";
        }
    }
}