package com.foxx.androidcast.diagnostics;

/*********************************************************************
 * CastDiagnosticsScorer.java
 * Created at: Sat 16 May 2026 13:35:48 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class CastDiagnosticsScorer
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 69 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	ee22fcdc1d1d6bce19746ebba8a85a125dee18c403318d0d38d7b5729b1929a2
**********************************************************************/
/** Heuristic stream quality score (0–100) from live metrics. */
public final class CastDiagnosticsScorer {
    private CastDiagnosticsScorer() {}

    public static int compute(float inFps, float renderFps, int bwInKbps, int suggestedBwKbps,
            int rttMs, int congestionLevel, long audioPackets, boolean audioRequested) {
        int score = 100;
        if (inFps > 0 && inFps < 12f) {
            score -= 25;
        } else if (inFps > 0 && inFps < 20f) {
            score -= 12;
        }
        if (inFps > 5f && renderFps > 0 && renderFps < inFps * 0.7f) {
            score -= 22;
        } else if (inFps > 5f && renderFps > 0 && renderFps < inFps * 0.85f) {
            score -= 10;
        }
        if (suggestedBwKbps > 0 && bwInKbps > 0 && bwInKbps < suggestedBwKbps * 0.55f) {
            score -= 20;
        } else if (suggestedBwKbps > 0 && bwInKbps > 0 && bwInKbps < suggestedBwKbps * 0.75f) {
            score -= 10;
        }
        if (rttMs > 600) {
            score -= 18;
        } else if (rttMs > 350) {
            score -= 8;
        }
        if (congestionLevel >= 2) {
            score -= 15;
        } else if (congestionLevel >= 1) {
            score -= 6;
        }
        if (audioRequested && audioPackets == 0) {
            score -= 8;
        }
        if (score < 0) {
            return 0;
        }
        if (score > 100) {
            return 100;
        }
        return score;
    }

    public static String label(int score) {
        if (score >= 85) {
            return "Excellent";
        }
        if (score >= 70) {
            return "Good";
        }
        if (score >= 50) {
            return "Fair";
        }
        if (score >= 30) {
            return "Poor";
        }
        return "Bad";
    }
}
