import { reflection } from 'first-di';
import "reflect-metadata";
import { promises as fs } from 'fs';
import { async } from 'node-stream-zip';
import * as cp from 'child_process';

import path from 'path';

async function exec(cmd: string, opts: (cp.ExecOptions & { trim?: boolean }) = {}): Promise<string> {
  return new Promise((c, e) => {
    cp.exec(cmd, { env: process.env, ...opts }, (err, stdout) => err ? e(err) : c(opts.trim ? stdout.trim() : stdout));
  });
}

@reflection
export class ComputeRepository {
  public computedAdjaxedMatrix = async (outPath: string, cadEntity: string, entityId: string) => {
    const envPath = '/home/idontsudo/t/framework/asp-review-app/server/computed/geometric_feasibility_predicate/env.json'
    const computedScript = '/home/idontsudo/t/framework/asp-review-app/server/computed/geometric_feasibility_predicate/main.py'
    const computedComand = 'freecadcmd'

    const env = JSON.parse((await fs.readFile(envPath)).toString())
    env['cadFilePath'] = cadEntity
    env['outPath'] = outPath
    await fs.writeFile(envPath, JSON.stringify(env))
    // console.log(this._computedPath(computedScript))
    exec(computedComand + ' ' + computedScript, { cwd: this._computedPath(computedScript) }).then((data) => {
      console.log(data)
    })
    this.cadGeneration(cadEntity, entityId, outPath)
    // if (stderr) {
    //   console.log(stderr)
    // }
    // console.log(stdout)
  };
  public computedWriteStability = async (assemblyFolder: string, buildNumber: string, id: string) => {
    const computedScript = '/home/idontsudo/t/framework/cad_stability_input/main.py'
    const computedComand = 'freecad'
    const envPath = '/home/idontsudo/t/framework/cad_stability_input/env.json'
    const env = JSON.parse((await fs.readFile(envPath)).toString())
    env.assemblyFolder = assemblyFolder
    env['projectId'] = id
    env['buildNumber'] = buildNumber
    env['assemblyFolder'] = assemblyFolder
    env['resultURL'] = 'http://localhost:3002/assembly/stabilty/create/?id=' + id + '&' + 'buildNumber=' + buildNumber

    await fs.writeFile(envPath, JSON.stringify(env))
    await exec(computedComand + ' ' + computedScript, { cwd: this._computedPath(computedScript) })
  }

  private _computedPath(f: string) {

    const file = path.basename(f);
    const absolutPath = path.resolve(f)
    return absolutPath.replace(file, '')
  }

  public cadGeneration = async (cadEntity, entity: string, outPath: string,) => {
    const computedScript = '/home/idontsudo/t/framework/cad_generation/main.py'
    const computedComand = 'freecad'
    const envPath = '/home/idontsudo/t/framework/cad_generation/env.json'

    const env = JSON.parse((await fs.readFile(envPath)).toString())
    env.doc = cadEntity
    env.projectId = entity
    env.resultURL = "http://localhost:3002/assembly/save/out"

    await fs.writeFile(envPath, JSON.stringify(env))
    // /stabilty/create

    exec(computedComand + ' ' + computedScript, { cwd: this._computedPath(computedScript) }).then((data) => {
      console.log(data)
    })
  }

}
