package zello

import com.fasterxml.jackson.annotation.JsonPropertyOrder
import somecodes.Codes.Companion.ValidString
import somecodes.Codes.Companion.fromCodecHeader

@Suppress("unused")
@JsonPropertyOrder(value = ["command", "type", "codec", "codec_header", "packet_duration", "stream_id", "channel", "from", "for"])
class Event_OnStreamStart {
    var command: String = ""    // must be "on_stream_start"
    var type: String = ""       // must be "audio"
    var codec: String = ""      // must be "opus"
    var codec_header: String = ""
    var packet_duration: Int = 0 // in milliseconds
    var stream_id: Int = 0
    var channel: String=""
    var from: String = "" // the user who started the stream
    var For: String = ""    // the user who is receiving the stream, should be "for" not "For"

    /**
     * Get Sampling Rate from codec_header.
     * @return sampling rate in Hz, or 0 if codec_header is not valid.
     */
    fun getSamplingRate() : Int{
        if (ValidString(codec_header)){
            val triple = fromCodecHeader(codec_header)
            if (triple!=null){
                return triple.first
            }
        }
        return 0
    }

    /**
     * Get number of frames per packet, either 1 or 2
     * @return number of frames per packet, or 0 if codec_header is not valid.
     */
    fun getFramesPerSecond() : Byte{
        if (ValidString(codec_header)){
            val triple = fromCodecHeader(codec_header)
            if (triple!=null){
                return triple.second
            }
        }
        return 0
    }

    /**
     * Get audio frame size in miliseconds
     * @return audio frame size in milliseconds, or 0 if codec_header is not valid.
     */
    fun getFrameSizePerMs() : Byte{
        if (ValidString(codec_header)){
            val triple = fromCodecHeader(codec_header)
            if (triple!=null){
                return triple.third
            }
        }
        return 0
    }
}