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

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

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

    execute: function () {

        var selection = this.get_editor().get_selection().get_selections().getAtIndex(0),
            context = this.get_feature().get_featureSet().get_data().context,
            targetData = this._getFieldData(selection.id);

        ProspectDirector.Web.UI.FeatureSets.JsonTargetEditorDialog.open({
            config: {
                title: "Legend Field - " + selection.columnName,
                height: 450,
                width: 500
            },
            data: {
                targetType: "ProspectDirector.Web.UI.Canvas.Elements.Charts.LegendField",
                targetData: targetData,
                layoutType: ProspectDirector.Web.UI.FeatureSets.Layouts.TabStripLayout,
                attributes: {
                    chart: {
                        value: context,
                        valueType: "ProspectDirector.Web.UI.FeatureSets.TargetContext"
                    }
                }
            },
            callbacks: {
                ok: $.proxy(this._dialog_onOk, this)
            }
        });

    },

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

    _dialog_onOk: function (sender, e) {

        var fields = this.get_feature().getValue("legendFields"),
            fieldsList = new ProspectDirector.Web.List(fields);

        fieldsList.each(function (index, field) {
            if (e.target.id === field.id) {
                fields[index] = $.extend({}, e.target);
            }
        });

        this.get_feature().setValue("legendFields", fields);

        this.get_editor().refresh();

    },

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

    _getFieldData: function (id) {

        var result = null,
            fields = this.get_feature().getValue("legendFields"),
            fieldsList = new ProspectDirector.Web.List(fields);

        fieldsList.each(function (index, field) {
            if (id === field.id) {
                result = field;
            }
        });

        return result;

    }


});