const edge = require('electron-edge');
const path = require('path');
const clone = require('../core/clone');

// 使用 Dll 擴充功能
module.exports = {

  // 回調
  msBuild: (event, args) => {
    var compile = edge.func({
      assemblyFile: path.join(process.env['APPDATA'], 'Bumblebee', 'lib', 'NetNative.dll'),
      typeName: 'NetNative.MsExtensions',
      methodName: 'Compile'
    });

    const option = {
      sourcePath: args.sourcePath,
      outputPath: args.outputPath
    };

    const log = compile(option, true);
    event.sender.send('console-log', log);
  },

  // 承諾
  msBuildPromise: (_option) => {
    return new Promise((resolve, reject) => {
      let option = clone(_option);

      global.BumblebeeLog.info('msBuildPromise 啟動');

      var compile = edge.func({
        assemblyFile: path.join(process.env['APPDATA'], 'Bumblebee', 'lib', 'NetNative.dll'),
        typeName: 'NetNative.MsExtensions',
        methodName: 'Compile'
      });

      const dotnatOption = {
        sourcePath: option.sourcePath,
        outputPath: option.outputPath,
        nugetDir: path.join(process.env['APPDATA'], 'Bumblebee', 'lib')
      };

      const log = compile(dotnatOption, function (err, logData) {
        if (err) {
          global.BumblebeeLog.info('msBuildPromise 錯誤' + JSON.stringify(err));

          reject(err);
        }
        global.BumblebeeLog.info('msBuildPromise 完成');

        resolve(logData);
      });
    });
  }
};
