/// class ProspectDirector.Web.UI.Canvas.Charts.FieldList
/// using ProspectDirector.Web.UI.Controls.Control

/**
@constructor
@class FieldList
@namespace Web.UI.Canvas.Charts
**/
ProspectDirector.Web.UI.Canvas.Charts.FieldList = ProspectDirector.Web.UI.Controls.Control.extend({

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

    _grid: null,
    _selection: null,

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

    init: function (options) {

        this._super($.extend({}, {
            getFieldSource: null,
            setFieldSource: null,
            change: null,
            dblClick: null,
            fieldType: ""
        }, options));

        this.setDomNode("<div style=\"border:0px;\"></idv>");

        this.render();

    },

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

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

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

    get_grid: function () {
        return this._grid;
    },

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

    get_fieldType: function () {
        return this.get_options().fieldType;
    },

    //================================
    // INITIALIZERS
    //================================

    _init_grid: function () {

        var that = this;

        this._grid = this.get_domNode().kendoGrid({
            dataSource: {
                data: this._getFieldSource(),
                schema: {
                    model: {
                        id: "id"
                    }
                }
            },
            height: "100%",
            columns: [
                {
                    title: "Name",
                    width: 250,
                    template: $.proxy(this._getFieldName, this)
                }
            ]
        }).data("kendoGrid");

    },

    _init_grid_selection: function () {

        this._selection = new ProspectDirector.Web.UI.Utilities.Kendo.GridSelection({
            grid: this.get_grid(),
            primaryKey: "id",
            selection: {
                mode: "multiple"
            },
            dblclick: $.proxy(this._grid_onDblClick, this),
            change: $.proxy(this._grid_onChange, this),
            message: function (sender, e) {

                if (e.recordCount === 0) {
                    sender.addMessage("No fields have been added.");
                }
                else {
                    sender.removeMessage();
                }

            }
        });

    },

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

    _grid_onDblClick: function (e) {

        if ($.isFunction(this.get_options().dblclick)) {
            this.get_options().dblclick(this.get_selection());
        }

    },

    _grid_onChange: function (e) {
        this._triggerChange();
    },

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

    add: function (data) {

        var fields = new ProspectDirector.Web.List(this._getFieldSource()),
            source = [];

        fields.add(data);
        this.get_selection().add(data);

        source = fields.toArray();

        this._setFieldSource(source);

        this.refresh();

    },

    remove: function (id) {

        var that = this,
            fields = new ProspectDirector.Web.List(this._getFieldSource()),
            removed = new ProspectDirector.Web.List(),
            source = [];

        fields.each(function (index, field) {
            if (field["id"] === id) {
                removed.add(field);
            }
        });

        removed.each(function (index, field) {
            fields.remove(field);
            that.get_selection().get_selections().remove(field);
        });

        source = fields.toArray();

        this._setFieldSource(source);

        this.refresh();

    },

    clear: function () {
        this._setFieldSource([]);
        this.refresh();
        this._triggerChange();
    },

    count: function () {
        return this.get_grid().dataSource.data().length;
    },

    shiftUp: function (data) {

        var fields = new ProspectDirector.Web.List(this._getFieldSource());

        this._shiftFields(fields)

        this._setFieldSource(fields.toArray());

        this.refresh();

    },

    shiftDown: function (data) {

        var fields = new ProspectDirector.Web.List(this._getFieldSource());

        fields.reverse();

        this._shiftFields(fields)

        fields.reverse();

        this._setFieldSource(fields.toArray());

        this.refresh();

    },

    refresh: function () {
        this.get_grid().dataSource.data(this._getFieldSource());
        this.get_grid().refresh();
    },

    resize: function (height, width) {

        this.get_container().width(width);
        this.get_container().height(height);

        var filler = new ProspectDirector.Web.UI.Utilities.Kendo.Grid.Fill({
            selector: this.get_domNode(),
            useAvailableSpace: false
        });

        filler.size();

    },

    render: function (container) {
        this._super(container);
        this._init_grid();
        this._init_grid_selection();
    },

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

    _triggerChange: function () {

        if ($.isFunction(this.get_options().change)) {
            this.get_options().change(this.get_selection());
        }

    },

    _getBaseFields: function (value) {

        var result = null;

        if ($.isFunction(this.get_options().getBaseFields)) {
            result = this.get_options().getBaseFields();
        }

        return result;

    },

    _setFieldSource: function (value) {

        if ($.isFunction(this.get_options().setFieldSource)) {
            this.get_options().setFieldSource(value);
        }

    },

    _getFieldSource: function () {

        var result = null;

        if ($.isFunction(this.get_options().getFieldSource)) {
            result = this.get_options().getFieldSource();
        }

        if (!$.isArray(result)) {
            result = [];
        }

        return result;

    },

    _getFieldsToShift: function (fieldsList) {

        var selectionsList = this.get_selection().get_selections(),
            candidateList = new ProspectDirector.Web.List(),
            result = new ProspectDirector.Web.List(),
            keys = selectionsList.toKeysList(),
            pk = selectionsList.get_primaryKey();

        fieldsList.each(function (index, field) {

            var key = field[pk],
                keyIndex = keys.indexOf(key);

            if (keys.contains(key)) {
                candidateList.add(field);
            }

        });

        candidateList.each(function (index, candidate) {

            var fieldIndex = fieldsList.indexOf(candidate);

            if (fieldIndex > index) {
                result.add(candidate);
            }

        });

        return result;

    },

    _shiftFields: function (fields) {

        fieldsToShift = this._getFieldsToShift(fields);

        fieldsToShift.each(function (index, field) {
            var oldIndex = fields.indexOf(field);
            fields.move(field, oldIndex - 1);
        });

    },

    _getFieldName: function (e) {

        var result = "";

        result = ProspectDirector.Web.UI.Canvas.Charts.ChartUtilities.FormatFieldName(e.toJSON());

        return result;

    }

});