/*global define*/
/**
 * Sends a request for capture paybox money
 *
 * @class de_epages.paybox.ui.payboxcapture
 *
 * @uses jQuery
 * @uses ep
 * @uses ep.ajax
 * @uses ep.fn.busy
 * @uses ep.ui.dialog
 *
 *
 */
define('de_epages/paybox/ui/payboxcapture', [
    'jquery',
    'ep',
    '$dict!../dictionary',

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

    'use strict';
    var tPayboxCapturePaymentQuestion = dict.translate('{PayboxCapturePaymentQuestion}');
    var tPayboxCapturePaymentConfirmation = dict.translate('{PayboxCapturePaymentConfirmation}');
    var tClose = dict.translate('{Close}');
    var tYes = dict.translate('{yes}');
    var tCancel = dict.translate('{Cancel}');
    var tUnexpectedError = dict.translate('{UnexpectedError}');

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

        if (visible) {
            UIControlParameters.CaptureLink.on('click', function(event) {
                event.preventDefault();
                var dialog = ep('<div/>')
                    .append('<div>' + tPayboxCapturePaymentQuestion + '</div>')
                    .uiDialog({
                        modal: true,
                        width: 400,
                        height: 200,
                        buttons: [{
                            id: 'YesId',
                            text: tYes,
                            click: function() {
                                handlepayboxcapture(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.DisplayCapture.hide();

        if (UIControlParameters.PaymentTypeAlias == "Paybox") {
            UIControlParameters.DisplayCapture.show();
            if (UIControlParameters.TransStatus == "STATUS_ACCEPTED") {
                UIControlParameters.CaptureLink.removeClass("Disabled").addClass("ep-captureamount ep-reloadlocation");
                return true;
            } else {
                UIControlParameters.CaptureLink.removeClass("ep-captureamount ep-reloadlocation").addClass("Disabled");
                return false;
            }
        }
        return false;
    }

    function handlepayboxcapture(context, objectid, currentWindow) { // handle the ajax request when cancelling or activating the invoice

        ep.ajax({
                data: {
                    'ObjectID': objectid,
                    'ChangeAction': 'JSONCapturePayBoxPayment'
                },
                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);
                    //currentWindow.location.reload();
                } 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>'+ tUnexpectedError +'</div>');
            });
    }

    function showConfirmation(context, currentWindow) {
        var dialog = ep('<div/>')
            .append('<div>' + tPayboxCapturePaymentConfirmation + '</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');
    }

});