const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
const ktShellNode = {
  sendMsgHandle: null,
  init(sendMsgHandle) {
    if (sendMsgHandle) {
      this.sendMsgHandle = sendMsgHandle;
    }
  },
  /**
   * 文件是否存在
   */
  exists(data) {
    return new Promise((resolve) => {
      let result = fs.existsSync(data.path);
      if (this.sendMsgHandle) {
        this.sendMsgHandle({
          type: data.type,
          data: result,
        });
        resolve();
      } else {
        resolve({
          type: data.type,
          data: result,
        });
      }
    });
  },
  /**
   * 读取文件
   * @param {*} data
   * @returns
   */
  readFile(data) {
    return new Promise((resolve) => {
      fs.readFile(data.path, (err, result) => {
        if (this.sendMsgHandle) {
          this.sendMsgHandle({
            type: data.type,
            data: result,
            err: err,
          });
          resolve();
        } else {
          resolve({
            type: data.type,
            data: result,
            err: err,
          });
        }
      });
    });
  },
  /**
   * 将数据写入文件。
   * @param {*} data
   * @returns
   */
  writeFile(data = {}) {
    return new Promise((resolve) => {
      // 如果data.message模板传的是buffer类型 需要用 Buffer.from(data.message) 转换一下才能正常用
      if (data.message&&data.message.type === 'Buffer') {
        data.message = Buffer.from(data.message);
      }
      fs.writeFile(data.path, data.message, (err) => {
        if (this.sendMsgHandle) {
          this.sendMsgHandle({
            type: data.type,
            err: err,
          });
          resolve();
        } else {
          resolve({
            type: data.type,
            err: err,
          });
        }
      });
    });
  },
  /**
   * 追加数据到一个文件
   * @param {*} data
   * @returns
   */
  appendFile(data) {
    return new Promise((resolve) => {
      fs.appendFile(data.path, data.message, (err) => {
        if (this.sendMsgHandle) {
          this.sendMsgHandle({
            type: data.type,
            err: err,
          });
          resolve();
        } else {
          resolve({
            type: data.type,
            err: err,
          });
        }
      });
    });
  },
  /**
   * 删除文件方法
   * @param {*} data
   * @returns
   */
  unlink(data) {
    return new Promise((resolve) => {
      fs.unlink(data.path, (err) => {
        if (this.sendMsgHandle) {
          this.sendMsgHandle({
            type: data.type,
            err: err,
          });
          resolve();
        } else {
          resolve({
            type: data.type,
            err: err,
          });
        }
      });
    });
  },
  /**
   * 删除空文件夹
   * @param {*} data
   * @returns
   */
  rmdir(data) {
    return new Promise((resolve) => {
      fs.rmdir(data.path, (err) => {
        if (this.sendMsgHandle) {
          this.sendMsgHandle({
            type: data.type,
            err: err,
          });
          resolve();
        } else {
          resolve({
            type: data.type,
            err: err,
          });
        }
      });
    });
  },
  /**
   * 读取指定目录下的文件和子目录列表。
   * @param {*} data
   * @returns
   */
  readdir(data) {
    return new Promise((resolve) => {
      fs.readdir(data.path, (err, files) => {
        if (this.sendMsgHandle) {
          this.sendMsgHandle({
            type: data.type,
            err: err,
            data: files,
          });
          resolve();
        } else {
          resolve({
            type: data.type,
            data: files,
            err: err,
          });
        }
      });
    });
  },
  /**
   * 重命名
   * @param {*} data
   * @returns
   */
  rename(data) {
    return new Promise((resolve) => {
      fs.rename(data.oldPath, data.newPath, (err) => {
        if (this.sendMsgHandle) {
          this.sendMsgHandle({
            type: data.type,
            err: err,
          });
          resolve();
        } else {
          resolve({
            type: data.type,
            err: err,
          });
        }
      });
    });
  },
  /**
   * 更改文件或目录的权限模式。
   * @param {*} data
   * @returns
   */
  chmod(data) {
    return new Promise((resolve) => {
      fs.chmod(data.path, data.mode, (err) => {
        if (this.sendMsgHandle) {
          this.sendMsgHandle({
            type: data.type,
            err: err,
          });
          resolve();
        } else {
          resolve({
            type: data.type,
            err: err,
          });
        }
      });
    });
  },
  /**
   * 使用平台特定的分隔符把全部给定的 path 片段连接到一起，并规范化生成的路径
   * @param {*} data
   * @returns
   */
  join(data) {
    let result = path.join(...data.paths);
    if (this.sendMsgHandle) {
      this.sendMsgHandle({
        type: data.type,
        data: result,
      });
    } else {
      return {
        type: data.type,
        data: result,
      };
    }
  },
  /**
   * 把一个路径或路径片段的序列解析为一个绝对路径。
   * @param {*} data
   * @returns
   */
  resolve(data) {
    let result = path.resolve(...data.paths);
    if (this.sendMsgHandle) {
      this.sendMsgHandle({
        type: data.type,
        data: result,
      });
    } else {
      return {
        type: data.type,
        data: result,
      };
    }
  },

  /**
   * 执行命令
   * @param {*} data
   * @returns
   */
  exec(data) {
    return new Promise((resolve) => {
      child_process.exec(data.command, data.options, (err, stdout, stderr) => {
        if (this.sendMsgHandle) {
          this.sendMsgHandle({
            type: data.type,
            data: stdout,
            err: err,
            stderr,
          });
          resolve();
        } else {
          resolve({
            type: data.type,
            data: stdout,
            err: err,
            stderr,
          });
        }
      });
    });
  },
};
module.exports = ktShellNode;
