const DEBUG = process.env.debug | false;

var p = require("./vlc_rc");

var player = new p.VLC_RC({verbose:true, debug:true, manual:false});
var http = require("http").createServer();

var io = require("socket.io")(http);
var activeFile = undefined;

io.on("connection", (socket)=>{
   console.log("client connected:", socket.client.id);
   socket.on("play", (data)=>{
      player.stop(()=>{
         console.log("Play", data);
         activeFile = data.file;
         player.loadFile(__dirname + "/files/" + data.file, ()=>{
            player.play(()=>{
               Update.start();
               console.log("playing");
               socket.emit("playing", {file: data.file});
            });
         });
      });
   });

   socket.on("stop", (data)=>{
      player.stop(()=>{
         Update.stop();
         io.emit("stopped");
      });
   });
});

class Update {
   static start() {
      console.log("Starte updates");
      Update.interv = setInterval(()=>{
         var start
         var update_infos = {};
         player.getTime((t)=>{
            player.getState((playing)=>{
               update_infos.file = activeFile;
               update_infos.time = t;
               update_infos.is_playing = playing;
               //update_infos.  
               io.emit("update", update_infos);
            })
         });
      }, 1000/25)
   }

   static stop() {
      clearInterval(Update.interv);
   }
}

http.listen(5000);