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

/**
@class EditCategoryField
@constructor
@namespace Web.UI.Canvas.Charts
@extends Web.UI.Command
**/
ProspectDirector.Web.UI.Canvas.Charts.EditCategoryField = 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: "Category Field - " + selection.columnName,
                height: 450,
                width: 500
            },
            data: {
                targetType: "ProspectDirector.Web.UI.Canvas.Elements.Charts.CategoryField",
                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("categoryFields"),
            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("categoryFields", fields);

        this.get_editor().refresh();

    },

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

    _getFieldData: function (id) {

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

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

        return result;

    }

});