package dev.adriankuta.visualizer.domain

import androidx.compose.ui.graphics.Color
import dev.adriankuta.visualizer.data.AudioVisualizer
import dev.adriankuta.visualizer.data.FftFrame
import dev.adriankuta.visualizer.data.VisualizerFrame
import dev.adriankuta.visualizer.data.WaveformFrame
import dev.adriankuta.visualizer.domain.processors.VisualizerProcessor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import javax.inject.Inject

class ObserveVisualizerColorUseCase @Inject constructor(
    private val audioVisualizer: AudioVisualizer,
    private val processor: VisualizerProcessor,
) {

    operator fun invoke(): Flow<Color> =
        audioVisualizer.visualizerFlow()
            .process(processor)
            .onStart { emit(Color.Black) }
}

fun Flow<VisualizerFrame>.process(processor: VisualizerProcessor) =
    this.filterIsInstance(WaveformFrame::class)
        .map(processor::invoke)

