define([
    'Global'
],
    function(Global) {
        var Clinic = Backbone.Model.extend({
            defaults: {
                "size": "",
                "object-type": "",
                "clinicId": "",
                "clinicName": "",
                "appointmentLength": "",
                "clinicDisplayStartTime": "",
                "displayIncrements": "",
                "active": true
            },
            parse: function(response) {
                var slots = response.appointmentTimeSlot || [],
	                that = this;

                $.each(slots, function(key, value) {
                    value.start = new Date(value.startDateTime);
                    value.end = new Date(value.endDateTime);
                    value.allDay = false;
                    value.clinicId = response.clinicId;
					value.clinicName = response.clinicName;
	                value.bookingStatus = value.bookingStatus ? that.getBookingCode(value.bookingStatus) : "No Availability";
	                value.stopCode = response.stopCode;
	                value.creditCode = response.creditCode;
	                value.apptLength = response.appointmentLength;
	                value.availability = typeof value.availability !== "undefined" ? value.availability : true;
	                value.title = "";
                    value.overbooks = value.remainingAllowedOverBookings;

                });

	            response.timeSlots = slots;

                response.className = 'clinic0';

                return response;
            },
            initialize: function() {
            },
	        getBookingCode: function(status) {
		        var bookingCode = "1",
			        charCode = status.charCodeAt(0);

		        if (charCode >= 106 && charCode <= 122) {
					bookingCode = charCode - 96;
		        } else if (status === " " || status.toUpperCase() === "NO AVAILABILITY"){
			        bookingCode = "No Availability";
		        } else {
			        bookingCode = status;
		        }

		        return bookingCode;
	        }
        });

        Clinic.singleInstance = null;

        Clinic.instance = function () {
            Clinic.singleInstance = Clinic.singleInstance || new Clinic();
            return Clinic.singleInstance;
        };

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

        return Clinic;
    }
);
