define([
    'Global',
    'DatatableView',
    'SchedulingResourceDirectory',
	'WaitListDetailsView',
	'WaitListEWL',
    'WaitListNEAR',
    'WaitListRecall',
	'SelectedDetails',
    'Text!../data/waitListEWLTable.json',
    'Text!../data/waitListNEARTable.json',
    'Text!../data/waitListRecallTable.json'
],
    function (Global,
              DatatableView,
              SchedulingResourceDirectory,
			  WaitListDetailsView,
			  WaitListEWL, WaitListNEAR, WaitListRecall,
			  SelectedDetails,
              EWLTableData, NEARTableData, RecallTableData) {
        var thisView,
			WaitListFilterView = Backbone.View.extend({

            initialize: function () {
				this.EWLList = new WaitListEWL();
				this.NEARList = new WaitListNEAR();
				this.recallList = new WaitListRecall();
				thisView = this;
            },
            render: function () {
	            WaitListDetailsView.instance({patientWaitList: false});

                var $container = getSidebarContent(),
                    schedulingResourceDirectory = SchedulingResourceDirectory.instance(),
                    ewlUrl = schedulingResourceDirectory.get("facility-ewl-waitlist").href,
                    nearUrl = schedulingResourceDirectory.get("facility-near-waitlist").href,
                    recallUrl = schedulingResourceDirectory.get("facility-recall-waitlist").href;

                this.$ewlTable = new DatatableView({
                        rowSelected: this.showEWLDetails,
                        tableConfiguration: JSON.parse(EWLTableData),
                        tableDataURL: ewlUrl,
                        dataCollection: this.EWLList
                    })
                    .render()
                    .getDataTable();

                this.$ewlHeader = getDataTableHeader(this.$ewlTable);

                this.$nearTable = new DatatableView({
                    rowSelected: this.showNEARDetails,
                    tableConfiguration: JSON.parse(NEARTableData),
                    tableDataURL: nearUrl,
                    dataCollection: this.NEARList
                })
                    .render()
                    .getDataTable();

                this.$nearHeader = getDataTableHeader(this.$nearTable);

				this.$recallTable = new DatatableView({
					rowSelected: this.showRecallDetails,
                    tableConfiguration: JSON.parse(RecallTableData),
					tableDataURL: recallUrl,
					dataCollection: this.recallList
				})
					.render()
					.getDataTable();

				this.$recallHeader = getDataTableHeader(this.$recallTable);

                $container
                    .append('<p>*List only shows actionable items</p>');

				this.delegateEvents();

                return $container;
            },
            events: {
                'change .filter': 'filterByValue',
				'keyup .filter': 'handleBackspace'
            },
            filterByValue: function (e) {
                this.filterTables(e.currentTarget.value, e.currentTarget.getAttribute('data-column'), false);
            },
            filterTables: function (filterValue, column, regularExpression) {
                this.$ewlTable.fnFilter(filterValue, column, regularExpression);
                this.$ewlHeader.setCount(this.$ewlTable);
            },
			handleBackspace: function (e) {
				var $target = $(e.currentTarget);

				if (e.which === 8 && $target.val() === "") {
					this.filterByValue(e);
				}
			},
            showEWLDetails: function (e) {
				var $target = $(e.currentTarget),
					id = $target.jqmData("requestHref");

				SelectedDetails
					.instance()
					.setCollection(thisView.EWLList)
					.setType("EWL")
					.setId(id);
            },
            showNEARDetails: function (e) {
				var $target = $(e.currentTarget),
					id = $target.jqmData("requestHref");

				SelectedDetails
					.instance()
					.setCollection(thisView.NEARList)
					.setType("NEAR")
					.setId(id);
            },
			showRecallDetails: function (e) {
				var $target = $(e.currentTarget),
					id = $target.jqmData("requestHref");

				SelectedDetails
					.instance()
					.setCollection(thisView.recallList)
					.setType("Recall")
					.setId(id);
			}
        });

        return new WaitListFilterView();
    }
);
