package commandServer

import codes.Somecodes.Companion.PagingResult_directory
import codes.Somecodes.Companion.filenameformat
import java.io.ByteArrayOutputStream
import java.nio.file.Path
import java.time.LocalDateTime

/**
 * Class to handle a paging job, storing incoming audio data and metadata.
 * @param fromIP The IP address from which the paging data is received.
 * @param broadcastzones The zones to which the paging is broadcasted, is a semicolon-separated string.
 */
class PagingJob(val fromIP: String, val broadcastzones: String) {
    val filePath : Path = PagingResult_directory.resolve("PAGING_"+fromIP+"_"+LocalDateTime.now().format(filenameformat)+".wav")
    private val bos : ByteArrayOutputStream = ByteArrayOutputStream()
    var totalBytesReceived = 0; private set
    var isRunning = true; private set

    /**
     * Expected Size from PCMFILE android
     */
    var expectedSize = 0


    /**
     * Adds incoming audio data to the job.
     * @param data The byte array containing audio data.
     * @param length The number of bytes to write from the data array.
     */
    fun addData(data: ByteArray, length: Int) {
        bos.write(data, 0, length)
        totalBytesReceived += length
    }

    /**
     * Retrieves the accumulated audio data as a byte array.
     * @return A byte array containing all received audio data.
     */
    fun GetData(): ByteArray {
        return bos.toByteArray()
    }

    fun Close(){
        bos.close()
        isRunning = false
    }
}