define([
    'Global',
    'AccordionView',
    'DatatableView',
	'FutureApptDetailsView',
    'PatientContext',
	'BookedAppointments',
	'SelectedDetails',
    'Text!../data/bookingSections.json',
    'Text!../data/bookingFutureApptsTable.json',
],
    function (Global, AccordionView, DatatableView, FutureApptDetailsView,
                PatientContext, BookedAppointments, SelectedDetails,
                BookingSectionsData, FutureApptsTableData) {
        'use strict';
        var thisView;

        return Backbone.View.extend({
            initialize: function () {
                this.patient = PatientContext.instance().getPatient();
                thisView = this;
//				this.waitListCollection = new WaitList();
//				WaitListDetailsView.instance({patientWaitList: true});
            },

            render: function () {
                var $futureApptsTable,
//					$waitListTable,
//                    $consultsTable,
					that = this;

                getSidebarContent().empty();

                new AccordionView({
                    sections: JSON.parse(BookingSectionsData),
                    container: getSidebarContent(),
                    attributes: {"data-corners": "false"},
                    onExpand: thisView.onExpand,
                    onCollapse: thisView.onCollapse
                }).render();

				BookedAppointments.instance().fetch({
                    cache: false,
					url: this.patient.get("patient-booked-appointments").href + "?startDate=" + formatDate(new Date()),
					success: function (response) {
						$futureApptsTable = new DatatableView({
							rowSelected: that.showFutureApptDetails,
                            tableConfiguration: JSON.parse(FutureApptsTableData),
							noFetch: true,
							dataCollection: response.noCancelledAppts()
						}).render().getDataTable();

						getDataTableHeader($futureApptsTable);
					}
				});

//                $waitListTable = new DatatableView({
//                    rowSelected: this.showWaitListDetails,
//                    tableConfiguration: JSON.parse(WaitListTableData),
//                    tableDataURL: this.patient.get("patient-waitlist").href,
//                    dataCollection: this.waitListCollection
//                }).render().getDataTable();
//
//                getDataTableHeader($waitListTable);
//
//                $consultsTable = new DatatableView({
//                    rowSelected: this.showConsultDetails,
//                    tableConfiguration: JSON.parse(ConsultsTableData),
//                    tableDataURL: this.patient.get("patient-consults-vista").href,
//                    dataCollection: ConsultOrders.instance()
//                }).render().getDataTable();
//
//                getDataTableHeader($consultsTable);

                return this;
            },

            events: {
                'tap #process-request': "goToBooking"
            },

//            showWaitListDetails: function(e) {
//				var $target = $(e.currentTarget),
//					selectedDetails = SelectedDetails.instance(),
//					id = $target.jqmData("requestHref");
//
//				selectedDetails
//					.setCollection(thisView.waitListCollection)
//					.setId(id);
//            },
//
//            showConsultDetails: function() {
//                new ConsultsDetailsPatientView().render();
//            },

			showFutureApptDetails: function() {
				new FutureApptDetailsView().render();
			},

            onExpand: function(e) {
                Global.getUtilities().eventAggregator.trigger("default-view");

                $('.accordion').data().expanded = $(e.currentTarget).attr('id');

                $('.selected-active')
                    .removeClass('selected-active')
                    .prop({'title': 'Tap to see details.'});
            },

            onCollapse: function(e) {
                var $which = $(e.currentTarget),
                    accordionData = $('.accordion').data();

                if ($which.attr('id') === accordionData.expanded) {
                    accordionData.expanded = "";
                    Global.getUtilities().eventAggregator.trigger("default-view");
                }
            },
            goToBooking: function (e) {
                e.preventDefault();

                $('#calendar-tabs #calendar-button')
                    .trigger('tap')
                    .addClass('ui-btn-active');

                $('#calendar-tabs #patient-button').removeClass('ui-btn-active');
            }
        });
    }
);
