define(
    ['Global', 'User', 'Resources'],
    function (Global, user, resources) {
        'use strict';
        var WaitListEntry = Backbone.Model.extend({
            defaults: {
                "accessionno": "",
                "appointmentLength": "",
				"appointmentRequestDate": "",
	            "cellPhone": "",
	            "clinicId": "",
	            "clinicName": "",
	            "comment": "",
	            "dateLastUpdated": "",
	            "daysOnList": "",
	            "desiredDate": "",
				"dfn": "",
                "eligibilityCodeInternal": "",
                "eligibilityCodeExternal": "",
	            "enrolleeStatus": "",
	            "ewlIen": "",
                "facilityId": "",
	            "fastNonFasting": "",
	            "homePhone": "",
	            "lastName": "",
	            "object-type": "WaitListEntry",
	            "originatingDate": "",
	            "patientZip": "",
	            "priority": "",
	            "provider": "",
	            "recallDate": "",
	            "recallDate1": "",
	            "serviceConnected": "",
	            "ssn": "",
				"stopCode": "",
	            "user": "",
				"waitListType": ""
            },

            convertPercentage: function (pct) {
                return pct >= 50 ? "50 +" : pct < 50 && pct > 0 ? "50 -" : pct !== "" ? "NO" : "";
            },

            parse: function (response) {
				var facilityCode = user.toJSON()['vistaLocation'],
				    patientLink = resources.get('system-root').get('href') + "patient/dfn-" + facilityCode + "/" + response.dfn;

				response.scCategory = this.convertPercentage(response.serviceConnected || response.eligibilityCodeInternal);
				response.patientLink = patientLink;
                response.clinicName = response.clinicName || response.clinicId;
				response.type = response.waitListType;
                response.fastNonFasting = response.fastNonFasting === "f" ? "Yes" : "No";
                response.recallDate = formatDate(new Date(response.recallDate));
                response.recallDate1 = formatDate(new Date(response.recallDate1));
                response.originatingDate = formatDate(new Date(response.originatingDate));
				response.appointmentRequestDate = formatDate(new Date(response.appointmentRequestDate))
                response.dateLastUpdated = formatDate(new Date(response.dateLastUpdated));
                response.desiredDate = formatDate(new Date(response.desiredDate)) || response.recallDate || response.appointmentRequestDate;
                response.waitListId = this.getWaitlistId(response);
                response.detailsLink = response.waitListId;
                response.patientName = response.patientName || response.lastName;

                return response;
            },

            getWaitlistId: function(response){
                var waitListID = "";
                switch (response.type) {
                    case "EWL" :
                        waitListID = response.dfn + ";" + response.clinicName + ";" + response.daysOnList;
                        break;
                    case "RECALL" :
                        waitListID = response.dfn + ";" + response.clinicId + ";" + response.recallDate;
                        break;
                    case "NEAR" :
                        waitListID = response.dfn + ";" + response.originatingDate;
                        break;
                }
                return waitListID;
            }
        });

		WaitListEntry.singleInstance = null;

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

		WaitListEntry.clearInstance = function () {
			WaitListEntry.singleInstance = null;
		};

		return WaitListEntry;
    }
);
