define(
    ['Global', 'User', 'Resources'],
    function (Global, user, resources) {
        'use strict';
        var WaitListEntryNEAR = Backbone.Model.extend({
            defaults: {
                appointmentRequestDate: "",
                cellPhone: "",
                dfn: "",
                eligibilityCodeExternal: "",
                eligibilityCodeInternal: "",
                facilityId: "",
                homePhone: "",
                "object-type": "WaitListEntry",
                "patientName": "",
                "ssn": "",
                "waitListType": "NEAR"
            },

            initialize: function () {
            },

            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.eligibilityCodeInternal || "NO");
                response.patientLink = patientLink;
                response.type = response.waitListType;
                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;

                return response;
            },

            getWaitlistId: function(response){
                return response.dfn + ";" + response.originatingDate;
            },

            getServiceConnectedCode: function(){
                return this.get('eligibilityCodeInternal') || "NO";
            }

        });

        WaitListEntryNEAR.singleInstance = null;

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

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

        return WaitListEntryNEAR;
    }
);
