/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.wicket.ui;

import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.nakedobjects.plugins.headless.embedded.NakedObjectsMetaModel;

import com.pragprog.dhnako.carserv.dom.customer.Customer;

abstract class EditCustomerForm extends Form {
	private static final long serialVersionUID = 1L;

	EditCustomerForm(String id, Customer customer) {
		super(id, new CompoundPropertyModel(customer));
	}

	protected final Customer getCustomer() {
		return (Customer) getModelObject();
	}
	
	protected final NakedObjectsMetaModel getMetaModel() {
		NakedObjectsPanel parent = (NakedObjectsPanel) getParent();
		return parent.getMetaModel();
	}
	
	final void init() {
		addLabelsAndFields();
	}

	private void addLabelsAndFields() {
		addFeedbackPanel();
		addAndConfigureEmailField();
		addAndConfigureNotesField();
		addAndConfigureFeedbackField();
		addAndConfigureSinceField();
	}

	private void addFeedbackPanel() {
		add(new FeedbackPanel("wicketFeedback"));
	}

	private void addAndConfigureEmailField() {
		LabelAndFormComponent lafc = 
			new LabelAndFormComponent(new TextField("emailAddress"), "Email:");
		add(lafc);
		configureEmailFieldRules(lafc);
	}
	
	private void addAndConfigureNotesField() {
		LabelAndFormComponent lafc = 
			new LabelAndFormComponent(new TextArea("notes"), "Notes:");
		add(lafc);
		configureNotesFieldRules(lafc);
	}

	private void addAndConfigureFeedbackField() {
		LabelAndFormComponent lafc = 
			new LabelAndFormComponent(new TextArea("feedback"), "Feedback:");
		add(lafc);
		configureFeedbackFieldRules(lafc);
	}

	private void addAndConfigureSinceField() {
		DateTextField dateField = new DateTextField("since", new PropertyModel(getCustomer(), "since"){
			private static final long serialVersionUID = 1L;
			@Override
			public void setObject(Object object) {
				if (object == null || !java.util.Date.class.isAssignableFrom(object.getClass()) ) {
					super.setObject(object);
					return;
				}
				java.util.Date dt = (java.util.Date) object;
				java.sql.Date sqlDate = new java.sql.Date(dt.getTime());
				super.setObject(sqlDate);
			}
		});
		dateField.add(new DatePicker());
		
		LabelAndFormComponent lafc = new LabelAndFormComponent(dateField, "Since:");
		add(lafc);
		configureSinceFieldRules(lafc);
	}

	private void add(LabelAndFormComponent labelAndFormComponent) {
		add(labelAndFormComponent.getLabel());
		add(labelAndFormComponent.getFormComponent());
	}

	protected abstract void configureEmailFieldRules(LabelAndFormComponent lafc);

	protected abstract void configureNotesFieldRules(LabelAndFormComponent lafc);

	protected abstract void configureFeedbackFieldRules(LabelAndFormComponent lafc);

	protected abstract void configureSinceFieldRules(LabelAndFormComponent lafc);

}