package com.kingdee.eas.custom.integration.feishu.service.detection;

import com.kingdee.eas.custom.integration.base.bizEnum.DeviceType;
import com.kingdee.eas.custom.integration.base.service.detection.PlatformDetector;
import org.springframework.stereotype.Component;

/**
 * 飞书客户端检测器，区分移动端和桌面端。
 *
 * <p>移动端 UA 特征：含 Lark / Feishu 且含 Mobile / Android / iPhone / iPad / iOS</p>
 * <p>桌面端 UA 特征：含 Lark / Feishu 且不含上述移动端关键词</p>
 */
@Component
public class FeishuPlatformDetector implements PlatformDetector {

    private static final String UA_LARK = "Lark";
    private static final String UA_FEISHU = "Feishu";
    // 移动端关键词
    private static final String[] UA_MOBILE_KEYWORDS = {
            "Mobile", "Android", "iPhone", "iPad", "iOS"
    };

    private boolean isFeishu(String ua) {
        return ua.contains(UA_LARK) || ua.contains(UA_FEISHU);
    }

    @Override
    public String detect(String userAgent) {
        if (userAgent == null || userAgent.trim().isEmpty()) return null;
        if (!isFeishu(userAgent)) return null;

        DeviceType device = detectDevice(userAgent);
        return device != null ? platform() + "_" + device.name().toLowerCase() : platform();
    }

    @Override
    public String platform() {
        return "feishu";
    }

    @Override
    public DeviceType detectDevice(String userAgent) {
        if (userAgent == null || userAgent.trim().isEmpty()) return null;
        if (!isFeishu(userAgent)) return null;

        for (String keyword : UA_MOBILE_KEYWORDS) {
            if (userAgent.contains(keyword)) {
                return DeviceType.MOBILE;
            }
        }
        return DeviceType.DESKTOP;
    }
}
