package com.mrousavy.camera.core

import android.media.MediaCodec
import android.media.MediaCodec.BufferInfo
import android.media.MediaCodecInfo
import android.media.MediaFormat
import android.util.Log
import android.util.Size
import android.view.Surface
import com.mrousavy.camera.types.Orientation
import com.mrousavy.camera.types.RecordVideoOptions
import java.io.File

/**
 * A recording manager that produces HLS-compatible fragmented MP4 segments.
 *
 * Uses HlsMuxer (following Android's MediaMuxer pattern) to produce:
 * - init.mp4: Initialization segment (ftyp + moov with mvex)
 * - 0.mp4, 1.mp4, ...: Media segments (moof + mdat)
 */
class FragmentedRecordingManager(
    private val encoder: MediaCodec,
    private val muxer: HlsMuxer
) : MediaCodec.Callback(), ChunkedRecorderInterface {

    companion object {
        private const val TAG = "FragmentedRecorder"
        private const val DEFAULT_SEGMENT_DURATION_SECONDS = 6

        fun fromParams(
            callbacks: CameraSession.Callback,
            size: Size,
            enableAudio: Boolean,
            fps: Int? = null,
            cameraOrientation: Orientation,
            bitRate: Int,
            options: RecordVideoOptions,
            outputDirectory: File,
            segmentDurationSeconds: Int = DEFAULT_SEGMENT_DURATION_SECONDS
        ): FragmentedRecordingManager {
            val mimeType = options.videoCodec.toMimeType()
            // Use cameraOrientation (from WindowManager) for rotation metadata
            // The options.orientation from JavaScript is unreliable on Android when rotating between landscape modes
            val orientationDegrees = cameraOrientation.toDegrees()

            // Swap dimensions based on camera orientation, same as ChunkedRecordingManager
            val (width, height) = if (cameraOrientation.isLandscape()) {
                size.height to size.width
            } else {
                size.width to size.height
            }

            Log.d(TAG, "Recording: ${width}x${height}, orientation=$orientationDegrees°")

            val format = MediaFormat.createVideoFormat(mimeType, width, height)
            val codec = MediaCodec.createEncoderByType(mimeType)

            format.setInteger(
                MediaFormat.KEY_COLOR_FORMAT,
                MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface
            )

            // Use 30fps as conservative default since many Android devices can't sustain
            // higher frame rates at high resolutions. This affects:
            // - Encoder: bitrate allocation and I-frame interval calculation
            // - HlsMuxer: timescale for accurate sample durations
            // The actual frame timing comes from camera timestamps regardless of this setting.
            val effectiveFps = 30
            format.setInteger(MediaFormat.KEY_FRAME_RATE, effectiveFps)
            format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, segmentDurationSeconds)
            format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate)

            codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)

            val muxer = HlsMuxer(
                outputDirectory = outputDirectory,
                callback = object : HlsMuxer.Callback {
                    override fun onInitSegmentReady(file: File) {
                        callbacks.onInitSegmentReady(file)
                    }

                    override fun onMediaSegmentReady(file: File, index: Int, durationUs: Long) {
                        callbacks.onVideoChunkReady(file, index, durationUs)
                    }
                },
                orientationDegrees = orientationDegrees,
                fps = effectiveFps
            )
            muxer.setSegmentDuration(segmentDurationSeconds * 1_000_000L)

            return FragmentedRecordingManager(codec, muxer)
        }
    }

    private var recording = false
    private var muxerStarted = false
    private var trackIndex = -1

    override val surface: Surface = encoder.createInputSurface()

    init {
        encoder.setCallback(this)
    }

    override fun start() {
        encoder.start()
        recording = true
    }

    override fun finish() {
        synchronized(this) {
            recording = false

            if (muxerStarted) {
                muxer.stop()
                muxer.release()
            }

            try {
                encoder.stop()
                encoder.release()
            } catch (e: Exception) {
                Log.e(TAG, "Error stopping encoder", e)
            }
        }
    }

    // MediaCodec.Callback methods

    override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {
        // Not used for Surface input
    }

    override fun onOutputBufferAvailable(codec: MediaCodec, index: Int, bufferInfo: BufferInfo) {
        synchronized(this) {
            if (!recording) {
                encoder.releaseOutputBuffer(index, false)
                return
            }

            if (!muxerStarted) {
                encoder.releaseOutputBuffer(index, false)
                return
            }

            val buffer = encoder.getOutputBuffer(index)
            if (buffer == null) {
                Log.e(TAG, "getOutputBuffer returned null")
                encoder.releaseOutputBuffer(index, false)
                return
            }

            try {
                muxer.writeSampleData(trackIndex, buffer, bufferInfo)
            } catch (e: Exception) {
                Log.e(TAG, "Error writing sample", e)
            }

            encoder.releaseOutputBuffer(index, false)
        }
    }

    override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) {
        Log.e(TAG, "Codec error: ${e.message}")
    }

    override fun onOutputFormatChanged(codec: MediaCodec, format: MediaFormat) {
        synchronized(this) {
            Log.i(TAG, "Output format changed: $format")

            trackIndex = muxer.addTrack(format)
            muxer.start()
            muxerStarted = true
        }
    }
}
