import {injectable, inject} from "inversify";
import kernel from '../../inversify.config';
import {Command, CommandUtil} from 'firmament-yargs';
import {VitaTasks} from "../../interfaces/vita-tasks";
const deepExtend = require('deep-extend');

@injectable()
export class VitaCommandImpl implements Command {
  aliases: string[] = [];
  command: string = '';
  commandDesc: string = '';
  //noinspection JSUnusedGlobalSymbols
  //noinspection JSUnusedLocalSymbols
  handler: (argv: any) => void = (argv: any) => {
  };
  options: any = {};
  subCommands: Command[] = [];

  constructor(@inject('CommandUtil') private commandUtil: CommandUtil,
              @inject('VitaTasks') private vitaTasks: VitaTasks) {
    this.buildCommandTree();
  }

  private buildCommandTree() {
    this.aliases = ['vita'];
    this.command = '<subCommand>';
    this.commandDesc = 'Run VITA tasks';
    this.pushProcessCommand();
    this.pushJsonOutCommand();
  }

  static processingOptions = {
    output: {
      alias: 'o',
      type: 'string',
      desc: `Name of merged PCAP file`
    },
    input: {
      alias: 'i',
      type: 'array',
      desc: `Space delimited list of PCAP archives. Archive files expected to have '.enc' extension`
    },
    password: {
      alias: 'p',
      type: 'string',
      desc: `OpenSSL passphrase for encrypted PCAP archive`
    }
  };

  private pushJsonOutCommand() {
    let me = this;
    let jsonOutCommand = kernel.get<Command>('CommandImpl');
    jsonOutCommand.aliases = ['json-out'];
    jsonOutCommand.commandDesc = 'Create JSON execution graph for later processing';
    jsonOutCommand.options = {
      'write': {
        alias: 'w',
        type: 'string',
        desc: 'Output path for JSON file. Writes to stdout if not specified'
      }
    };

    deepExtend(jsonOutCommand.options, VitaCommandImpl.processingOptions);
    jsonOutCommand.handler = this.vitaTasks.jsonOut.bind(this.vitaTasks);
    me.subCommands.push(jsonOutCommand);
  }

  private pushProcessCommand() {
    let me = this;
    let processCommand = kernel.get<Command>('CommandImpl');
    processCommand.aliases = ['process'];
    processCommand.commandDesc = 'Process encrypted PCAP archives';
    processCommand.options = {
      'json-in': {
        type: 'string',
        desc: 'Name of execution graph to process'
      }
    };

    deepExtend(processCommand.options, VitaCommandImpl.processingOptions);

    processCommand.handler = this.vitaTasks.process.bind(this.vitaTasks);
    me.subCommands.push(processCommand);
  }
}

