/// class ProspectDirector.Web.UI.Canvas.Charts.AddField
/// using ProspectDirector.Web.UI.Canvas.Charts.FieldListCommand

/**
@class AddField
@constructor
@namespace Web.UI.Canvas.Charts
@extends Web.UI.Command
**/
ProspectDirector.Web.UI.Canvas.Charts.AddField = ProspectDirector.Web.UI.Canvas.Charts.FieldListCommand.extend({

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

    execute: function () {

        var featureSet = this.get_feature().get_featureSet();

        ProspectDirector.Web.UI.Queries.SelectColumnsDialog.open({
            config: {
                title: "Select Fields"
            },
            data: ProspectDirector.Web.UI.Canvas.Charts.ChartUtilities.GetQuerySettings(featureSet),
            callbacks: {
                ok: $.proxy(this._dialog_onOk, this)
            }
        });

    },

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

    _dialog_onOk: function (sender, e) {

        var that = this;

        this.get_editor().get_selection().clear();

        e.selections.each(function (index, field) {
            that.get_editor().add(that._buildField(field));
        });

        this.get_editor().refresh();

    },

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

    _buildField: function (field) {

        var result = {
            id: uuid.v4(),
            columnId: field.id,
            columnName: field.name,
            scalarFunctionId: null,
            scalarFunctionName: null
        };

        if (this.get_editor().get_fieldType() === "valueFields") {
            result.aggregateFunctionId = "count";
            result.aggregateFunctionName = "Count";
        }

        return result;

    }

});