package ocelot.desktop.util

import ocelot.desktop.audio._

trait AudibleComputerAware extends ComputerAware {
  lazy val soundComputerRunning: SoundSource = SoundSource.fromBuffer(
    SoundBuffers.MachineComputerRunning,
    SoundCategory.Environment,
    looping = true,
  ).build()

  def updateRunningSound(): Unit = {
    if (!computer.machine.isRunning && soundComputerRunning.playing) {
      soundComputerRunning.stop()
    } else if (computer.machine.isRunning && !soundComputerRunning.playing && !Audio.isDisabled) {
      soundComputerRunning.play()
    }
  }

  override def turnOn(): Unit = {
    super.turnOn()

    soundComputerRunning.play()
  }

  override def turnOff(): Unit = {
    super.turnOff()

    soundComputerRunning.stop()
  }
}
