define([
    'Global',
	'User',
	'Resources'
],
    function (Global, user, resources) {
        'use strict';
        var AppointmentRequest = Backbone.Model.extend({
            defaults: {
                "object-type": "AppointmentRequest",
                "dataIdentifier": {
                    "uniqueId": "",
                    "systemId": ""
                },
                "patientIdentifier": {
                    "uniqueId": "",
                    "assigningAuthority": ""
                },
                "link": {
                    "href": ""
                },
                "patient" : {
                    "patientId": "",
                    "firstName": "",
                    "lastName": "",
                    "textMessagingAllowed": false,
                    "textMessagingPhoneNumber": ""
                },
                "createdDate": "",
                "lastUpdatedDate": "",
                "secondRequest": false,
                "secondRequestSubmitted": false,
                "appointmentDate": "",
                "appointmentTime": "",
                "requestedPhoneCall": false,
                "optionDate1": "",
                "optionTime1": "",
                "optionDate2": "",
                "optionTime2": "",
                "optionDate3": "",
                "optionTime3": "",
                "appointmentType": "",
                "visitType": "",
                "purposeOfVisit": "",
                "otherPurposeOfVisit": "",
                "bestTimetoCall": [],
                "providerId": "",
                "providerName": "",
                "providerPersonClass": "",
                "providerOption": "",
                "facilityName": "",
                "facility": {
                    "name": "",
                    "type": "",
                    "facilityCode": "",
                    "state": "",
                    "city": "",
                    "address": "",
                    "parentSiteCode": ""
                },
                "email": "",
                "textMessagingAllowed": false,
                "textMessagingPhoneNumber": "",
                "phoneNumber": "",
                "status": "",
                "hasVeteranNewMessage": false,
                "hasProviderNewMessage": false,
                "providerSeenAppointmentRequest": false
            },
            initialize: function () {
                if (!this.has('appointmentRequestDetailCode')) {
                    this.set('appointmentRequestDetailCode', []);
                }
            },
            isValid: function () {
                return this.get('dataIdentifier').uniqueId;
            },
            parse: function (response) {
	            var facilityCode = user.toJSON()['vistaLocation'],
		            patientId = response.patientIdentifier.uniqueId,
		            patientLink = resources.get('system-root').get('href') + "patient/dfn-" + facilityCode + "/" + patientId;

	            response.patient.name = response.patient.lastName + ", " + response.patient.firstName;

                response.optionDate1 = response.optionDate1 !== "No Date Selected" ? produceDate(0, response.optionDate1) : response.optionDate1;
                response.optionDate2 = response.optionDate2 !== "No Date Selected" ? produceDate(0, response.optionDate2) : response.optionDate2;
                response.optionDate3 = response.optionDate3 !== "No Date Selected" ? produceDate(0, response.optionDate3) : response.optionDate3;
	            response.patientLink = patientLink;
	            response.uniqueId = response.dataIdentifier.uniqueId;
                return response;
            },
            secondRequestAllowed: function () {
                var lessThanSevenDays,
                    secondRequestSubmitted = this.get('secondRequestSubmitted'),
                    secondRequest = this.get('secondRequest'),
                    firstDay = new Date(this.get('lastUpdatedDate')),
                    seventhDay = new Date(subtractFromDateUnformatted(0, -7, 0, 0, 0, 0, 0, firstDay)),
                    today = new Date();

                seventhDay.setHours(23);
                seventhDay.setMinutes(59);
                seventhDay.setSeconds(59);
                today.setHours(0);
                today.setMinutes(0);
                today.setSeconds(0);

                lessThanSevenDays = today < seventhDay;

                return (lessThanSevenDays && !secondRequest && !secondRequestSubmitted);
            }
        });

	    AppointmentRequest.singleInstance = null;

	    AppointmentRequest.instance = function (attributes) {
		    if (attributes) {
			    AppointmentRequest.singleInstance = new AppointmentRequest(attributes);
		    } else {
			    AppointmentRequest.singleInstance = AppointmentRequest.singleInstance || new AppointmentRequest();
		    }

		    return AppointmentRequest.singleInstance;
	    };

	    AppointmentRequest.clearInstance = function (options) {
		    options = options || {};

		    var singleInstance = AppointmentRequest.singleInstance;

		    if (singleInstance && singleInstance.get('self')) {
				var url = singleInstance.get('self').href;

			    $.ajax({
				    type: "DELETE",
				    url: url + "/inprocess",
				    success: function () {
					    if ($.isFunction(options.success)) {
						    options.success();
					    }
				    }
			    });
		    } else {
			    if ($.isFunction(options.success)) {
				    options.success();
			    }
		    }

		    AppointmentRequest.singleInstance = null;
	    };

	    return AppointmentRequest;
    }
);
