/*global window, rJS, loopEventListener */
/*jslint nomen: true, indent: 2, maxerr: 3*/
(function (window, rJS, loopEventListener) {
  "use strict";

  function displayURLList(gadget, event) {
    var url_list;
    return new RSVP.Queue()
    .push(function () {
       return gadget.crib_sw_allDocs()
    })
    .push(function (data) {
      var promise_list = [],
        url;
      url_list = data.urls;
      for (url in url_list) {
        if (url_list.hasOwnProperty(url)) {
       	  promise_list.push(gadget.getUrlFor({page: 'editor', url: url}));
        }
      }
      return RSVP.all(promise_list);
    })
    .push(function(url_link_list) {
      var contentsElement = gadget.props.element.querySelector('.crib-url-list-content tbody'),
        footer_element = gadget.props.element.querySelector('.crib-url-list-content tfoot'),
        url_number = 0, cached_number = 0,
        url, trElement, tdElement;
      // Clear out the existing items from the list.
      while (contentsElement.firstChild) {
        contentsElement.removeChild(contentsElement.firstChild);
      }
      console.log(url_list);
      // Add each cached URL to the list, one by one.
      for (url in url_list) {
        if (url_list.hasOwnProperty(url)) {
          var element;
          trElement = document.createElement('tr');
          tdElement = document.createElement('td');
          tdElement.innerHTML = (url_list[url].cached ? "<span>&#x2713;</span>" : "")
          trElement.appendChild(tdElement);
          tdElement = document.createElement('td');
          element = document.createElement('a');
          element.setAttribute('href', url_link_list[url_number]);
          element.textContent = url;
          tdElement.appendChild(element);
          trElement.appendChild(tdElement);
          tdElement = document.createElement('td');
          element = document.createElement('a');
          element.textContent = "Go";
          element.setAttribute('href', url);
          element.setAttribute('target', "_blank");
          element.setAttribute("class", "btn btn-primary btn-xs");
          tdElement.appendChild(element);
          trElement.appendChild(tdElement);
          contentsElement.appendChild(trElement);
          cached_number = url_list[url].cached ? cached_number + 1 : cached_number;
          url_number += 1;
        }
      };
      while (footer_element.firstChild) {
        footer_element.removeChild(footer_element.firstChild);
      }
      trElement = document.createElement('tr');
      tdElement = document.createElement('td');
      tdElement.innerHTML = "<span>" + cached_number + " &#x2713;</span>";
      trElement.appendChild(tdElement);
      tdElement = document.createElement('td');
      tdElement.innerHTML =  url_number + " URLs";
      trElement.appendChild(tdElement);      
      tdElement = document.createElement('td');
      trElement.appendChild(tdElement);
      footer_element.appendChild(trElement);
    })
  }

    rJS(window)
    .ready(function (g) {
      g.props = {};
      return g.getElement()
        .push(function (element) {
          g.props.element = element;
          g.props.start_deferred = RSVP.defer();
         });
    })
    .declareAcquiredMethod("crib_sw_allDocs", "crib_sw_allDocs")
    .declareAcquiredMethod("getUrlFor", "getUrlFor")
    .declareMethod('render', function (options) {
      var gadget = this;
      if (options === undefined) 
        options = {}
      gadget.props.options = options;
      return new RSVP.Queue()
        .push(function () {
          return displayURLList(gadget, undefined);
        })
        .push(function () {
          return gadget.props.start_deferred.resolve();
        })  
    })
    .declareService(function () {
      var gadget = this;
      return new RSVP.Queue()
        .push(function () {
          return gadget.props.start_deferred.promise;
        })
        .push(function () {
          return loopEventListener(
            gadget.props.element.querySelector(".crib-url-list-content"),
            'submit',
            false,
            function (event) {displayURLList(gadget, event)}
          )
        });
    })  
}(window, rJS, loopEventListener));