const Blamer = require('./util/blamer');
const BlameListView = require('./views/blame-list-view');

// reference to the Blamer instance created in initializeContext if this
// project is backed by a git repository.
var projectBlamer = null;

function activate() {
  initializeContext();

  // git-blame:blame
  atom.workspaceView.command('git-blame:blame', function() {
    return blame();
  });

  return;
}

function initializeContext() {
  var editor = atom.workspace.activePaneItem;
  var projectRepo = atom.project.getRepo();

  // Ensure this project is backed by a git repository
  if (!projectRepo) {
    // TODO visually alert user
    return console.error('Cant initialize blame! there is no git repo for this project');
  }

  projectBlamer = new Blamer(projectRepo);
}

function blame() {
  // Nothing to do if projectBlamer isnt defined. Means this project is not
  // backed by git.
  if (!projectBlamer) {
    return;
  }

  var editor = atom.workspace.activePaneItem;
  var filePath = editor.getPath();

  toggleBlame(filePath);
}

// Potentially move these into a ViewController...

function getFocusedEditorContainer () {
  var activePane = atom.workspaceView.getActivePaneView();
  return activePane.find('.editor.is-focused');
}

function toggleBlame(filePath) {
  var $focusedEditor = getFocusedEditorContainer();
  if ($focusedEditor.hasClass('blaming')) {
    // kill the blame container
    $focusedEditor.removeClass('blaming');
    $focusedEditor.find('.git-blame').remove();
  } else {
    // blame the given file + show view on success
    projectBlamer.blame(filePath, function(err, blame) {
      if (err) {
        console.error('[ERROR]' + err);
      } else {
        insertBlameView(blame, $focusedEditor);
      }
    });
  }
}

function insertBlameView(blameData, focusedEditor) {
  var viewData = {
    annotations: blameData
  };
  var view = new BlameListView(viewData);
  focusedEditor.prepend(view);
  focusedEditor.addClass('blaming');
}

module.exports = {
  blame: blame,
  activate: activate
};

@@ -1041,16 +1041,86 @@
 th);%0A%7D%0A%0A
+function handleError(error) %7B%0A  console.error('%5BERROR%5D ' + error);%0A%7D%0A%0A
 // Poten
@@ -1473,16 +1473,56 @@
 ontainer
+ if we're already blaming this container
 %0A    $fo
@@ -1753,34 +1753,20 @@
     
-console.error('%5BERROR%5D' + 
+handleError(
 err)
@@ -2003,31 +2003,97 @@
 a);%0A
-  focusedEditor.prepend
+%0A  // insert the BlameListView after the gutter div%0A  focusedEditor.find('.gutter').after
 (vie

insert blame view between gutter and content