/// class ProspectDirector.Web.UI.Canvas.Charts.FieldListToolBar
/// using ProspectDirector.Web.UI.Controls.ToolBar

ProspectDirector.Web.UI.Canvas.Charts.FieldListToolBar = ProspectDirector.Web.UI.Controls.ToolBar.extend({

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

    _selection: null,

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

    init: function (options) {
        this._super(options);
        this.load(this.get_options().selection);
    },

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

    get_selection: function () {
        return this._selection;
    },

    get_editor: function () {
        return this.get_options().editor;
    },

    get_feature: function () {
        return this.get_options().feature;
    },

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

    load: function (selection) {

        var buttons = null;

        this._selection = selection;

        if (!selection || selection.count() === 0) {
            buttons = this._getButtons(0);
        }
        else {
            buttons = this._getButtons(selection.count());
        }

        this.clear();

        this.get_items().add({
            type: "group",
            items: buttons
        });

        this.render();

    },

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

    _getButtons: function (selectionCount) {

        var result = [],
            commands = new ProspectDirector.Web.UI.Canvas.FieldListCommands({
                feature: this.get_feature(),
                editor: this.get_editor()
            });

        result[result.length] = {
            toolTip: "Add",
            icon: "ADD.PNG",
            command: commands.addField,
            disabled: false
        };

        result[result.length] = {
            toolTip: "Remove",
            icon: "DELETE.PNG",
            command: commands.removeField,
            disabled: selectionCount === 0
        };

        result[result.length] = {
            toolTip: "Duplicate",
            icon: "COPY.PNG",
            command: commands.duplicateField,
            disabled: selectionCount === 0
        };

        this._addEditButton(selectionCount, result, commands);

        result[result.length] = {
            toolTip: "Remove All",
            icon: "ERASER2.PNG",
            command: commands.clearFields,
            disabled: this.get_editor().count() === 0
        };

        result[result.length] = {
            toolTip: "Move Up",
            icon: "ARROW2_UP_GREEN.PNG",
            command: commands.moveFieldUp,
            disabled: selectionCount === 0
        };

        result[result.length] = {
            toolTip: "Move Down",
            icon: "ARROW2_DOWN_GREEN.PNG",
            command: commands.moveFieldDown,
            disabled: selectionCount === 0
        };

        return result;

    },

    _addEditButton: function (selectionCount, result, commands) {

        switch (this.get_editor().get_fieldType()) {

            case "valueFields":

                result[result.length] = {
                    toolTip: "Edit",
                    icon: "PENCIL.PNG",
                    command: commands.editValueField,
                    disabled: selectionCount !== 1
                };

                break;

            case "categoryFields":

                result[result.length] = {
                    toolTip: "Edit",
                    icon: "PENCIL.PNG",
                    command: commands.editCategoryField,
                    disabled: selectionCount !== 1
                };

                break;

            case "legendFields":

                result[result.length] = {
                    toolTip: "Edit",
                    icon: "PENCIL.PNG",
                    command: commands.editLegendField,
                    disabled: selectionCount !== 1
                };

                break;
        }

    }

});