
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");
var PlayerStateEnum = ["libvlc_NothingSpecial", "libvlc_Opening", "libvlc_Buffering", "libvlc_Playing", "libvlc_Paused", "libvlc_Stopped", "libvlc_Ended", "libvlc_Error"];
console.log("VLCPlayerApi begin load");
const vlcApi = ffi.Library("libvlc.so", {
    "libvlc_new": [ref.refType(ref.types.void), ["int", "string"]],
    "libvlc_media_player_new": [ref.refType(ref.types.void), [ref.refType(ref.types.void)]],
    "libvlc_media_new_path": [ref.refType(ref.types.void), [ref.refType(ref.types.void), "string"]],
    "libvlc_media_player_set_media": [ref.types.void, [ref.refType(ref.types.void), ref.refType(ref.types.void)]],
    "libvlc_media_player_play": [ref.types.int, [ref.refType(ref.types.void)]],
    "libvlc_media_player_is_playing": [ref.types.bool, [ref.refType(ref.types.void)]],
    "libvlc_media_player_set_pause": [ref.types.void, [ref.refType(ref.types.void), ref.types.int]],
    "libvlc_media_player_stop": [ref.types.int, [ref.refType(ref.types.void)]],
    "libvlc_media_release": [ref.types.void, [ref.refType(ref.types.void)]],
    "libvlc_media_player_release": [ref.types.void, [ref.refType(ref.types.void)]],
    "libvlc_release": [ref.types.void, [ref.refType(ref.types.void)]],
    "libvlc_media_player_get_position": [ref.types.float, [ref.refType(ref.types.void)]],
    "libvlc_media_player_get_state": [ref.types.int32, [ref.refType(ref.types.void)]]
});
const { PlayCmds, PlayMode } = require("../TTSEnums");

class VLCPlayerApi {
    static playAudio(audioFilePath, cmd, playMode) {
        try {
            if (cmd == PlayCmds.Stop) {
                if (VLCPlayerApi.vlcInstance &&
                    VLCPlayerApi.vlcMediaPlayerInstance &&
                    VLCPlayerApi.vlcMediaInstance
                ) {
                    let stopMediaPlayerResult = vlcApi.libvlc_media_player_stop(VLCPlayerApi.vlcMediaPlayerInstance);
                    console.log("指令：" + cmd + ",停止播放API 返回:" + stopMediaPlayerResult);
                    //vlcApi.libvlc_media_player_release(VLCPlayerApi.vlcMediaPlayerInstance);
                    //vlcApi.libvlc_media_release(VLCPlayerApi.vlcMediaInstance);
                    //vlcApi.libvlc_media_player_set_pause(VLCPlayerApi.vlcMediaPlayerInstance, 1);
                    //vlcApi.libvlc_media_release(VLCPlayerApi.vlcMediaInstance);
                    VLCPlayerApi.currentPlayMode = playMode;
                }
                return;
            }
            if (cmd == PlayCmds.Play) {
                let vlcInstance = null;
                let vlcMediaPlayerInstance = null;
                let vlcMediaInstance = null;
                vlcInstance = VLCPlayerApi.vlcInstance;
                if (ref.isNull(vlcInstance)) {
                    return;
                }
                vlcMediaPlayerInstance = VLCPlayerApi.vlcMediaPlayerInstance
                if (ref.isNull(vlcMediaPlayerInstance)) {
                    return;
                }
                let position = vlcApi.libvlc_media_player_get_position(VLCPlayerApi.vlcMediaPlayerInstance);
                let result = vlcApi.libvlc_media_player_get_state(vlcMediaPlayerInstance);
                console.log("position=" + position + ",PlayerState=" + result + "," + PlayerStateEnum[result]);
                if (VLCPlayerApi.vlcMediaInstance &&
                    [1, 2, 3].indexOf(result) >= 0 &&
                    playMode != PlayMode.Override &&
                    playMode != PlayMode.Clear
                ) {
                    return -1;
                }
                let wavFilePath = audioFilePath;
                VLCPlayerApi.vlcMediaInstance = vlcMediaInstance = vlcApi.libvlc_media_new_path(vlcInstance, wavFilePath);
                if (ref.isNull(vlcMediaInstance)) {
                    return;
                }
                //设置要播放的音频文件。
                vlcApi.libvlc_media_player_set_media(vlcMediaPlayerInstance, vlcMediaInstance);
                //play
                let playResult = vlcApi.libvlc_media_player_play(vlcMediaPlayerInstance);
                console.log("libvlc_media_player_play 返回：" + playResult);
                let state = vlcApi.libvlc_media_player_get_state(vlcMediaPlayerInstance);
                console.log("PlayerState=" + state + "," + PlayerStateEnum[state]);
                return 0;

                // let isPlaying = vlcApi.libvlc_media_player_is_playing(vlcMediaPlayerInstance);
                // var test = false;
                // vlcApi.libvlc_media_player_set_pause(vlcMediaPlayerInstance, 1);
                // isPlaying = vlcApi.libvlc_media_player_is_playing(vlcMediaPlayerInstance);

                // vlcApi.libvlc_media_player_set_pause(vlcMediaPlayerInstance, 0);
                // isPlaying = vlcApi.libvlc_media_player_is_playing(vlcMediaPlayerInstance);

                // test = true;
                // let stopMediaPlayerResult = vlcApi.libvlc_media_player_stop(vlcMediaPlayerInstance);
                // test = true;
            }
        }
        catch (e) {
            debugger;
            return 0;
        }
    }
    static playStatus() {
        let playStatus = "";
        if (VLCPlayerApi.vlcInstance &&
            VLCPlayerApi.vlcMediaPlayerInstance
        ) {
            // let isPlaying = vlcApi.libvlc_media_player_is_playing(VLCPlayerApi.vlcMediaPlayerInstance);
            // playStatus = isPlaying ? "playing" : "other";
            let result = vlcApi.libvlc_media_player_get_state(VLCPlayerApi.vlcMediaPlayerInstance);
            console.log("get playStatus=" + result + "," + PlayerStateEnum[result]);
            playStatus = [1, 2, 3].indexOf(result) >= 0 ? "playing" : "other";
        }
        else {
            playStatus = "other";
        }
        //
        // if (playStatus != "playing") {
        //     let position = vlcApi.libvlc_media_player_get_position(VLCPlayerApi.vlcMediaPlayerInstance);
        //     console.log("playStatus position=" + position);
        //     if (VLCPlayerApi.vlcMediaInstance && position >= 0 && position < 1) {
        //         playStatus = "playing";
        //     }
        // }
        return playStatus;
    }
    static stopPlay() {
        if (VLCPlayerApi.vlcInstance &&
            VLCPlayerApi.vlcMediaPlayerInstance &&
            VLCPlayerApi.vlcMediaInstance
        ) {
            let stopMediaPlayerResult = vlcApi.libvlc_media_player_stop(VLCPlayerApi.vlcMediaPlayerInstance);
            console.log("stopPlay->停止播放API 返回:" + stopMediaPlayerResult);
        }
    }
    static dispose() {
        try {
            VLCPlayerApi.stopPlay();
            if (VLCPlayerApi.vlcMediaInstance) {
                vlcApi.libvlc_media_release(VLCPlayerApi.vlcMediaInstance);
                VLCPlayerApi.vlcMediaInstance=null;
            }
            if (VLCPlayerApi.vlcMediaPlayerInstance) {
                vlcApi.libvlc_media_player_release(VLCPlayerApi.vlcMediaPlayerInstance);
                VLCPlayerApi.vlcMediaPlayerInstance=null;
            }
            if (VLCPlayerApi.vlcInstance) {
                vlcApi.libvlc_release(VLCPlayerApi.vlcInstance);
                VLCPlayerApi.vlcInstance=null;
            }
            console.log("VLC Disposed");
        }
        catch (e) {
            console.error("vlc dispose exception:", e);
        }
    }
}
VLCPlayerApi.vlcInstance = null;
VLCPlayerApi.vlcMediaPlayerInstance = null;
VLCPlayerApi.vlcMediaInstance = null;
if (!VLCPlayerApi.vlcInstance) {
    VLCPlayerApi.vlcInstance = vlcApi.libvlc_new(0, ref.NULL);
}
VLCPlayerApi.vlcMediaPlayerInstance = vlcApi.libvlc_media_player_new(VLCPlayerApi.vlcInstance);
module.exports = VLCPlayerApi;