/// class ProspectDirector.Types.View.ColumnContextMenuColumn
/// using ProspectDirector.Types.Core.ContextMenu

/**
@class ColumnContextMenuColumn
@namespace ProspectDirector.Types.View
**/
ProspectDirector.Types.View.ColumnContextMenuColumn = ProspectDirector.Types.Core.ContextMenu.extend({

    //---------------------------
    //--- CONSTRUCTOR
    //---------------------------

    init: function (options) {

        this._super($.extend({
            parent: null
        }, options));

        this._filterInfo = new ProspectDirector.Types.View.FilterAddIn();

    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

    filterInfo: function () {
        return this._filterInfo;
    },

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

    get_contextMenuSelector: function () {
        return this.get_grid().get_grid().selector + " .k-grid-header .k-header :not([data-field=\"\"])";
    },

    //---------------------------
    //--- PROTECTED METHODS
    //---------------------------

    _executeCommand: function (command, e) {

        var trigger = e.$trigger,
            col = $(trigger).closest(".k-header"),
            colId = col.data().field;

        switch (command) {

            case "addFilter":

                this.applyFilter(colId, col);
                break;

            case "editFilter":

                this.applyFilter(colId, col);
                break;

            case "clearFilter":

                this.clearFilter(colId, col);
                break;

            case "removeColumn":

                var url = ProspectDirector.RUNTIME.resolveUrl("~/viewcolumn/removefromdesign?")
                            + $.param({
                                viewStateId: this.get_grid().get_viewStateId(),
                                columnId: colId
                            });

                PD.ajax({
                    url: url,
                    success: $.proxy(function (a, b, c) {
                        this.get_grid().create();
                    }, this)
                });

                break;

            case "addColumnLeft":

                ProspectDirector.Types.Core.Dialogs.FindColumnInDesign.show({
                    params: {
                        viewStateId: this.get_grid().get_viewStateId(),
                        columnId: colId,
                        insert: 0
                    },
                    callbacks: {
                        ok: $.proxy(function (sender, e) {
                            this._addColumn(colId, false, e);
                        },
                        this)
                    }
                });

                break;

            case "addColumnRight":

                ProspectDirector.Types.Core.Dialogs.FindColumnInDesign.show({
                    params: {
                        viewStateId: this.get_grid().get_viewStateId(),
                        columnId: colId,
                        insert: 0
                    },
                    callbacks: {
                        ok: $.proxy(function (sender, e) {
                            this._addColumn(colId, true, e);
                        },
                        this)
                    }
                });

                break;

        }

    },

    _configureContextMenu: function (sender, e) {

        var column = $(sender).closest(".k-header"),
            isFiltered = this.filterInfo().isFiltered(column);

        if (!isFiltered) {

            e.config.items.addFilter = {
                name: "Filter..."
            };

        };

        if (isFiltered) {

            e.config.items.editFilter = {
                name: "Edit Filter"
            };

            e.config.items.clearFilter = {
                name: "Clear Filter"
            };

        };

        e.config.items.sep1 = "sep1";

        e.config.items.addColumnLeft = {
            name: "Add columns left..."
        };

        e.config.items.addColumnRight = {
            name: "Add columns right..."
        };

        e.config.items.sep2 = "sep2";

        e.config.items.removeColumn = {
            name: "Remove column"
        };

    },

    //---------------------------
    //--- PRIVATE METHODS
    //---------------------------

    applyFilter: function (columnId, column) {

        var viewStateId = this.get_grid().get_viewStateId();

        ProspectDirector.Types.Core.Dialogs.QuickFilterEditor.show({
            
            params: {
                viewStateId: viewStateId,
                filterId: columnId
            },
            
            callbacks: {

                ok: $.proxy(function (sender, e) {

                    var url = ProspectDirector.RUNTIME.resolveUrl("~/viewfilter/addquick?");

                    url = url + $.param({
                        viewStateId: viewStateId,
                        filter: JSON.stringify(e)
                    });

                    PD.ajax({
                        url: url,
                        dataType: "json",
                        
                        success: $.proxy(function (data, jqXHR, status) {
                                this.get_grid().refresh();
                                this._filterInfo.filter(column);
                            }, this)

                    });

                }, this)

            }
        });

    },

    clearFilter: function (columnId, column) {

        var url = ProspectDirector.RUNTIME.resolveUrl("~/viewfilter/removequick?");

        url = url + $.param({
            viewStateId: this.get_grid().get_viewStateId(),
            filterId: columnId
        });

        PD.ajax({
            url: url,
            dataType: "json",
            
            success: $.proxy(function (data, jqXHR, status) {
                this.get_grid().refresh();
                this._filterInfo.unfilter(column); ;
            }, this)

        });

    },

    _addColumn: function (column, insertAfter, columns) {

        var additions = [],
            url = ProspectDirector.RUNTIME.resolveUrl("~/viewcolumn/addtodesign?");

        for (var i = 0; i < columns.length; i++) {
            additions[additions.length] = columns[i].id;
        }

        PD.ajax({
            url: url,
            type: "POST",
            
            data: {
                viewStateId: this.get_grid().get_viewStateId(),
                additions: additions,
                columnId: column,
                insertAfter: insertAfter
            },
            
            traditional: true,

            success: $.proxy(function (a, b, c) {

                this.get_grid().create();

            }, this)

        });

    }

});
