package audio
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class AudioUtility {

    private val logger : Logger = LoggerFactory.getLogger(AudioUtility::class.java)



    companion object{
        private val bass = Bass.Instance

        fun DetectPlaybackDevices() : List<Pair<Int, String>> {
            val result = ArrayList<Pair<Int, String>>()
            for(i in 0..10){
                val dev = Bass.BASS_DEVICEINFO()
                if (bass.BASS_GetDeviceInfo(i, dev)){
                    if (dev.flags and Bass.BASS_DEVICE_ENABLED != 0){
                        result.add(Pair(i, dev.name))
                    }
                }
            }
            return result
        }

        fun InitDevice(deviceID: Int, device_samplingrate: Int = 48000) : Boolean {
            val dev = Bass.BASS_DEVICEINFO()
            if (bass.BASS_GetDeviceInfo(deviceID, dev)) {
                if (dev.flags and Bass.BASS_DEVICE_ENABLED > 0){
                    if (dev.flags and Bass.BASS_DEVICE_INIT > 0){
                        return true     // sudah init
                    } else {
                        val initflag = Bass.BASS_DEVICE_16BITS or Bass.BASS_DEVICE_MONO
                        return bass.BASS_Init(deviceID, device_samplingrate,initflag)
                    }
                }
            }
            return false        // gagal GetDeviceInfo
        }

        /**
         * Check if the device is having some handles opened.
         * @param deviceID the device ID to check
         * @return true if the device is opening something, false otherwise
         */
        fun DeviceIsOpeningSomething(deviceID: Int) : Boolean {
            return bass.BASS_GetConfig(Bass.BASS_CONFIG_HANDLES) > 0

        }

        fun Free(){
            bass.BASS_Free()
        }
    }

    init{
        logger.info("Bass Version = ${bass.BASS_GetVersion().toHexString()}")
    }



}