define([
	'Global',
	'AppointmentRequest',
	'RequestMetaData',
	'SchedulingResourceDirectory',
	'User'
],
    function (Global, AppointmentRequest, requestMetaData, SchedulingResourceDirectory, user) {
        'use strict';
        var AppointmentRequests = Backbone.Collection.extend({
            model: AppointmentRequest,
            initialize: function () {
                var numberOfDaysInThePast = requestMetaData.get('farthestTimeVeteranCanRequestAppointmentInDays'),
                    parentSiteCode = user.get('vistaLocation');

                this.url = SchedulingResourceDirectory.instance().get('appointment-requests').href + '?startDate=' + subtractFromDate(0, numberOfDaysInThePast, 0, 0, 0, 0, 0, new Date());
                this.url += '&noSort=true';

                if (typeof parentSiteCode !== 'undefined' && parentSiteCode !== null) {
                    this.url += '&parentSiteCode=' + parentSiteCode;
                }
            },
            parse: function (response) {
	            this.currentPage = response.currentPage;
	            this.pageCount = response.pageCount;
	            this.recordCount = response.recordCount;
	            this.startRecord = response.startRecord;
	            this.stopRecord = response.stopRecord;
                this.lastAccessDate = response.lastAccessDate;
                return response.appointmentRequest;
            },
            findByAppointmentRequestId: function (appointmentRequestId) {
                return this.find(function (appointmentRequest) {
                    return appointmentRequest.get('dataIdentifier').uniqueId === appointmentRequestId;
                });
            }
        });

	    AppointmentRequests.singleInstance = null;

        AppointmentRequests.instance = function () {
            AppointmentRequests.singleInstance = AppointmentRequests.singleInstance || new AppointmentRequests();

            return AppointmentRequests.singleInstance;
        };

        return AppointmentRequests;
    }
);