/**
 * Summary widget for rendering enrollment summary (dealer and course) info.
 * @extends HD.Widget
 * 
 * @param {Object} config configuration object containing configurable properties 
 * @param {HD.REO.Model} model the model object, signals model events
 * @param {HD.REO.View} view the view object, facilitates the communication of events between widgets 
 */
HD.REO.View.Summary = function(config, model, view) {
	this.observers = [];
	this.config = config || {};
    
	if(model != null) {
		this.model = model;
		this.model.addObserver(this);
	}
	
	if(view != null) {
		this.view = view; 
	    this.view.addObserver(this);
	}
};

/**
 * Template object containing overrideable methods and properties.
 * @static
 */
HD.REO.View.Summary.template = {
	infoText : 	"Your reservation in the chosen class is an agreement between the _DEALER_NAME. " + 
				"Harley-Davidson dealers are independent business and are not agents of Harley-Davidson Motor Company. " + 
				"Harley-Davidson Motor Company does not have responsibility for agreements made with the dealership.<br/><br/>" + 
				"Many states offer a streamlined process for acquiring your Motorcycle License Endorsement as a benefit of passing this class. " +
				"Each state has different laws and policies regarding the benefits. " + 
				"Please confirm with your selected Harley-Davidson dealership or with your state Department of Motor Vehicles for motorcycle licensing requirements.",
	getInfoText : function(dealerName) {
		return HD.REO.View.Summary.template.infoText.replace(/_DEALER_NAME/, dealerName);
	}
};

HD.REO.View.Summary.prototype = {
	/**
	 * @see HD.Widget#update 
	 */
	update : function(eventName, eventData) {
		if(eventName == "model_GetSelectedCourse_Start") {
			this.loading(true);
		} else if(eventName == "model_GetSelectedCourse_Finish") {
			var formNumber = HD.util.Common.getRequestParam("form", true);
			var typeOfRegistration = eventData.openSeats > 0 ? "Reservation" : "Waiting+List";
			var courseType = HD.REO.template.courseTypesLabels[eventData.type].replace(/ /, "+");
			HD.util.Analytics.track(HD.REO.analytics.getSignupForm(courseType, typeOfRegistration, formNumber));
			this.loading(false);
			this.render(eventData);
		}
	},
	
	/**
	 * @see HD.Widget#render 
	 */
	render : function(data) {
		HD.util.Dom.get(this.config.summaryParent).innerHTML = this.getSummaryHtml(data);
		HD.util.Dom.get(this.config.infoParent).innerHTML = this.getInfoHtml(data);
	},
	
	/**
	 * @see HD.Widget#setListeners 
	 */
	setListeners : function(data) {},
	
	/**
	 * @see HD.Widget#getHtml 
	 */
	getSummaryHtml : function(data) {
    	var classes = HD.CSS_CLASSES;
    	var reoClasses = HD.REO.View.CSS_CLASSES;
    	
    	var date = HD.util.Date;
    	
    	var dateFormat = "mmm d',' yyyy";
    	var dayFormat = "ddd";
    	
    	var dealer = data.dealer;
    	var address = dealer.address;
    	var phone = dealer.phone;
    	
		var classDays = [];
		var schedule = data.schedule;
		if(schedule != null && schedule.classes != null) {
			for(var j = 0, len1 = schedule.classes.length; j < len1; j++) {
				classDays.push(schedule.classes[j].startTime);
			}
		}
    	
    	return ['<div class="', classes.WIDGET, ' ', reoClasses.SUMMARY ,'">' , 
		        	'<div>',
		        		dealer.name, '<br/>',
		        		address.street1, '<br/>',
		        		address.city, ', ', address.state, ' ', address.zip, '<br/>',
		        		'(', phone.npa, ') ', phone.nxx, '-', phone.station,
		        	'</div>',
    	        	'<div class="hdDivider">&nbsp;</div>',
    	        	'<div>',
    	        		'<div class="hdDates">',
    	        			HD.util.Date.formatSpan(data.startDate, data.endDate),
	    	        	'</div>',
	    	        	'<div class="hdDays">',
	    	        		HD.util.Date.formatDaySpan(classDays, 'ddd', ', '),
	    	        	'</div>',
    	        	'</div>',
    	        	'<div class="hdDivider">&nbsp;</div>',
    	        	'<div>',
    	        		'Fee: $', data.fee, '<br/>',
    	        		(HD.util.Common.hasValue(data.subTypeName) ? 'Course Sub-Type: ' + data.subTypeName : '') , 
		        	'</div>',
				'</div>'].join('');
	},
	
	/**
	 * @see HD.Widget#getHtml 
	 */
	getInfoHtml : function(data) {
		return HD.REO.View.Summary.template.getInfoText(data.dealer.name); 
	}
};

HD.util.Common.extend(HD.REO.View.Summary, [HD.Widget]);
