/**
 * Service which provides helper methods for performing some simple UI operations.
 */
angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$location', function($timeout, $rootScope, $location) {
  var CheckStateController = function(items, itemKey, opt_checked) {
    this.items = items;
    this.itemKey = itemKey;
    this.checked = opt_checked || [];
    this.checkedMap = {};
    this.listeners_ = [];

    this.buildMap_();
  };

  CheckStateController.prototype.listen = function(callback) {
    this.listeners_.push(callback);
  };

  CheckStateController.prototype.isChecked = function(item) {
    return !!this.checkedMap[item[this.itemKey]];
  };

  CheckStateController.prototype.toggleItem = function(item) {
    if (this.isChecked(item)) {
      this.uncheckItem(item);
    } else {
      this.checkItem(item);
    }
  };

  CheckStateController.prototype.toggleItems = function() {
    if (this.checked.length) {
      this.checked = [];
      this.checkedMap = {};
    } else {
      this.checked = this.items.slice();
      this.buildMap_();
    }
    this.callListeners_();
  };

  CheckStateController.prototype.setChecked = function(items) {
    this.checked = items.slice();
    this.buildMap_();
  };

  CheckStateController.prototype.buildMap_ = function() {
    var that = this;
    this.checked.forEach(function(item) {
      that.checkedMap[item[that.itemKey]] = true;
    });
  };

  CheckStateController.prototype.checkByFilter = function(filter) {
    this.checked = $.grep(this.items, filter);
    this.buildMap_();
    this.callListeners_();
  };

  CheckStateController.prototype.checkItem = function(item) {
    this.checked.push(item);
    this.checkedMap[item[this.itemKey]] = true;
    this.callListeners_();
  };

  CheckStateController.prototype.uncheckItem = function(item) {
    this.checked = $.grep(this.checked, function(cItem) {
      return cItem != item;
    });
    this.checkedMap[item[this.itemKey]] = false;
    this.callListeners_();
  };

  CheckStateController.prototype.callListeners_ = function() {
    var checked = this.checked;
    this.listeners_.map(function(listener) {
      listener(checked);
    });
  };

  var uiService = {};

  uiService.hidePopover = function(elem) {
    var popover = $(elem).data('bs.popover');
    if (popover) {
      popover.hide();
    }
  };

  uiService.showPopover = function(elem, content, opt_placement) {
    var popover = $(elem).data('bs.popover');
    if (!popover) {
      $(elem).popover({'content': '-', 'placement': opt_placement || 'left'});
    }

    setTimeout(function() {
      var popover = $(elem).data('bs.popover');
      popover.options.content = content;
      popover.show();
    }, 500);
  };

  uiService.showFormError = function(elem, result, opt_placement) {
    var message =  result.data['message'] || result.data['error_description'] || '';
    if (message) {
      uiService.showPopover(elem, message, opt_placement);
    } else {
      uiService.hidePopover(elem);
    }
  };

  uiService.createCheckStateController = function(items, opt_checked) {
    return new CheckStateController(items, opt_checked);
  };

  uiService.showPasswordDialog = function(message, callback, opt_canceledCallback) {
    var success = function() {
      var password = $('#passDialogBox').val();
      $('#passDialogBox').val('');
      callback(password);
    };

    var canceled = function() {
      $('#passDialogBox').val('');
      opt_canceledCallback && opt_canceledCallback();
    };

    var box = bootbox.dialog({
      "message": message +
        '<form style="margin-top: 10px" action="javascript:void(0)">' +
        '<input id="passDialogBox" class="form-control" type="password" placeholder="Current Password">' +
        '</form>',
      "title": 'Please Verify',
      "buttons": {
        "verify": {
          "label": "Verify",
          "className": "btn-success",
          "callback": success
        },
        "close": {
          "label": "Cancel",
          "className": "btn-default",
          "callback": canceled
        }
      }
    });

    box.bind('shown.bs.modal', function(){
      box.find("input").focus();
      box.find("form").submit(function() {
        if (!$('#passDialogBox').val()) { return; }
        box.modal('hide');
        success();
      });
    });
  };

  uiService.clickElement = function(el) {
    // From: http://stackoverflow.com/questions/16802795/click-not-working-in-mocha-phantomjs-on-certain-elements
    var ev = document.createEvent("MouseEvent");
    ev.initMouseEvent(
      "click",
       true /* bubble */, true /* cancelable */,
       window, null,
       0, 0, 0, 0, /* coordinates */
       false, false, false, false, /* modifier keys */
       0 /*left*/, null);
    el.dispatchEvent(ev);
  };

  uiService.initializeTabs = function(scope, element, opt_clickCallback) {
    var locationListener = null;
    var disposed = false;

    var changeTab = function(activeTab) {
      if (disposed) { return; }

      $('a[data-toggle="tab"]').each(function(index) {
        var tabName = this.getAttribute('data-target').substr(1);
        if (tabName != activeTab) {
          return;
        }

        if ($(this).parent().hasClass('active')) {
          return;
        }

        if (this.clientWidth == 0) {
          setTimeout(function() {
            changeTab(activeTab);
          }, 100);
          return;
        }

        var elem = this;
        setTimeout(function() {
          uiService.clickElement(elem);
        }, 0);
      });
    };

    var resetDefaultTab = function() {
      if (disposed) { return; }

      $timeout(function() {
        element.find('a[data-toggle="tab"]').each(function(index) {
          if (index == 0) {
            var elem = this;
            setTimeout(function() {
              uiService.clickElement(elem);
            }, 0);
          }
        });
      });
    };

    var checkTabs = function() {
      if (disposed) { return; }

      // Poll until we find the tabs.
      var tabs = element.find('a[data-toggle="tab"]');
      if (tabs.length == 0) {
        $timeout(checkTabs, 50);
        return;
      }

      // Register listeners.
      registerListeners(tabs);

      // Set the active tab (if any).
      var activeTab = $location.search()['tab'];
      if (activeTab) {
        changeTab(activeTab);
      }
    };

    var registerListeners = function(tabs) {
      // Listen for scope destruction.
      scope.$on('$destroy', function() {
        disposed = true;
        locationListener && locationListener();
      });

      // Listen for route changes and update the tabs accordingly.
      locationListener = $rootScope.$on('$routeUpdate', function(){
        if ($location.search()['tab']) {
          changeTab($location.search()['tab']);
        } else {
          resetDefaultTab();
        }
      });

      // Listen for tab changes.
      tabs.on('shown.bs.tab', function (e) {
        // Invoke the callback, if any.
        opt_clickCallback && opt_clickCallback();

        // Update the search location.
        var tabName = e.target.getAttribute('data-target').substr(1);
        $rootScope.$apply(function() {
          var isDefaultTab = tabs[0] == e.target;
          var newSearch = $.extend($location.search(), {});
          if (isDefaultTab) {
            delete newSearch['tab'];
          } else {
            newSearch['tab'] = tabName;
          }

          $location.search(newSearch);
        });

        e.preventDefault();
      });
    };

    // Start the checkTabs timer.
    checkTabs();
  };

  return uiService;
}]);