/*jslint node: true, nomen: true, unparam: true */
/*global jquery, $, _, define, Marionette, jqm, describe, it, expect, beforeEach, spyOn */

define([ 'Global', 'moment',
	'Text!patient-search/templates/patient.html',
	'patient-search/models/Patient' ],
	function ( Global, moment, template, Patient) {
		'use strict';

		return Backbone.Marionette.ItemView.extend({
			model: Patient,
			tagName: 'li',
			template: _.template(template),
            templateHelpers: {
                formatDate: function (date, format) {
                    if (date) {
                        if (moment(date, 'MMM YYYY', true).isValid()) {
                            return date;
                        } else if (moment(date, 'YYYY', true).isValid()) {
                            return date;
                        } else {
                            return moment(date).format(format);
                        }
                    } else {
                        return '';
                    }
                },
                formatGender: function (gender, format) {
                    if (!gender) {
                        return '';
                    }

                    var genderFormats = {
                        single: gender.charAt(0),
                        lowercase: gender.toLowerCase(),
                        titlecase: (function(str) {
                            return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
                        })(gender),
                        uppercase: gender.toUpperCase()
                    };
                    return genderFormats[format];
                }
            }
		});
	});