/*global define*/
/**
 * Sends a request for cancel SIA Transaction
 *
 * @class de_epages.sia.ui.siacancel
 *
 * @uses jQuery
 * @uses ep
 * @uses ep.ajax
 * @uses ep.fn.busy
 * @uses ep.ui.dialog
 *
 *
 */
define('de_epages/sia/ui/siacancel', [
    'jquery',
    'ep',
    '$dict!../dictionary',

    '$ready!',
    'ep/ajax',
    'ep/fn/busy',
    'ep/ui/dialog',
], function($, ep, dict) {
    'use strict';

    var tSIACancelPaymentQuestion       = dict.translate('{SIACancelPaymentQuestion}');
    var tSIACancelPaymentConfirmation   = dict.translate('{SIACancelPaymentConfirmation}');
    var tSIACancelPayment               = dict.translate('{SIACancelPayment}');
    var grandTotal                      = dict.translate('{GrandTotal}');
    var tClose  = dict.translate('{Close}');
    var tYes    = dict.translate('{yes}');
    var tNo = dict.translate('{no}');

    return function(UIControlParameters){
        var currentWindow   = UIControlParameters.CurrentWindow || window;

        if (UIControlParameters.TransStatus == "STATUS_PAID"){
            UIControlParameters.CancelLink.on('click', function(event) {
                event.preventDefault();
                var dialog = ep('<div/>')
                    .append('<div class="DialogMessage MessageConfirmAction TopSmallMargin Bold">' + tSIACancelPaymentQuestion + '</div><div class="Bold">' + grandTotal + ': ' + UIControlParameters.TotalAmount +'</div>')
                    .uiDialog({
                        modal: true,
                        title: tSIACancelPayment,
                        width: 400,
                        height: 200,
                        buttons: [{
                            id: 'YesId',
                            text: tYes,
                            click: function() {
                                handleSIAcancel(ep(this), UIControlParameters.Objectid, currentWindow);
                                ep(this).busy('show');
                            }
                        }, {
                            id: 'CancelId',
                            text: tNo,
                            click: function() {
                                ep(this).uiDialog('close');
                            }
                        }]
                    });
                dialog.uiDialog('open');
            });
        }

        function handleSIAcancel(context, objectid, currentWindow) {
            ep.ajax({
                    data: {
                        'ObjectID': objectid,
                        'ChangeAction': 'JSONCancelSIA'
                    },
                    cache: false,
                    type: "POST",
                    dataType: "json"
                })
                .done(function(jsonData) {
                    var success = jsonData.success;
                    var errorMessage = jsonData.error_message;
                    context.busy('hide');
                    if (success == 1) {
                        context.uiDialog('close');
                        showConfirmation(context, currentWindow);
                    } else if (success == 0) {
                        context.append('<div id="Response">' + errorMessage + '</div>');
                    } else {
                        context.append('<div id="Response">Transaction Failed</div>');
                    }
                })
                .fail(function(jsonData) {
                    context.busy('hide');
                    context.empty();
                    context.buttons.empty();
                    context.append('<div class="DialogMessage MessageWarning">ajax error</div>');
                });
        }

        function showConfirmation(context, currentWindow) {
            var dialog = ep('<div/>')
                .append('<div class="TopPaddingWide Bold AlignCenter"><i class="Sprite ico_l_test_ok MarginRight"></i>' + tSIACancelPaymentConfirmation + '</div>')
                .uiDialog({
                    modal: true,
                    title: tSIACancelPayment,
                    width: 400,
                    height: 200,
                    buttons: [{
                        id: 'CloseId',
                        text: tClose,
                        click: function() {
                            ep(this).busy('show');
                            currentWindow.location.reload();
                            context.uiDialog('close');
                        }
                    }]
                });
            dialog.uiDialog('open');
        }
    }
});