package solutions.tretter.mindmachine.session

object TimelineRuntimeEvaluator {
    fun evaluate(state: TimelineEditorState, tSec: Float): RuntimeParams {
        val flash01 = state.visualLeft.valueAt(tSec)
        val blank01 = state.visualRight.valueAt(tSec)
        val carrier01 = state.audioLeft.valueAt(tSec)
        val binaural01 = state.audioRight.valueAt(tSec)

        val flashOnMs = ParameterMapping.logMap01(flash01, ParameterRanges.FLASH_INTERVAL_MS_MIN, ParameterRanges.FLASH_INTERVAL_MS_MAX).toInt()
        val flashOffMs = ParameterMapping.logMap01(blank01, ParameterRanges.BLANK_INTERVAL_MS_MIN, ParameterRanges.BLANK_INTERVAL_MS_MAX).toInt()
        val carrierHz = ParameterMapping.logMap01(carrier01, ParameterRanges.CARRIER_HZ_MIN, ParameterRanges.CARRIER_HZ_MAX)
        val binauralHz = ParameterMapping.logMap01(binaural01, ParameterRanges.BINAURAL_HZ_MIN, ParameterRanges.BINAURAL_HZ_MAX)

        return RuntimeParams(
            flashOnMs = flashOnMs.coerceIn(50, 2000),
            flashOffMs = flashOffMs.coerceIn(50, 2000),
            carrierHz = carrierHz.coerceIn(ParameterRanges.CARRIER_HZ_MIN, ParameterRanges.CARRIER_HZ_MAX),
            binauralHz = binauralHz.coerceIn(ParameterRanges.BINAURAL_HZ_MIN, ParameterRanges.BINAURAL_HZ_MAX),
        )
    }
}
