/// class PD.Manage.Modules.Resources.UploadZipDialogCommand

PD.Manage.Modules.Resources.UploadZipDialogCommand =
PD.Commands.Command.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    setDefaults: function () {

        this.props("id", this.inits("id") || "zip/upload-dialog");
        this.props("enabled", true);
        this.props("label", "Upload Zip");
        this.props("icon", PD.Path.to16x16("folder_zip_up.png"));
        this.props("tooltip", "");
        this.props("canExecute", true);

        this.state("targetFolderId", null);
        
    },

    setState: function (state) {
        this._super(state);
        this.props("enabled", this.state("targetFolderId") && this.props("canExecute"));
    },

    execute: function (e) {
        this._super(e);
        this._executeServiceCall();
    },

    //===================================
    // PRIVATE METHODS
    //===================================   

    _executeServiceCall: function () {
        
        var that = this,
            targetFolderId = this.state("targetFolderId");

        PD.Manage.Modules.Resources.UploadZipDialog.open({ 
            data: {
                currentFolderId: targetFolderId
            },
            callbacks: {
                ok: function (sender, e) {
                    that._showTaskDialog(e.uploadId, e.folderId);
                }
            }
        });

    },

    _enabledAndCanExecute: function (value) {
        this.props("enabled", value);
        this.props("canExecute", value);
    },

    _showTaskDialog: function (uploadId, folderId) {

        var that = this,
            service = new PD.Manage.Services.ResourcesService();

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Unpack Zip"
            },
            showDetail: true,
            enableClose: false,
            taskStart: function (args) {

                service.addPackage(
                    uploadId,
                    folderId,
                    function (e) {
                        args.startPolling(e);
                    },
                    function (e) {
                        args.abort(e.responseJSON);
                        e.errorHandled = true;
                    });

            },
            taskSuccess: function (e) {
                e.enableClose(true);
            },
            close: function (e) {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("resources/add", {
                        folderId: folderId
                    });

            }
        });

    }


});