define([
	'Global',
    'PatientContext'
],
	function (Global, PatientContext) {
		var apptRequestDetails = Backbone.Model.extend({
			defaults: {
				"age": "",
				"apptType": "",
				"apptLength": "",
				"bookingNotes": "",
                "cancelReason": "",
                "cancelCode": "",
				"clinicName": "",
				"clinicId": "",
				"creditCode": "",
                "currentStatus": "",
				"dateOfBirth": "",
				"dateTime": "",
				"desiredDate": "",
				"displayName": "",
				"end": "",
				"endTime": "",
				"gender": "",
				"patientIdentifier": {
					"assigningAuthority": "",
					"uniqueId": ""
				},
				"purpose": "",
                "remarks": "",
				"ssn": "",
				"start": "",
				"startDate": "",
				"startTime": "",
				"stopCode": ""
			},
			initialize: function () {
			},
			fillData: function (data) {
				var patient = PatientContext.instance().getPatient().toJSON();
				data = data || {};

				data.startDate = formatDate(data.start);
				data.startTime = formatTime(data.start);
				data.dateTime = data.startDate + " " + this.getRequestTime(data.start);
				data.endTime = data.end ? formatTime(data.end) : "";
				data.dateOfBirth = formatDate(patient.dateOfBirth);
				data.patientIdentifier = patient.patientIdentifier;
				data.age = patient.age;
				data.displayName = patient.displayName;
				data.ssn = patient.ssn;
				data.gender = patient.gender;

				this.set(data);
			},
			isValid: function () {
				var start = this.get('start');

				return start;
			},
			getRequestTime: function (date) {
				var hour = date.getHours(),
					minutes = date.getMinutes();

				hour = hour < 10 ? "0" + hour : hour;
				minutes = minutes < 10 ? "0" + minutes : minutes;

				return hour + ":" + minutes + ":00";
			},
			toBookRequestJSON: function () {
				var thisJSON = this.toJSON(),
					data = {
                        "apptType": thisJSON.apptType,
                        "apptLength": thisJSON.apptLength,
                        "bookingNotes": thisJSON.bookingNotes,
                        "clinicId": thisJSON.clinicId,
                        "dateTime": thisJSON.dateTime,
                        "bookedApptDateTime": thisJSON.dateTime,
						"desiredDate": thisJSON.desiredDate,
                        "patientIdentifier": thisJSON.patientIdentifier,
                        "purpose": thisJSON.purpose
				    };

				return data;
			},
            toCancelRequestJSON: function(){
                var thisJSON = this.toJSON(),
                    data = {
						"clinicId": thisJSON.clinicId,
						"appointmentTime": thisJSON.dateTime,
						"cancelCode": thisJSON.cancelCode,
                        "cancelReason": thisJSON.cancelReason,
                        "remarks": thisJSON.remarks
                    };

                return data;
            }
		});

		apptRequestDetails.singleInstance = null;

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

		apptRequestDetails.clearInstance = function () {
			apptRequestDetails.singleInstance = null;

			return apptRequestDetails;
		};
		
		return apptRequestDetails;
	}
);