package dev.adriankuta.visualizer.data

import android.media.audiofx.Visualizer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

class AudioVisualizerImpl @Inject constructor(
    private val visualizer: Visualizer
) : AudioVisualizer {

    override fun start() {
       // visualizer.enabled = true
    }

    override fun stop() {
        visualizer.enabled = false
    }

    override fun visualizerFlow(): Flow<VisualizerFrame> = callbackFlow {
        val listener = object : Visualizer.OnDataCaptureListener {
            override fun onFftDataCapture(
                visualizer: Visualizer?,
                fft: ByteArray,
                samplingRate: Int
            ) {
                trySend(FftFrame(fft.copyOf()))
            }

            override fun onWaveFormDataCapture(
                visualizer: Visualizer?,
                waveform: ByteArray,
                samplingRate: Int
            ) {
                trySend(WaveformFrame(waveform.copyOf()))
            }
        }

        val result = visualizer.setDataCaptureListener(
            listener,
            Visualizer.getMaxCaptureRate(),
            true,
            true
        )

        visualizer.enabled = true

        awaitClose {
            runCatching {
                visualizer.enabled = false
                visualizer.setDataCaptureListener(null, 0, false, false)
                visualizer.release()
            }
        }
    }
        .conflate()
        .flowOn(Dispatchers.Default)
}

sealed interface VisualizerFrame {
    val data: ByteArray
}

data class WaveformFrame(
    override val data: ByteArray,
) : VisualizerFrame

data class FftFrame(
    override val data: ByteArray,
) : VisualizerFrame