/*global define*/
/**
 * Sends a request for capture ipayment money
 *
 * @class de_epages.ipayment.ui.ipaymentcapture
 *
 * @uses jQuery
 * @uses ep
 * @uses ep.ajax
 * @uses ep.fn.busy
 * @uses ep.ui.dialog
 *
 *
 */
define('de_epages/ipayment/ui/ipaymentcapture', [
    'jquery',
    'ep',
    '$dict!ep/dict',
    '$ready!',
    'ep/ajax',
    'ep/fn/busy',
    'ep/ui/dialog',
], function($, ep, epDict) {

    'use strict';
    var tCapturePaymentQuestion = epDict.translate('{CapturePaymentQuestion}');
    var tYes = epDict.translate('{yes}');
    var tCancel = epDict.translate('{Cancel}');
    var tAccepted = epDict.translate('{IpaymentStatusAccepted}');
    var tFailed = epDict.translate('{IpaymentStatusFailed}');
    var tPaid = epDict.translate('{IpaymentStatusPaid}');
    var tCancelled = epDict.translate('{IpaymentStatusCancelled}');

    /**
     * @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>' + tCapturePaymentQuestion + '</div>')
                    .uiDialog({
                        modal: true,
                        width: 350,
                        height: 150,
                        buttons: [{
                            id: 'YesId',
                            text: tYes,
                            click: function() {
                                handleipaymentcapture(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 == "Ipayment" && UIControlParameters.SelectedTransactionType == "preauth") {
            UIControlParameters.DisplayCapture.show();
            if (UIControlParameters.TransStatus == "STATUS_ACCEPTED" || UIControlParameters.TransStatus == "STATUS_FAILED") {
                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 handleipaymentcapture(context, objectid, currentWindow) { // handle the ajax request when cancelling or activating the invoice

        ep.ajax({
                data: {
                    'ObjectID': objectid,
                    'ChangeAction': 'JSONCaptureIpaymentPayment'
                },
                cache: false,
                type: "POST",
                dataType: "json"
            })
            .done(function(jsonData) {
                var status = jsonData.status;
                var transstatus = jsonData.transstatus;
                context.busy('hide');
                if (status == 'ok' && transstatus == 'STATUS_PAID') {
                    context.empty();
                    context.append('<div id="Response" class="DialogMessage MessageWarning">OK</div>');
                    context.uiDialog('close');
                    currentWindow.location.reload();
                } else if (status == 'error') {
                    context.empty();
                    context.append('<div id="Response" class="DialogMessage MessageWarning">' + jsonData.message + '</div>');
                    context.uiDialog('close');
                    $("#IpaymentActionsNotificationDiv").empty();
                    $("#IpaymentActionsNotificationDiv").append(TextWithTransStatus(transstatus)).removeClass("MessageInfo").addClass("MessageWarning");
                    $("#IpaymentActionsNotificationDiv").show();
                } else {
                    context.empty();
                    context.append('<div id="Response" class="DialogMessage MessageWarning">Transaction Failed</div>');
                    context.uiDialog('close');
                    $("#IpaymentActionsNotificationDiv").empty();
                    $("#IpaymentActionsNotificationDiv").append(TextWithTransStatus(transstatus)).removeClass("MessageInfo").addClass("MessageWarning");
                    $("#IpaymentActionsNotificationDiv").show();
                }
            })
            .fail(function(jsonData) {
                context.busy('hide');
                context.empty();
                context.buttons.empty();
                context.append('<div class="DialogMessage MessageWarning">ajax error</div>');
            });
    }

    function TextWithTransStatus(transstatus){
        if (transstatus == 'STATUS_PAID'){
            return tPaid;
        }

        if (transstatus == 'STATUS_FAILED'){
            return tFailed;
        }

        if (transstatus == 'STATUS_ACCEPTED'){
            return tAccepted;
        }

        if (transstatus == 'STATUS_CANCELLED'){
            return tCancelled;
        }

    }
});