'use babel';

import GitLineView from './git-line-view';
import { CompositeDisposable } from 'atom';
import moment from 'moment';
import parser from './output-parser';
import Path from 'path';
import { exec } from 'child_process';

export default {

  gitLineView: null,
  subscriptions: null,
  githubUrl: atom.project.getRepositories()[0].getOriginURL().replace('.git', ''),

  activate(state) {
    this.gitLineView = new GitLineView(state.gitLineViewState);

    // Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
    this.subscriptions = new CompositeDisposable();

    // Register command that toggles this view
    this.subscriptions.add(atom.commands.add('atom-workspace', {
      'git-line:toggle': () => this.toggle()
    }));
  },

  deactivate() {
    this.subscriptions.dispose();
    this.gitLineView.destroy();
  },

  serialize() {
    return {
      gitLineViewState: this.gitLineView.serialize()
    };
  },

  toggle() {

    // if we have a current disposable, dispose it
    if (this.gitLineView.isVisible()) {
      this.gitLineView.hide();
    }
    else {
      const editor = atom.workspace.getActiveTextEditor(),
            editorView = atom.views.getView(editor);

      // use child_process to call out to git and get the git blame info for this line.
      const filePath = editor.buffer.file.path;
      const cmdText = `git blame ${filePath} --line-porcelain -L ${editor.getCursorBufferPosition().row + 1},+1`;
      exec(cmdText, { cwd: Path.dirname(filePath) }, (error, stdout, stderr) => {
        if (error) return console.error(`exec error: ${error}`);

        // TODO: if the currently selected line is not on the screen (ie it's scrolled offscreen),
        // we can only cry and shake our fists for now.  fix it maybe by adding the tooltip
        // to the bottom or top of the gutter to indicate that it is scrolled away
        const lineNumberEl =  editorView.querySelector('body /deep/ .line-number.cursor-line');
        if (!lineNumberEl) return;

        // immediately show the tooltip on the line number
        this.gitLineView.show(
          lineNumberEl,
          this.message(parser.parseOutput(stdout))
        );
      });
    }
  },

  message(output) {
    let msg = `${output.author}, ${moment(output.commitDate).fromNow()}.`;
    return (
      output.author === 'Not Committed Yet'
        ? msg
        : msg + ` View on <a target="_blank" class="git-line-github-link" rel="noopener noreferrer" href="${this.githubUrl}/commit/${output.hash}">Github</a>.`
    );
  }
};

@@ -229,82 +229,56 @@
 ';%0A%0A
-export default %7B%0A%0A  gitLineView: null,%0A  subscriptions: null,%0A  githubUrl:
+%0Aconst parseOrigin = () =%3E %7B%0A  const originUrl =
  ato
@@ -342,16 +342,479 @@
 it', '')
+;%0A%0A  // check the origin's host.  if bitbucket, we have some formatting to do%0A  if (originUrl.indexOf('@bitbucket') !== -1) %7B%0A    // we want everything after the @%0A    return %22https://%22 + originUrl.split('@')%5B1%5D;%0A  %7D%0A%0A  // github is easy: its origins are of the form: https://github.com/joelgardner/git-line.git%0A  // so we only need to remove the .git%0A  return originUrl;%0A%7D%0A%0Aexport default %7B%0A%0A  gitLineView: null,%0A  subscriptions: null,%0A  githubUrl: parseOrigin()
 ,%0A%0A  act
@@ -2762,16 +2762,190 @@
 w()%7D.%60;%0A
+    const isGithub = this.githubUrl.indexOf('github') !== -1;%0A    const commit = isGithub ? 'commit' : 'commits';%0A    const serviceName = isGithub ? 'Github' : 'Bitbucket';%0A%0A
     retu
@@ -3131,22 +3131,25 @@
 hubUrl%7D/
+$%7B
 commit
+%7D
 /$%7Boutpu
@@ -3157,22 +3157,30 @@
 .hash%7D%22%3E
-Github
+$%7BserviceName%7D
 %3C/a%3E.%60%0A 

fix broken link (and link text) to commits in Bitbucket repos