'use strict';

angular.module('fileBrowserApp').controller('BrowserController', [
  '$scope',
  'FetchFileFactory',
  function($scope, FetchFileFactory) {
    function isTreebank(node) {
      return node.id.match(/examples\/data\/treebanks\/.*\.xml$/);
    }

    function generateTreebankLink(node) {
      var base = '../app/#/staging?doc=';
      var docId = node.text.match(/(.*)\.xml/)[1];

      $scope.arethusaHref = base + docId;
    }

    function unsetTreebankLink() {
      $scope.arethusaHref = undefined;
    }

    var placeholder = 'Please select a file to view its contents';

    function toggleHighlighting(bool) {
      $scope.syntaxHighlighting = bool;
    }

    function checkHighlighting(file) {
      if (file.length < 50000) {
        toggleHighlighting(true);
      } else {
        toggleHighlighting(false);
      }
    }

    function checkForTreebankLink(node) {
      if (isTreebank(node)) {
        generateTreebankLink(node);
      } else {
        unsetTreebankLink();
      }
    }

    $scope.fileViewer = placeholder;

    $scope.nodeSelected = function(e, data) {
      var node = data.node;
      var _l = node.li_attr;

      checkForTreebankLink(node);

      if (_l.isLeaf) {
        FetchFileFactory.fetchFile(_l.base).then(function(data) {
          var _d = data.data;
          if (typeof _d == 'object') {
            //http://stackoverflow.com/a/7220510/1015046//
            _d = JSON.stringify(_d, undefined, 2);
          }
          $scope.fileViewer = _d;
          checkHighlighting(_d);
        });
      } else {
        //http://jimhoskins.com/2012/12/17/angularjs-and-apply.html//
        $scope.$apply(function() {
          toggleHighlighting(false);
          $scope.fileViewer = placeholder;
        });
      }
    };
  }
]);

@@ -365,20 +365,18 @@
 ode.
-text
+id
 .match(/
 (.*)
@@ -371,16 +371,27 @@
 .match(/
+treebanks%5C/
 (.*)%5C.xm

Allow nested paths for treebanks