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

    'use strict';
    var tCaptureShippoAppQuestion = epDict.translate('{ShippoUninstallDialogAppQuestion}');
    var tYesUninstallApp = epDict.translate('{yes}');
    var tCancel = epDict.translate('{Cancel}');

    return function(UninstallAppCheck, SaveButton, objectid, CurrentWindow){
       var currentWindow = CurrentWindow || window;
       SaveButton.hide();
            UninstallAppCheck.on('click', function(event) {
                    event.preventDefault();
                    var dialog = ep('<div/>')
                        .append('<div>' + tCaptureShippoAppQuestion + '</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': 'Save'
                },
                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');
                currentWindow.location.reload();
            })
            .fail(function(jsonData) {
                /* return error*/
                context.busy('hide');
                context.empty();
                context.append('<div class="DialogMessage MessageWarning">ajax error</div>');
            });
    }
});