/// class PD.Manage.Modules.Resources.ActionEditorDialog
// using PD.Web.UI.Dialog

PD.Manage.Modules.Actions.ActionEditorDialog =
PD.Web.UI.Dialog.extend({
    
    //===================================
    // FIELDS
    //===================================

    _actionParams: null,
    _actionSelection: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (options) {

        this._super($.extend(true, {
            config: {
                modal: true,
                height: "400px",
                minHeight: "300px",
                width: "600px",
                minWidth: "400px",
                title: "Action Editor"
            }
        }, options));

    },

    //===================================
    // PROPERTIES
    //===================================

    get_okButton: function () {
        return this.get_node().find("BUTTON[data-id=ok]");
    },

    get_canelButton: function () {
        return this.get_node().find("BUTTON[data-id=cancel]");
    },

    //===================================
    // PROTECTED EVENT HANDLERS
    //===================================

    onInit: function () {
        this._init_selection_box();
        this._init_parameter_editor();
    },

    onActivate: function (e) {
        this.get_okButton().on("click", _.bind(this._okButton_onClick, this));
        this.get_canelButton().on("click", _.bind(this._cancelButton_onClick, this));
    },

    onComplete: function (e) {

        var data = this.get_options()["data"].action;
        var actionDefinition = PD.Actions.ActionDefinition.getActionDefinition(data.pluginId, data.id);
        this.onActionSelected(actionDefinition, data.parameters);
    
    },

    onActionSelected: function (actionDefinition, values) {

        var action = null;

        if (actionDefinition != null && actionDefinition.get_id() != null) {
            action = actionDefinition.toAction(values);
        }

        if (action != null) {
            this._actionSelection.setSelection(action.get_id(), action.getFullName());
            this._actionParams.setAction(action);
        }

    },

    //===================================
    // PRIVATE EVENT HANDLERS
    //===================================

    _okButton_onClick: function () {

        var value = this._actionParams.getValue();

        this.close({
            command: "ok",
            data: value
        });

    },

    _cancelButton_onClick: function () {
        this.close();
    },

    _selectionBox_onBrowse: function () {

        var that = this;

        PD.Manage.Modules.Actions.ActionSelectionDialog.open({
            callbacks: {
                ok: function (sender, e) {
                    that.onActionSelected(e);
                }
            }
        });

    },

    //===================================
    // PROTECTED METHODS
    //===================================

    template: function (options) {
        return [
            "<div class=\"-pd-action-editor-dialog\">" +
                "<div class='action-label'>Action:</div>" +
                "<div class='action'></div>" +
                "<div class='action-params-label' data-id='params'>Action Parameters:</div>" +
                "<div class='action-params' data-id='params'></div>" +
                "<button data-id=\"ok\" class=\"-pd-button\">OK</button>" +
                "<button data-id=\"cancel\" class=\"-pd-button\">Cancel</button>" +
            "</div>"
        ];

    },

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

    _init_parameter_editor: function () {

        this._actionParams = new PD.Manage.Modules.Actions.ActionParameterEditor({
            insertAt: ".action-params"
        });

        this.get_children().add(this._actionParams);

    },


    _init_selection_box: function () {

        this._actionSelection = new PD.Web.UI.Controls.SelectionBox({
            insertAt: ".action",
            callback: _.bind(this._selectionBox_onBrowse, this)
        });

        this.get_children().add(this._actionSelection);

    }

});

PD.Manage.Modules.Actions.ActionEditorDialog.open = function (options) {
    return new PD.Manage.Modules.Actions.ActionEditorDialog(options);
};