/**
 * @uses jQuery
 * @uses ep
 */
define('de_epages/paypalplus/ui/paypalpluspaymentwall', [
    'jquery',
    'ep',
    '$dict!../dictionary',
    '$ready!',
    'ep/ajax',
    'ep/fn/busy'
], function($, ep, epDict) {
    'use strict';
    var PaymentWallError = epDict.translate('{PaymentWallError}');
    var PatchPaymentError = epDict.translate('{PatchPaymentError}');
    var isMobileGlobal = undefined;
    var isClassicalGlobal = undefined;
    var isErrorShown = false;

    return function(isMobile, isClassical, BasketOrOrderID, PaymentLineItemID) {
      isMobileGlobal = isMobile;
      isClassicalGlobal = isClassical;

      ep('.SubmitButton').click(function( event ) {
          event.stopImmediatePropagation();
          event.preventDefault();
          ep('.SubmitButton').prop('disabled', true);
          ep.ajax({
            data : {
              'ObjectID' : BasketOrOrderID,
              'ChangeAction' : 'PatchPaymentPayPalPlus'
            },
            cache : false,
            type : "POST",
            dataType : "json"
          }).done(function(jsonData) {
            if (jsonData.WasPatchPaymentSuccessful) {
              ep('#BasketForm').submit();
            } else {
              showErrorOnStoreFront(PatchPaymentError);
            }
          }).fail(function(jsonData) {
            showErrorOnStoreFront(PaymentWallError);
          });
     });

      showBusyBox();
      ep.ajax({
        data : {
          'ObjectID' : BasketOrOrderID,
          'ChangeAction' : 'CreatePaymentPayPalPlus'
        },
        cache : false,
        type : "POST",
        dataType : "json"
      }).done(function(jsonData) {
        if (jsonData.WasCreatePaymentSuccessful) {
          buildPaymentWall(PaymentLineItemID);
        } else {
          showErrorOnStoreFront(PaymentWallError);
        }
      }).fail(function(jsonData) {
        showErrorOnStoreFront(PaymentWallError);
      });
    };

   function showBusyBox() {
     if (isMobileGlobal) {
       ep('#CheckOut').prop('disabled', true);
       ep('#CheckOutTop').prop('disabled', true);
     }
     else if(isClassicalGlobal) {
       ep('#ppplus').addClass('ep-busy');
       ep('.ep-uiInput-button').prop('disabled', true);
     }
     else {
       ep('#ppplus').addClass('ep-busy');
       ep('.SubmitButton').prop('disabled', true);
     }
   };

   function OnPaymentWallLoaded() {
     if (isMobileGlobal) {
       ep('#CheckOut').prop('disabled', isErrorShown);
       ep('#CheckOutTop').prop('disabled', isErrorShown);
     }
     else if(isClassicalGlobal) {
       ep('#ppplus').removeClass('ep-busy');
       ep('.ep-uiInput-button').prop('disabled', isErrorShown);
     }
     else {
       ep('#ppplus').removeClass('ep-busy');
       ep('.SubmitButton').prop('disabled', isErrorShown);
     }

     appendPaypalplusCookieToForm();
   };

   function OnDeselect() {
     if (isMobileGlobal) {
       ep('#CheckOut').prop('disabled', true);
       ep('#CheckOutTop').prop('disabled', true);
     }
     else if(isClassicalGlobal) {
       ep('.ep-uiInput-button').prop('disabled', true);
     }
     else {
       ep('.SubmitButton').prop('disabled', true);
     }
   };

   function appendPaypalplusCookieToForm() {

     var cookieName = 'paypalplus_session_v2';

     var input = $('<input>').attr({
        id: 'paypalplus-cookie',
        type: 'hidden',
        name: cookieName,
        value: $.cookie(cookieName)
     });

     $('#paypalplus-cookie').remove();
     ep('#BasketForm').append(input);
   }

   function showErrorOnStoreFront(message) {
     isErrorShown = true;
     OnPaymentWallLoaded();

         ep("#PaymentWallErrorMessage").css({
           "display" : "block"
         }).html(message);
   };

   function buildPaymentWall(PaymentLineItemID) {
     ep.ajax({
       data : {
         'ObjectID' : PaymentLineItemID,
         'ChangeAction' : 'GetPaymentWallDataPayPalPlus'
       },
       cache : false,
       type : "POST",
       dataType : "json"
     }).done(function(jsonData) {
       if(jsonData.wasSuccessful) {
         isErrorShown = false;
         showPaymentWall(jsonData);
         ep('#WasPayPalPlusLoadingSuccessful').val('1');
       } else {
         showErrorOnStoreFront(jsonData.errorMessage);
       }
     }).fail(function(jsonData) {
       showErrorOnStoreFront(jsonData.statusText);
     });
   };

   function showPaymentWall(jsonData) {
     PAYPAL.apps.PPP({
       "showPuiOnSandbox": "true",
       "approvalUrl" : jsonData.approvalUrl,
       "placeholder" : "ppplus",
       "mode" : jsonData.mode,
       "buttonLocation" : jsonData.buttonLocation,
       "language" : jsonData.language,
       "country" : jsonData.country,
       "enableContinue" : OnPaymentWallLoaded,
       "disableContinue" : OnDeselect,
     });
   };
});