package tv.tivilux.nativeplayer.data;

/** Pure monotonic-clock policy. No player, UI, wall clock, or background threads. */
public final class LiveRecoveryPolicy {
    private long lastProgress;
    private long stableSince;
    private int attempts;
    private boolean exhausted;

    public LiveRecoveryPolicy(long now) { lastProgress = now; stableSince = now; }

    /** 0=no action; 1=retry; 2=live edge; 3=fresh media source. */
    public int sample(long now, boolean active, boolean progressed, boolean failed) {
        if (!active) {
            lastProgress = now;
            stableSince = now;
            return 0;
        }
        if (progressed && !failed) {
            lastProgress = now;
            if (now - stableSince >= 60_000L) { attempts = 0; exhausted = false; }
            return 0;
        }
        stableSince = now;
        long wait = failed ? (attempts == 0 ? 2_000L : attempts == 1 ? 5_000L : 10_000L)
                           : (attempts == 0 ? 20_000L : attempts == 1 ? 30_000L : 45_000L);
        if (exhausted || now - lastProgress < wait) return 0;
        lastProgress = now;
        attempts++;
        if (attempts == 3) exhausted = true;
        return attempts;
    }

    public boolean isExhausted() { return exhausted; }
}
