/**
* This file is an exception. It's licensed CC-0.
*/
class SoundTest implements JSTerminal.Program {

   stdio: Std.IO;
   sound: Std.Sound;

   create(terminal: JSTerminal.Terminal, stdio: Std.IO) {
      this.stdio = stdio;

      stdio.loadSound("audio/gripofnature.ogg", (ref) => {
         this.sound = {
            soundRefrence: ref,
            onStartCallback: () => {
               console.log("I started!");
            },
            onEndCallback: () => {
               console.log("I ended :(");
            }
         };

         stdio.println("Welcome to the soundtester. Simply input some of these commands to begin:");
         stdio.println("playnew 0\nplay 0\nstop 0\npause 0\nplayloop 0", "red");
         stdio.println("\n<command> <channel> <volume (0-1)>\n", "red");
         this.takeCommand();
      });
   }

   enable() {

   }

   disable() {

   }

   onClose() {
      return true;
   }

   takeCommand() {
      this.stdio.readline({
         callback: (res) => {
            let parts = res.split(" ");
            let command = parts[0];
            let channel = parseInt(parts[1]);
            let volume = parseFloat(parts[2]) || 1;
            console.log(volume);
            if (!isNaN(channel)) {
               this.sound.loops = false;
               this.sound.volume = volume;
               if (command == "play") {
                  this.stdio.playSound(channel);
               } else if (command == "pause") {
                  this.stdio.pauseSound(channel);
               } else if (command == "stop") {
                  this.stdio.stopSound(channel);
               } else if (command == "playnew") {
                  this.stdio.playSound(channel, this.sound);
               } else if (command == "playloop") {
                  this.sound.loops = true;
                  this.stdio.playSound(channel, this.sound);
               }
            }
            this.takeCommand();
         },
         prefix: "\n> "
      })
   }

}
