var electron = require('electron');
var path = require('path');
var ffi = require('ffi-napi'); // electron.remote ? electron.remote.require("ffi-napi") : require("ffi-napi");
var ref = require('ref-napi'); // electron.remote ? electron.remote.require("ref-napi") : require("ref-napi");
console.log('MCISendStringCmds begin load');
var api = ffi.Library('winmm.dll', {
  mciSendStringA: [
    ref.types.ulong,
    [
      ref.types.CString,
      ref.types.CString,
      ref.types.uint32,
      ref.refType(ref.types.void),
    ],
  ],
});
const { PlayCmds } = require('../TTSEnums');
class MCISendStringApi {
  static playAudio(audioFilePath, cmd) {
    try {
      if (cmd == PlayCmds.Play) {
        api.mciSendStringA('close audio', '', 0, null);
        api.mciSendStringA(`open ${audioFilePath} alias audio`, '', 0, null);
        api.mciSendStringA('play audio', '', 0, null);
        return 0;
      } else if (cmd == PlayCmds.Stop) {
        api.mciSendStringA('close audio', '', 0, null);
      }
    } catch (e) {
      debugger;
      return 0;
    }
  }
  static playStatus() {
    var status = ref.alloc(ref.refType(ref.types.void));
    var result = api.mciSendStringA(`status audio mode`, status, 128, null);
    var statusValue = ref.reinterpret(
      status,
      status.length >= 128 ? 128 : status.length
    );
    return statusValue.toString();
  }
  static stopPlay() {
    api.mciSendStringA('close audio', '', 0, null);
  }
}
module.exports = MCISendStringApi;
