var path = require("path");
var fs = require("fs");
var currentDir = __dirname;
var sdkPath = path.join(currentDir, "libs/arm/libmsc.so");
var sdkTargetPath = sdkPath;
var electron = require("electron");
var app = (electron.remote || electron).app;
var platform = "arm";
console.log("TTSLinuxApi->__dirname="+__dirname);
if (process.arch == "x32") {
    sdkPath = path.join(currentDir, "libs/x86/libmsc.so");
    platform = "x86";
}
else if (process.arch == "x64") {
    sdkPath = path.join(currentDir, "libs/x64/libmsc.so");
    platform = "x64";
}
if (app.getAppPath().indexOf(".asar") >= 0 || app.isPackaged) {
    //currentDir = path.join(app.getAppPath(), `node_modules/@keytop/ktofflinetts/linux/libs/${platform}`);
    //sdkPath = path.join(currentDir, path.basename(sdkPath));
    sdkTargetPath = path.join(app.getPath("appData"), path.basename(sdkPath));
}
if (!fs.existsSync(sdkTargetPath)) {
    fs.writeFileSync(sdkTargetPath, fs.readFileSync(sdkPath));
}
else {
    // var targetInfo = fs.statSync(sdkTargetPath);
    // var itemInfo = fs.statSync(sdkPath);
    // if (itemInfo.mtimeMs > targetInfo.mtimeMs || itemInfo.size > targetInfo.size) {
    //     fs.writeFileSync(sdkTargetPath, fs.readFileSync(sdkPath));
    // }
    let crypto=require("crypto");
    let sourceHash= crypto.createHash("sha256").update(fs.readFileSync(sdkPath)).digest("hex");
    let targetHash=crypto.createHash("sha256").update(fs.readFileSync(sdkTargetPath)).digest("hex");
    if(targetHash!=sourceHash){
        console.log(`update tts sdk ${sdkTargetPath}`);
        fs.writeFileSync(sdkTargetPath, fs.readFileSync(sdkPath));
    }
}
const { PlayMode } = require("../TTSEnums");
const TTSApi = require("../TTSApi")(sdkTargetPath);
const AudioApi = require("./VLCPlayerApi");
function TTSLinuxApi(options) {
    TTSApi.call(this, AudioApi, options);
}
TTSLinuxApi.stopPlay = function () {
    AudioApi.stopPlay();
}
TTSLinuxApi.prototype = new TTSApi();
module.exports = {
    TTSApi: TTSLinuxApi,
    PlayMode: PlayMode
}
