'use babel';

import 'atom';
import child_process from 'child_process';
import AtomPhpunitView from './atom-phpunit-view.js';

export default {

  config: {
    successAsNotifications: {
      order: 1,
      description: 'Show successful output as a notification instead of using the output panel',
      type: 'boolean',
      default: true
    },
    useVendor: {
      order: 2,
      description: 'Uses the project\'s phpunit binary (./vendor/bin/phpunit)',
      type: 'boolean',
      default: true
    },
    phpunitPath: {
      order: 3,
      title: 'PHPUnit Binary Path',
      description: 'Used only if \'Use Vendor\' is not ticked.',
      type: 'string',
      default: '/usr/local/bin/phpunit'
    }
  },

  exec: null,

  activate(state) {
    this.exec = child_process.exec;
    this.errorView = new AtomPhpunitView(state.atomPhpunitViewState);
    this.outputPanel = atom.workspace.addBottomPanel({
      item: this.errorView.getElement(),
      visible: false
    });

    atom.commands.add('atom-workspace', {
      'atom-phpunit:run-test': () => this.runTest(),
      'atom-phpunit:run-class': () => this.runClass(),
      'atom-phpunit:run-suite': () => this.runSuite(),
      'atom-phpunit:toggle-output': () => this.toggleOutput()
    });
  },

  serialize() {
    return {
      atomPhpunitViewState: this.errorView.serialize()
    };
  },

  deactivate() {
    this.outputPanel.destroy();
    this.errorView.destroy();
  },

  runTest() {
    if (this.outputPanel.isVisible()) {
      this.errorView.update('No Output');
      this.outputPanel.hide();
    }

    filepath = this.getFilepath();
    if (!filepath) {
      atom.notifications.addError('Failed to get filename! Make sure you are in the test file you want run.');
      return;
    }

    func = this.getFunctionName();
    if (!func) {
      atom.notifications.addError('Function name not found! Make sure your cursor is inside the test you want to run.');
      return;
    }

    this.execute(filepath, func);
  },

  runClass() {
    if (this.outputPanel.isVisible()) {
      this.errorView.update('No Output');
      this.outputPanel.hide();
    }
    filename = this.getFilepath();
    if (!filename) {
      atom.notifications.addError('Failed to get filename! Make sure you are in the test file you want run.');
      return;
    }

    this.execute(filename);
  },

  runSuite() {
    if (this.outputPanel.isVisible()) {
      this.errorView.update('No Output');
      this.outputPanel.hide();
    }
    this.execute();
  },

  toggleOutput() {
    this.outputPanel.isVisible()
      ? this.outputPanel.hide()
      : this.outputPanel.show();
  },

  execute(filepath, functionName) {
    if (atom.config.get('atom-phpunit.useVendor'))
      cmd = './vendor/bin/phpunit';
    else
      cmd = atom.config.get('atom-phpunit.phpunitPath');

    if (typeof functionName !== 'undefined')
      cmd += ` --filter=${functionName}`;

    if (typeof filepath !== 'undefined')
      cmd += ` ${filepath}`;

    console.log(`atom-phpunit: ${cmd}`);

    this.exec(cmd, {cwd: atom.project.getPaths()[0]}, (error, stdout, stderr) => {
      if (error && stderr) {
        this.errorView.update(stderr, false);
        this.outputPanel.show();
      } else if (error) {
        this.errorView.update(stdout, false);
        this.outputPanel.show();
      } else {
        this.errorView.update(stdout, true);
        if (atom.config.get('atom-phpunit.successAsNotifications')) {
          atom.notifications.addSuccess('Test Passed!', {description: cmd, detail: stdout});
        } else {
          this.outputPanel.show();
        }
      }
    });
  },

  getFunctionName() {
    let editor = atom.workspace.getActiveTextEditor();
    if (!editor)
      return false;

    let pos = editor.getCursorBufferPosition();
    let buffer = editor.getBuffer();

    for (var row = pos.row; row--; row > 0) {
      let line = buffer.lineForRow(row);
      if (line.includes('function ')) {
        let parts = line.match(/function\s+([^\s(]+)\s*\(/);
        if (parts && parts.length > 1)
          return parts[1];
      }
    }

    return false
  },

  getFilepath() {
    let editor = atom.workspace.getActivePaneItem();
    if (!editor)
      return false;

    let buffer = editor.buffer;
    if (!buffer)
      return false;

    let file = buffer.file;

    if (!file.path) {
      return false;
    }

    return file.path.replace(`${atom.project.getPaths()[0]}/`, '');
  }

};

@@ -2144,20 +2144,20 @@
     file
-name
+path
  = this.
@@ -2184,20 +2184,20 @@
 f (!file
-name
+path
 ) %7B%0A    
@@ -2345,20 +2345,20 @@
 ute(file
-name
+path
 );%0A  %7D,%0A
@@ -2488,24 +2488,227 @@
 de();%0A    %7D%0A
+%0A    filepath = this.getFilepath();%0A    if (!filepath) %7B%0A      atom.notifications.addError('Failed to find phpunit! Make sure you are in the test file from the suit you want run.');%0A      return;%0A    %7D%0A%0A
     this.exe
@@ -3264,34 +3264,35 @@
 wd: 
-atom.project.get
+this.getProjectFolder
 Path
-s
 ()
-%5B0%5D
 %7D, (
@@ -4613,60 +4613,112 @@
 path
-.replace(%60$%7Batom.project.getPaths()%5B0%5D%7D/%60, '')
+;%0A  %7D,%0A%0A  getProjectFolderPath() %7B%0A    return atom.project.relativizePath(this.getFilepath())%5B0%5D
 ;%0A  %7D%0A
+  
 %0A%7D;%0A

Fix path used as cwd for running test when multiple project folders are opened