/*global define*/
/**
 * Sends a request for cancel PayBox authorization
 *
 * @class de_epages.PayBox.ui.PayBoxcancel
 *
 * @uses jQuery
 * @uses ep
 * @uses ep.ajax
 * @uses ep.fn.busy
 * @uses ep.ui.dialog
 *
 *
 */
define('de_epages/paybox/ui/payboxcancel', [
    'jquery',
    'ep',
    '$dict!../dictionary',

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

    'use strict';
    var tPayboxCancelPaymentQuestion = dict.translate('{PayboxCancelPaymentQuestion}');
    var tPayboxCancelPaymentConfirmation = dict.translate('{PayboxCancelPaymentConfirmation}');
    var tClose = dict.translate('{Close}');
    var tYes = dict.translate('{yes}');
    var tCancel = dict.translate('{Cancel}');

    /**
     * @param {String}  UIControlParameters                 JS Object with UI control parameters.
     */
    return function(UIControlParameters){
        var currentWindow = UIControlParameters.CurrentWindow || window;
        var visible = IsVisible(UIControlParameters);

        if (visible) {
            UIControlParameters.CancelLink.on('click', function(event) {
                event.preventDefault();
                var dialog = ep('<div/>')
                    .append('<div>' + tPayboxCancelPaymentQuestion + '</div>')
                    .uiDialog({
                        modal: true,
                        width: 400,
                        height: 200,
                        buttons: [{
                            id: 'YesId',
                            text: tYes,
                            click: function() {
                                handlePayBoxcancel(ep(this), UIControlParameters.Objectid, currentWindow);
                                ep(this).busy('show');
                            }
                        }, {
                            id: 'CancelId',
                            text: tCancel,
                            click: function() {
                                ep(this).uiDialog('close');
                            }
                        }]
                    });
                dialog.uiDialog('open');
            });
        }
    };

    function IsVisible(UIControlParameters) {
        UIControlParameters.DisplayCancel.hide();

        if (UIControlParameters.PaymentTypeAlias == "Paybox") {
            UIControlParameters.DisplayCancel.show();
            if (UIControlParameters.TransStatus == "STATUS_PAID") {
                UIControlParameters.CancelLink.removeClass("Disabled").addClass("ep-cancelamount ep-reloadlocation");
                return true;
            } else {
                UIControlParameters.CancelLink.removeClass("ep-cancelamount ep-reloadlocation").addClass("Disabled");
                return false;
            }
        }
        return false;
    }

    function handlePayBoxcancel(context, objectid, currentWindow) {
        ep.ajax({
                data: {
                    'ObjectID': objectid,
                    'ChangeAction': 'JSONCancelPayBoxPayment'
                },
                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>' + tPayboxCancelPaymentConfirmation + '</div>')
            .uiDialog({
                modal: true,
                width: 400,
                height: 200,
                buttons: [{
                    id: 'CloseId',
                    text: tClose,
                    click: function() {
                        ep(this).busy('show');
                        currentWindow.location.reload();
                        context.uiDialog('close');
                    }
                }]
            });
        dialog.uiDialog('open');
    }

});