const childproc = require('child_process');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const querystring = require('querystring');
const settings = require('../../config/settings');
class RestClient {
  constructor() {
    this.cmdGetPattern = 'curl {-G} -k  -d \'{data}\' {url}';
    this.cmdPostPattern = 'curl -k -H \'Content-type: application/json\' -d \'{data}\' {url}';
    this.cmdDownLoadFilePattern = 'curl -G -o {fileName} {url}';
    this.cmdPostPattern2 = 'curl -k -H \'Content-type: application/x-www-form-urlencoded\'  -d \'{data}\' {url}';
    this.cmdPostPatternWithAK = 'curl -k -H \'Content-type: application/json\'  -H  \'AccessKey:{ak}\'   -d \'{data}\' {url}';

    // form-data形式post  data参数类型 md5=2&data=1
    this.cmdPostPattern5 = 'curl -k --data \'{data}\' {url}';
  }
  FetchGetCmd(subData, url) {
    const cmd = this.cmdGetPattern.replace(/\{-G\}/g, '-G').replace(/\{data\}/g, subData)
      .replace(/\{url\}/g, url);
    return cmd;
  }
  FetchPostCmd(subData, url) {
    const data = JSON.stringify(subData);
    const cmd = this.cmdPostPattern.replace(
      /\{data\}/g,
      data,
    ).replace(/\{url\}/g, url);
    return cmd;
  }
  FetchPostCmdWithAK(subData, url, acck) {
    const data = JSON.stringify(subData);
    const cmd = this.cmdPostPatternWithAK.replace(
      /\{data\}/g,
      data,
    ).replace(/\{url\}/g, url)
      .replace(/\{ak\}/g, acck);
    return cmd;
  }
  FetchPostCmd2(subData, url) {
    const data = subData;
    const cmd = this.cmdPostPattern2.replace(
      /\{data\}/g,
      data,
    ).replace(/\{url\}/g, url);
    return cmd;
  }
  FetchPostCmd3(subData, url) {
    const data = subData;
    const cmd = this.cmdPostPattern3.replace(
      /\{data\}/g,
      data,
    ).replace(/\{url\}/g, url);
    return cmd;
  }
  FetchGetCmd3(url) {
    const cmd = this.cmdGetPattern3.replace(/\{-G\}/g, '-G').replace(/\{url\}/g, url);
    return cmd;
  }
  FetchPostCmd4(subData, url) {
    const data = subData;
    const cmd = this.cmdPostPattern4.replace(
      /\{data\}/g,
      data,
    ).replace(/\{url\}/g, url);
    return cmd;
  }
  FetchPostCmd5(subData, url) {
    const data = subData;
    const cmd = this.cmdPostPattern5.replace(
      /\{data\}/g,
      data,
    ).replace(/\{url\}/g, url);
    return cmd;
  }
  FetchDownLoadCmd(outfname, url) {
    // console.log(this.cmdPattern);
    const cmd = this.cmdDownLoadFilePattern.replace(/\{fileName\}/g, outfname).replace(/\{url\}/g, url);
    return cmd;
  }
  async exec(cmd) {
    // await后面表达式返回的promise对象，是then的语法糖，await返回then函数的返回值
    // 异常需要try/catch自己捕获或外部catch捕获
    const { stdout, stderr } = await exec(cmd);
    return { stdout, stderr };
  }
  async execDownload(url, outfname) {
    const cmd = this.FetchDownLoadCmd(outfname, url);
    const result = await this.exec(cmd);
    return result;
  }
  async execGet(subData, url) {
    const cmd = this.FetchGetCmd(subData, url);
    const result = await this.exec(cmd);
    return result;
  }
  async execGet2(subData, url) {
    const data = querystring.stringify(subData);
    const cmd = this.FetchGetCmd(data, url);
    const result = await this.exec(cmd);
    return result;
  }
  async execPost(subData, url) {
    const cmd = this.FetchPostCmd(subData, url);
    const result = await this.exec(cmd, {
      maxBuffer: 4096 * 1024,
    });
    return result;
  }
  async execPostWithAK(subData, url, ak) {
    const cmd = this.FetchPostCmdWithAK(subData, url, ak);
    const result = await this.exec(cmd, {
      maxBuffer: 1024 * 1024 * 15,
    });
    const rtn = result.stdout;
    if (rtn) {
      return JSON.parse(rtn);
    }
    return null;
  }
  async execPost2(subData, url) {
    const cmd = this.FetchPostCmd2(subData, url);
    console.log(cmd);
    const result = await this.exec(cmd);
    return result;
  }
  async execPost3(subData, url) {
    const cmd = this.FetchPostCmd3(subData, url);
    console.log(cmd);
    const result = await this.exec(cmd);
    return result;
  }
  async execGet3(url) {
    const cmd = this.FetchGetCmd3(url);
    console.log('execGet3-----01');
    console.log(cmd);
    const result = await this.exec(cmd);
    return result;
  }
  async execPostESignBao(subData, url) {
    const cmd = this.FetchPostCmd4(subData, url);
    console.log(cmd);
    const result = await this.exec(cmd);
    return result;
  }
  async execPostForm(subData, url) {
    const cmd = this.FetchPostCmd5(subData, url);
    console.log(cmd);
    const result = await this.exec(cmd);
    return result;
  }
  async execCustomPostESignBao(cmd) {
    console.log(cmd);
    const result = await this.exec(cmd);
    return result;
  }
  test() {
    console.log('hello');
  }
}
module.exports = RestClient;
// var x=new RestClient();
// x.execGet("","http://www.163.com").then(function(r){
//   console.log(r.stdout);
//   console.log(r.stderr);
// });
