/// class PD.Manage.Modules.ObjectModel.ExplorerFolderUpdateCommand

PD.Manage.Modules.ObjectModel.ExplorerFolderUpdateCommand =
PD.Commands.Command.extend({

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

    init: function (options) {
        this._super(options);
        this.props("id", "explorer-folder/update");
        this.props("enabled", true);
        this.props("icon", PD.Path.to16x16("floppy_disk.png"));
        this.props("label", "Save");
        //this.props("tooltip", "Save");
        this.props("visible", true);
    },

    setState: function (state) {
        if ("editor" in state) {
            this.state("editor", state.editor);
        }
        if ("eventBus" in state) {
            this.state("eventBus", state.eventBus);
        }
    },

    execute: function (e) {
        this._super(e);

        var that = this,
            editor = this.state("editor"),
            success = e.success,
            error = e.error;

        editor.save(function (e) {
            that._editorOnSaveSuccess(e, success);
        },
        function (e) {
            that._editorOnSaveError(e, error);
        });

    },

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

    _editorOnSaveSuccess: function (e, fn) {

        var args = this._transformSaveResult(e);

        if (args.role === "entitytype") {
            PD.context.get_default().get_events().publish("entity-type/update", args);
        }

        PD.context.get_default().get_events().publish("explorer-folder/update", args);

        PD.monitor.getActiveSource().setClean();

        if (fn) {
            fn(args);
        }

    },

    _editorOnSaveError: function (e, fn) {
        if (fn) {
            fn(e);
        }
    },

    _transformSaveResult: function (data) {

        var item = data.general,
            result = {
                role: item.folderId != null ? "entitytype" : "folder",
                id: item.folderId != null ? item.folderId : item.id,
                icon: item.icon,
                name: item.name,
                isTab: item.isTab,
                isHidden: item.isHidden
            };

        return result;

    }

});