/// class ProspectDirector.Types.View.Finder

/**
@class Finder
@namespace ProspectDirector.Types.View
@constructor
**/
ProspectDirector.Types.View.Finder = Class.extend({

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

    init: function (options) {

        this.options = $.extend({
            dialogSelector: null,
            searchUrl: "",
            gridHeight: 300,
            viewStateId: null,
            textOnly: false
        }, options);

        this._initSearchBox();

        this._initGrid();

        this._registerEventHandlers();

    },

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

    /**
    @property get_dialog {Object}
    @protected
    **/
    get_dialog: function () {
        return $(this.options.dialogSelector);
    },

    /**
    @property get_searchBox {Object}
    @protected
    **/
    get_searchBox: function () {
        return this.get_dialog().find("[data-role=search]");
    },

    /**
    @property get_grid {Object}
    @protected
    **/
    get_grid: function () {
        return this.get_dialog().find("[data-role=grid]");
    },

    /**
    @property get_selectionCount {Object}
    @protected
    **/
    get_selectionCount: function () {
        return this.get_dialog().find("[data-role=selection-count]");
    },

    /**
    @property get_okButton {Object}
    @protected
    **/
    get_okButton: function () {
        return this.get_dialog().find("[data-role=dialog-button][data-command=ok]");
    },

    /**
    @property get_cancelButton {Object}
    @protected
    **/
    get_cancelButton: function () {
        return this.get_dialog().find("[data-role=dialog-button][data-command=cancel]");
    },

    /**
    @property get_depthSelector {Object}
    @protected
    **/
    get_depthSelector: function () {
        return this.get_dialog().find("[data-role=depth-selector]");
    },

    /**
    @property get_selection {Object}
    @protected
    **/
    get_selection: function () {
        return this.get_grid().data("selection");
    },

    /**
    @property get_searchOptions {Object}
    @protected
    **/
    get_searchOptions: function () {
        return this.get_dialog().find("[data-role=search-options]");
    },

    //---------------------------
    //--- EVENT HANDLERS
    //---------------------------

    /**
    @method _okButton_onClick
    @protected
    **/
    _okButton_onClick: function (sender, e) {
        alert("The \"_okButton_onClick\" must be overridden");
    },

    /**
    @method _cancelButton_onClick
    @protected
    **/
    _cancelButton_onClick: function (sender, e) {
        this.get_dialog().data("dialog").hide();
    },

    /**
    @method _searchOptions_onClick
    @protected
    **/
    _searchOptions_onClick: function (sender, e) {
        ProspectDirector.Types.Core.Dialogs.FinderOptions.show({
            callbacks: {
                ok: $.proxy(function () {

                    this.get_grid().data("kendoGrid").dataSource.read();

                }, this)
            }
        });
    },

    /**
    @method _grid_onClick
    @private
    **/
    _grid_onClick: function (sender, e) {

        var count = this.get_selection().get_selections().length,
            message = count + " item selected";

        if (count > 1) {
            message = count + " items selected";
        }

        this.get_selectionCount().text(message);

    },

    _grid_onDblClick: function (sender, e) {
        this._okButton_onClick(sender, e);
    },

    _depthSelector_onChange: function (sender, e) {
        this.get_grid().data("kendoGrid").dataSource.read();
    },

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

    /**
    @method _initSearchBox
    @private
    **/
    _initSearchBox: function () {

        this.get_searchBox().searchBox({
            watermark: "Find...",
            search: $.proxy(function (sender, e) {
                var grid = this.get_grid().data("kendoGrid");
                grid.dataSource._skip = 0;
                grid.dataSource.read();
            }, this)
        });

    },

    /**
    @method _initGrid
    @private
    **/
    _initGrid: function () {

        var columns = [
            {
                field: "name",
                title: "Name",
                width: 300,
                attributes: {
                    style: "white-space:nowrap;"
                }
            },
            {
                field: "usedBy",
                title: "Used By",
                width: 150,
                attributes: {
                    style: "white-space:nowrap;"
                }
            },
            {
                field: "foundIn",
                title: "Found In",
                width: 400,
                attributes: {
                    style: "white-space:nowrap;"
                }
            }
        ];

        /* if view is not expandable only display the name column in the grid */
        if (!this.options.expandable) {
            columns = columns.slice(0, 1);
        }

        this.get_grid().kendoGrid({

            height: this.options.gridHeight,

            selectable: null,

            resizable: true,

            reorderable: true,

            columns: columns,

            dataSource: {
                serverPaging: true,
                serverSorting: true,
                pageSize: this.options.pageSize,
                transport: {
                    read: {
                        url: ProspectDirector.RUNTIME.resolveUrl(this.options.searchUrl),
                        data: $.proxy(function () {
                            return {
                                viewStateId: this.options.viewStateId,
                                term: this.get_searchBox().data("searchBox").value(),
                                textOnly: this.options.textOnly,
                                depth: this._getSelectedDepth()
                            };
                        }, this)
                    }
                },
                schema: {
                    data: "data",
                    total: "totalCount"
                },
                sort: [
                    { field: "name", dir: "asc" }
                ]
            },

            sortable: {
                mode: "multiple",
                allowUnsort: true
            },

            scrollable: {
                virtual: true
            }

        });

        this.get_grid().disableTextSelection();

        this.get_grid().on("dblclick", "TR[data-uid]", $.proxy(this._grid_onDblClick, this));

        this.get_grid().data("selection", new ProspectDirector.Types.Core.GridSelectionManager({
            grid: this.get_grid(),
            pk: "id",
            keys: ["id"],
            selectionMode: "multiple",
            click: $.proxy(this._grid_onClick, this)
        }));

    },

    /**
    @method _getSelectedRows
    @private
    **/
    _getSelectedRows: function () {
        return this.get_grid().find(".k-grid-content tr.k-state-selected");
    },

    /**
    @method _getRow
    @private
    **/
    _getRow: function (rowElement) {
        return this.get_grid().data("kendoGrid").dataSource.getByUid($(rowElement).data().uid);
    },

    /**
    @method _getSelectedDepth
    @private
    **/
    _getSelectedDepth: function (rowElement) {

        var depth = this.get_depthSelector().val();

        if (depth === undefined || depth === null) {
            depth = 0;
        }

        return depth;

    },

    /**
    @method _registerEventHandlers
    @private
    **/
    _registerEventHandlers: function () {

        this.get_okButton().on("click", $.proxy(this._okButton_onClick, this));
        this.get_cancelButton().on("click", $.proxy(this._cancelButton_onClick, this));
        this.get_searchOptions().on("click", $.proxy(this._searchOptions_onClick, this));
        this.get_depthSelector().bind("change", $.proxy(this._depthSelector_onChange, this));

    }

});