package com.foxx.androidcast.discovery;

/*********************************************************************
 * ReceiverCompatibility.java
 * Created at: Sun 17 May 2026 22:49:49 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class ReceiverCompatibility
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 52 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	c38fdecb8b4800e6d3ef26bf677413cf454d96b640d8783129a47b3301cb4fc9
**********************************************************************/
import com.foxx.androidcast.CastSettings;
import com.foxx.androidcast.VersionInfo;
import com.foxx.androidcast.network.CastCodecFlags;

/** Filters discovered receivers the sender can actually cast to. */
public final class ReceiverCompatibility {
    private ReceiverCompatibility() {}

    public static boolean isCompatible(DiscoveryManager.DiscoveredDevice device, CastSettings sender) {
        if (device == null || sender == null) {
            return false;
        }
        if (device.protoVersion != 0L && device.protoVersion != VersionInfo.PROTO_VERSION) {
            return false;
        }
        if (device.transport == null || !device.transport.equalsIgnoreCase(sender.getTransport())) {
            return false;
        }
        int senderMask = senderVideoMask(sender.getVideoCodec());
        if (device.videoCodecs != 0 && senderMask != 0 && (device.videoCodecs & senderMask) == 0) {
            return false;
        }
        return true;
    }

    private static int senderVideoMask(CastSettings.VideoCodec codec) {
        if (codec == null || codec == CastSettings.VideoCodec.AUTO) {
            return CastCodecFlags.localVideoMask();
        }
        switch (codec) {
            case H264:
                return CastCodecFlags.VIDEO_H264;
            case H265:
                return CastCodecFlags.VIDEO_H265;
            case LIBVPX_VP8:
                return CastCodecFlags.VIDEO_VP8;
            case LIBVPX_VP9:
                return CastCodecFlags.VIDEO_VP9;
            default:
                return CastCodecFlags.localVideoMask();
        }
    }
}
