/// class PD.Web.UI.Controls.ViewGridColumnRemoveSortCommand
/// using PD.Web.UI.Controls.ViewGridColumnCommand

PD.Web.UI.Controls.ViewGridColumnRemoveSortCommand =
PD.Web.UI.Controls.ViewGridColumnCommand.extend({

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

    setDefaults: function () {
        this._super();
        this.props("id", "column/remove-sort");
        this.props("enabled", true);
        this.props("visible", true);
        this.props("label", "Remove Sort");
        this.props("tooltip", "Remove Sort");
    },

    setState: function (state) {
        this._super(state);
        this.props("enabled", this._isEnabled());
    },

    execute: function (e) {
        this._super(e);

        var columnId = this.get_columnId(),
            dataSource = this.get_viewGrid().get_kendo().dataSource,
            oldSort = dataSource.sort(),
            newSort = [];

        newSort = _.reject(oldSort, function (column) {
            return column.field === columnId;
        });

        if (oldSort.length > newSort.length) {
            dataSource.sort(newSort);
            dataSource.read();
        }

    },

    //===================================
    // PROTECTED METHODS
    //===================================

    _isEnabled: function () {
        return this._super() 
            && this._isSorted();
    },

    _isSorted: function () {
        
        var result = null,
            columnId = this.get_columnId(),
            dataSource = this.get_viewGrid().get_kendo().dataSource,
            sort = dataSource.sort();

        result = _.find(sort, function (column) {
            return column.field === columnId;
        });

        return result != null;

    },

});