/*
 * Copyright (c) 2014 ThoughtWorks, Inc.
 *
 * Pixelated is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pixelated is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
 */
'use strict';

define([
    'flight/lib/component',
    'flight/lib/utils',
    'page/events',
    'helpers/view_helper'
  ],
  function (defineComponent, utils, events, viewHelper) {

    return defineComponent(sendButton);

    function sendButton() {
      var RECIPIENTS_BOXES_COUNT = 3;

      this.enableButton = function () {
        this.$node.prop('disabled', false);
      };

      this.disableButton = function () {
        this.$node.prop('disabled', true);
      };

      this.atLeastOneFieldHasRecipients = function () {
        return _.any(_.values(this.attr.recipients), function (e) { return !_.isEmpty(e); });
      };

      this.atLeastOneInputHasMail = function () {
        return _.any(_.values(this.attr.inputHasMail), function (e) { return e === true; });
      };

      this.updateButton = function () {
        if (this.atLeastOneInputHasMail() || this.atLeastOneFieldHasRecipients()) {
          this.enableButton();
        } else {
          this.disableButton();
        }
      };

      this.inputHasNoMail = function (ev, data) {
        this.attr.inputHasMail[data.name] = false;
        this.updateButton();
      };

      this.inputHasMail = function (ev, data) {
        this.attr.inputHasMail[data.name] = true;
        this.updateButton();
      };

      this.updateRecipientsForField = function (ev, data) {
        this.attr.recipients[data.recipientsName] = data.newRecipients;
        this.attr.inputHasMail[data.recipientsName] = false;

        this.updateButton();
      };

      this.updateRecipientsAndSendMail = function () {

        this.on(document, events.ui.mail.recipientsUpdated, utils.countThen(RECIPIENTS_BOXES_COUNT, function () {
          this.trigger(document, events.ui.mail.send);
          this.off(document, events.ui.mail.recipientsUpdated);
        }.bind(this)));

        this.disableButton();
        this.$node.text(viewHelper.i18n('sending-mail'));

        this.trigger(document, events.ui.recipients.doCompleteInput);
      };

      this.forceEnableButton = function () {
        this.enableButton();
        this.$node.html(viewHelper.i18n('send-button'));
      };

      this.after('initialize', function () {
        this.attr.recipients = {};
        this.attr.inputHasMail = {};
        this.$node.html(viewHelper.i18n('send-button'));

        this.on(document, events.ui.recipients.inputHasMail, this.inputHasMail);
        this.on(document, events.ui.recipients.inputHasNoMail, this.inputHasNoMail);
        this.on(document, events.ui.recipients.updated, this.updateRecipientsForField);

        this.on(this.$node, 'click', this.updateRecipientsAndSendMail);

        this.on(document, events.dispatchers.rightPane.clear, this.teardown);
        this.on(document, events.ui.sendbutton.enable, this.enableButton);
        this.on(document, events.mail.send_failed, this.forceEnableButton);

        this.disableButton();
      });
    }

  }
);

@@ -1527,32 +1527,87 @@
 = function () %7B%0A
+        if (this.attr.sendingInProgress === false) %7B%0A  
         if (this
@@ -1676,32 +1676,34 @@
 ()) %7B%0A          
+  
 this.enableButto
@@ -1715,16 +1715,18 @@
         
+  
 %7D else %7B
@@ -1728,32 +1728,34 @@
 lse %7B%0A          
+ 
+ 
 this.disableButt
@@ -1752,32 +1752,44 @@
 isableButton();%0A
+          %7D%0A
         %7D%0A      
@@ -2701,24 +2701,69 @@
 g-mail'));%0A%0A
+        this.attr.sendingInProgress = true;%0A%0A
         this
@@ -2837,35 +2837,29 @@
 %0A      this.
-forceEnable
+reset
 Button = fun
@@ -2878,38 +2878,54 @@
        this.
-enableButton()
+attr.sendingInProgress = false
 ;%0A        th
@@ -2971,16 +2971,45 @@
 ton'));%0A
+        this.enableButton();%0A
       %7D;
@@ -3144,51 +3144,21 @@
 his.
-$node.html(viewHelper.i18n('send-b
+resetB
 utton
-'));
+()
 %0A%0A  
@@ -3617,30 +3617,29 @@
 nable, this.
-enable
+reset
 Button);%0A   
@@ -3695,19 +3695,13 @@
 his.
-forceEnable
+reset
 Butt

Send button not auto re-enabled because of synchronous script execution.