/*global define*/
/**
 * @uses jQuery
 * @uses ep
 * @uses ep.ui.dialog
 */
define('de_epages/packlink/ui/packlinkuninstallapp', [
    'jquery',
    'ep',
    '$dict!../dictionary',
    'ep/ajax',
    '$ready!',
    'ep/ui/dialog',
    'ep/fn/busy'
], function($, ep, epDict) {

    'use strict';
    var tCapturePacklinkAppQuestion = epDict.translate('{PacklinkUninstallDialogAppQuestion}');
    var tYesUninstallApp = epDict.translate('{yes}');
    var tCancel = epDict.translate('{Cancel}');
    var tClose = epDict.translate('{Close}');

    return function(UninstallAppCheck, div , objectid, CurrentWindow){
       var currentWindow = CurrentWindow || window;
       div.empty();
       var buttonCode = '<button type="button" id="CloseButtonForPacklink">' + tClose + '</button>';
       var b = $(buttonCode);
       div.append(b);
       var closebuton = $('#CloseButtonForPacklink');
       closebuton.on('click',  function(event) {$('#ShippingSettingsDialog').dialog('close');} );
       UninstallAppCheck.on('click', function(event) {
                event.preventDefault();
                var dialog = ep('<div/>')
                    .append('<div>' + tCapturePacklinkAppQuestion + '</div>')
                    .uiDialog({
                        modal: true,
                        width: 350,
                        height: 150,
                        buttons: [{
                            id: 'YesId',
                            text: tYesUninstallApp,
                            click: function() {
                                handleappuninstallation(ep(this), objectid ,currentWindow);
                                ep(this).busy('show');
                            }
                        }, {
                            id: 'CancelId',
                            text: tCancel,
                            click: function() {
                                ep(this).uiDialog('close');
                            }
                        }]
                    });
                dialog.uiDialog('open');	
       });
    };

    function handleappuninstallation(context, objectid, currentWindow) { 

        ep.ajax({
                data: {
                    'ObjectID': objectid,
                    'ChangeAction': 'JSONUninstallPackstationApp'
                },
                cache: false,
                type: "POST",
                dataType: "json"
            })
            .done(function(jsonData) {
                /* close dialog // reload window */
                context.busy('hide');
                context.empty();
                context.append('<div id="Response" class="DialogMessage MessageWarning">OK</div>');
                context.uiDialog('close');
                if (jsonData.redirectUrl.length == 0) {
                    currentWindow.location.reload();
                } else {
                    currentWindow.location.assign(jsonData.redirectUrl);
                }
            })
            .fail(function(jsonData) {
                /* return error*/
                context.busy('hide');
                context.empty();
                context.append('<div class="DialogMessage MessageWarning">ajax error</div>');
            });
    }
});