/// class PD.Manage.Modules.Actions.ActionParameterEditor
// using PD.Web.UI.Controls.Control

PD.Manage.Modules.Actions.ActionParameterEditor =
PD.Web.UI.Controls.Control.extend({

    //===================================
    // FIELDS
    //===================================

    _grid: null,
    _action: null,

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

    onInit: function (e) {
        this._init_grid();
    },

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

    template: function (options) {
        return [
            "<div style='position:absolute;top:0;left:0;right:0;bottom:0;'>",
            "</div>"
        ];
    },

    setAction: function (action) {

        if (action != null) {

            this._action = action;
            this._grid.get_dataSource().data(action.parametersToLiteral());

            setTimeout(function () {
                Tipped.create(".param-tooltip", { skin: "white", maxWidth: 200 });
            }, 500);

        }

    },

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

    _init_grid: function () {

        var that = this;

        this._grid = new PD.Web.UI.Controls.Grid({
            showToolbar: false,
            height: "100%",
            footer: {
                visible: false
            },
            dataSource: {
                data: []
            },
            selection: {
                map: function (e) {
                    return _.map(e.selections.toArray(), function (item, index) {
                        return {
                            id: item.id
                        };
                    });
                }
            },
            messaging: {
                noRecordsMessage: "No parameters."
            },
            selectionMode: "none",
            resizable: true,
            columns: [
                new PD.Web.UI.Controls.GridIconColumn({
                    field: "id",
                    width: "25px",
                    icon: "BULLET_BALL_GREEN.png",
                    transform: function (e) {
                        return e.optional === true ? "BULLET_SQUARE_GREEN.PNG" : "BULLET_SQUARE_RED.PNG"
                    }
                }),
                new PD.Web.UI.Controls.GridColumn({
                    field: "name",
                    title: "Parameter",
                    width: "125px",
                    attributes: {
                        style: "font-weight:bold;"
                    }
                }),
                new PD.Web.UI.Controls.GridColumn({
                    field: "type",
                    title: "Type",
                    width: "80px",
                    template: function (e) {
                        return PD.Actions.ActionParameterType.getName(e.type);
                    },
                    attributes: {
                        style: "padding:0 0 0 5px;position:relative;"
                    }
                }),
                new PD.Web.UI.Controls.GridTextboxColumn({
                    field: "text",
                    title: "Value",
                    width: "240px",
                    class: "param-value",
                    transform: function (e) {
                        return (e.text != null ? e.text : (e.value != null ? e.value : ""));
                    },
                }),
                new PD.Web.UI.Controls.GridColumn({
                    field: "name",
                    title: "&nbsp;",
                    width: "25px",
                    template: function (e) {
                        return "<div style='min-height:26px;width:min-width:25px;background-color:white;margin:0;padding:4px;box-sizing:border-box;'><img src='" + PD.Path.to16x16("help.png") + "' title=\"" +  e.description +"\" class='param-tooltip' /></div>"
                    },
                attributes: {
                        style: "padding:0;position:relative;"
                    }
                }),
                new PD.Web.UI.Controls.GridFillColumn()
            ]
        });

        this.get_children().add(this._grid);

    },

    getValue: function () {

        var action = this._action;
        
        if (action != null) {

            this.get_node().find(".param-value INPUT[type=text]").each(function (index) {

                var element = $(this);
                var param = action.get_parameters()[index];
                var value = element.val();

                param.set_text(value);

            });

        }

        return action

    }

});