package com.ponzischeme89.memby.ui.player

import androidx.media3.common.PlaybackException

/**
 * A viewer-facing description of a Media3 failure. Keep this independent from Activity
 * state so the retry decision remains deterministic and unit-testable.
 */
internal data class PlaybackFailure(
    val title: String,
    val detail: String,
    val canAutoRetry: Boolean,
    val requiresTranscode: Boolean = false,
    val requiresFreshStream: Boolean = false,
)

internal fun describePlaybackFailure(errorCode: Int): PlaybackFailure =
    when (errorCode) {
        PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED,
        PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT,
        PlaybackException.ERROR_CODE_IO_UNSPECIFIED,
        PlaybackException.ERROR_CODE_TIMEOUT,
        -> PlaybackFailure(
            title = "Connection interrupted",
            detail = "Memby couldn’t keep a reliable connection to the media server.",
            canAutoRetry = true,
        )

        PlaybackException.ERROR_CODE_IO_BAD_HTTP_STATUS,
        PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND,
        PlaybackException.ERROR_CODE_IO_INVALID_HTTP_CONTENT_TYPE,
        PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE,
        -> PlaybackFailure(
            title = "Video unavailable",
            detail = "The media server couldn’t provide this video. It may have moved or be temporarily unavailable.",
            canAutoRetry = true,
            requiresFreshStream = true,
        )

        PlaybackException.ERROR_CODE_IO_CLEARTEXT_NOT_PERMITTED,
        PlaybackException.ERROR_CODE_IO_NO_PERMISSION,
        -> PlaybackFailure(
            title = "Playback blocked",
            detail = "The TV is not permitted to open this stream. Check the server address and access settings.",
            canAutoRetry = false,
        )

        PlaybackException.ERROR_CODE_DECODER_INIT_FAILED,
        PlaybackException.ERROR_CODE_DECODING_FAILED,
        PlaybackException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED,
        PlaybackException.ERROR_CODE_DECODING_FORMAT_EXCEEDS_CAPABILITIES,
        -> PlaybackFailure(
            title = "Video format not supported",
            detail = "This TV couldn’t decode the original format. Memby will request a compatible stream.",
            canAutoRetry = true,
            requiresTranscode = true,
        )

        PlaybackException.ERROR_CODE_PARSING_CONTAINER_UNSUPPORTED,
        PlaybackException.ERROR_CODE_PARSING_MANIFEST_UNSUPPORTED,
        -> PlaybackFailure(
            title = "Video file couldn’t be read",
            detail = "This TV couldn’t read the original stream. Memby will request a compatible format.",
            canAutoRetry = true,
            requiresTranscode = true,
        )

        PlaybackException.ERROR_CODE_PARSING_CONTAINER_MALFORMED,
        PlaybackException.ERROR_CODE_PARSING_MANIFEST_MALFORMED,
        -> PlaybackFailure(
            title = "Video file couldn’t be read",
            detail = "The media server returned a malformed stream.",
            canAutoRetry = false,
        )

        else -> PlaybackFailure(
            title = "Playback stopped",
            detail = "Memby hit an unexpected playback problem.",
            canAutoRetry = false,
        )
    }

internal fun automaticRetryDelayMs(attempt: Int): Long? =
    when (attempt) {
        1 -> 1_000L
        2 -> 3_000L
        else -> null
    }

/** One prolonged mid-programme rebuffer gets a lower-risk H.264 stream. */
internal fun shouldRecoverProlongedRebuffer(
    renderedFirstFrame: Boolean,
    prerollActive: Boolean,
    seekBuffering: Boolean,
    recoveryAttempted: Boolean,
): Boolean = renderedFirstFrame && !prerollActive && !seekBuffering && !recoveryAttempted
