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', {
  // "PlaySound": [ref.types.bool, [ref.types.CString, ref.types.int32, ref.types.uint32]],
  // "mciSendCommandA": [
  //     ref.types.uint64,
  //     [ref.types.uint32, ref.types.uint32, ref.types.uint64, ref.types.uint64]
  // ],
  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);
      }
      //api.mciSendStringW(cmdPlay, "", 0, null);
      // var cmdOpen = "open " + audioFilePath + " alias audio";
      // var cmdPlay = "play audio";
      // api.mciSendStringW(cmdOpen, "", 0, null);
      // api.mciSendStringW(cmdPlay, "", 0, null);

      //PlaySound Api
      // const SND_FILENAME = 0x00020000;
      // const SND_ASYNC = 0x0001;
      // var resolvePath = path.resolve("./tts_sample.wav");
      // var absolutePath = "E:\\Desktop\\Code\\Demo\\ElectronDemo\\tts_sample.wav";
      // var result = api.PlaySound(absolutePath, 0, SND_ASYNC | SND_FILENAME);
    } 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;
