package com.foxx.androidcast.media;

/*********************************************************************
 * CodecPriorityId.java
 * Created at: Mon 18 May 2026 18:59:14 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * enum CodecPriorityId
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 103 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	43f0c38b4db03f970badd018a05f0d173081f3f241f54644c2f6d3858dbc4b3d
**********************************************************************/
import android.media.MediaFormat;

import com.foxx.androidcast.CastSettings;

/** Canonical codec id for scoring tables and {@code codecs.json} keys. */
public enum CodecPriorityId {
    H264("h264"),
    H265("h265"),
    VP8("vp8"),
    VP9("vp9"),
    PASSTHROUGH("passthrough"),
    AAC("aac"),
    OPUS("opus"),
    SPEEX("speex"),
    PCM("pcm");

    public final String jsonKey;

    CodecPriorityId(String jsonKey) {
        this.jsonKey = jsonKey;
    }

    public static CodecPriorityId fromVideoMime(String mime) {
        if (mime == null) {
            return null;
        }
        if (MediaFormat.MIMETYPE_VIDEO_AVC.equalsIgnoreCase(mime)) {
            return H264;
        }
        if (MediaFormat.MIMETYPE_VIDEO_HEVC.equalsIgnoreCase(mime)) {
            return H265;
        }
        if (MediaFormat.MIMETYPE_VIDEO_VP8.equalsIgnoreCase(mime)) {
            return VP8;
        }
        if (MediaFormat.MIMETYPE_VIDEO_VP9.equalsIgnoreCase(mime)) {
            return VP9;
        }
        return null;
    }

    public static CodecPriorityId fromVideoPreference(CastSettings.VideoCodec codec) {
        if (codec == null) {
            return null;
        }
        switch (codec) {
            case H264:
                return H264;
            case H265:
                return H265;
            case LIBVPX_VP8:
                return VP8;
            case LIBVPX_VP9:
                return VP9;
            case PASSTHROUGH:
                return PASSTHROUGH;
            case AUTO:
            default:
                return null;
        }
    }

    public static CodecPriorityId fromAudioPreference(CastSettings.AudioCodec codec) {
        if (codec == null) {
            return null;
        }
        switch (codec) {
            case AAC:
                return AAC;
            case OPUS:
                return OPUS;
            case SPEEX:
                return SPEEX;
            case PASSTHROUGH_DEBUG:
                return PCM;
            case AUTO:
            default:
                return null;
        }
    }

    public String toMime() {
        switch (this) {
            case H264:
                return MediaFormat.MIMETYPE_VIDEO_AVC;
            case H265:
                return MediaFormat.MIMETYPE_VIDEO_HEVC;
            case VP8:
                return MediaFormat.MIMETYPE_VIDEO_VP8;
            case VP9:
                return MediaFormat.MIMETYPE_VIDEO_VP9;
            default:
                return null;
        }
    }
}
