'use strict';

/* global jsondiffpatch */

angular.module('arethusa.review').service('review', [
  'configurator',
  'state',
  'morph',
  '$rootScope',
  'navigator',
  'plugins',
  function (configurator, state, morph, $rootScope, navigator, plugins) {
    var self = this;
    var retriever;
    var doc;
    self.goldTokens = {};

    self.defaultConf = {
      "name" : "review",
      "link" : true
    };

    function configure() {
      configurator.getConfAndDelegate('review', self);
      configurator.getStickyConf('review', self, ['link', 'autoDiff']);
      self.comparators = [
        'morphology.lemma',
        'morphology.attributes',
        'head.id',
        'relation.label'
      ];
      var retrievers = configurator.getRetrievers(self.conf.retrievers);
      retriever = retrievers.TreebankRetriever;
    }

    configure();

    function addStyleInfo(tokens) {
      angular.forEach(tokens, function (token, id) {
        var form = token.morphology;
        if (form) {
          morph.postagToAttributes(form);
          token.style = morph.styleOf(form);
        }
      });
    }

    function broadcast() {
      $rootScope.$broadcast('diffLoaded');
    }

    self.goToCurrentChunk = function() {
      self.pos = navigator.status.currentPos;
      self.goldTokens = doc[self.pos].tokens;
      addStyleInfo(self.goldTokens);
    };

    function loadDocument() {
      retriever.getData(function (res) {
        doc = res;
        postInit(true);
      });
    }


    function extract(obj) {
      return arethusaUtil.inject({}, obj, function(memo, id, token) {
        memo[id] = arethusaUtil.copySelection(token, self.comparators);
      });
    }

    this.compare = function () {
      var diff = jsondiffpatch.diff(
        extract(state.tokens),
        extract(self.goldTokens)
      );

      if (diff) {
        angular.forEach(diff, function (diff, id) {
          state.setState(id, 'diff', diff);
        });
        broadcast();
      }
    };

    loadDocument();

    function postInit(initialLoad) {
      if (self.link || initialLoad) self.goToCurrentChunk();

      plugins.doAfter('depTree', function() {
        if (self.autoDiff) self.compare();
      });
    }

    this.init = function () {
      configure();

      if (!doc) return; // in case this gets called before we're ready
      postInit();
    };
  }
]);

@@ -818,24 +818,55 @@
 kRetriever;%0A
+      self.diffActive = false;%0A
     %7D%0A%0A    c
@@ -1161,24 +1161,54 @@
 oadcast() %7B%0A
+      self.diffActive = true;%0A
       $rootS

Make known when a diff is active