/***
 * 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.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.nakedobjects.metamodel.adapter.NakedObject;
import org.nakedobjects.metamodel.adapter.oid.Oid;
import org.nakedobjects.runtime.context.NakedObjectsContext;
import org.nakedobjects.runtime.persistence.PersistenceSession;
import org.nakedobjects.runtime.persistence.adaptermanager.AdapterManager;

import com.pragprog.dhnako.carserv.dom.customer.Customer;
import com.pragprog.dhnako.carserv.wicket.app.ModelForm;
import com.pragprog.dhnako.carserv.wicket.app.NakedObjectsPropertyAdapter;

class EditCustomerForm extends Form implements ModelForm {

	private static final long serialVersionUID = 1L;

	private static final class LoadableDetachableModelUsingOid extends
			LoadableDetachableModel {
		private final Oid oid;
		private static final long serialVersionUID = 1L;

		private LoadableDetachableModelUsingOid(Oid oid) {
			this.oid = oid;
		}

		@Override
		protected Object load() {
			NakedObject adapter = getAdapterManager().getAdapterFor(oid);
			return adapter.getObject();
		}
	}
	
	private Oid oid;

	EditCustomerForm(final String id, final Oid oid) {
		super(id, new CompoundPropertyModel(new LoadableDetachableModelUsingOid(oid)));
		this.oid = oid;
	}

	final void init() {
		addLabelsAndFields();
	}

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

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

	private void addAndConfigureEmailField() {
		NakedObjectsPropertyAdapter nopa = 
			new NakedObjectsPropertyAdapter(new TextField("emailAddress"));
		nopa.addToFormAndConfigureRules(this);
	}
	
	private void addAndConfigureNotesField() {
		NakedObjectsPropertyAdapter nopa = 
			new NakedObjectsPropertyAdapter(new TextArea("notes"));
		nopa.addToFormAndConfigureRules(this);
	}

	private void addAndConfigureFeedbackField() {
		NakedObjectsPropertyAdapter nopa = 
			new NakedObjectsPropertyAdapter(new TextArea("feedback"));
		nopa.addToFormAndConfigureRules(this);
	}

	private void addAndConfigureSinceField() {
		DateTextField sinceField = new DateTextField("since", 
				new PropertyModel(
						new LoadableDetachableModelUsingOid(oid), 
						"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);
			}
		});
		sinceField.add(new DatePicker());
		
		NakedObjectsPropertyAdapter nopa = new NakedObjectsPropertyAdapter(sinceField);
		nopa.addToFormAndConfigureRules(this);
	}


	public Class<?> getModelClass() {
		return Customer.class;
	}

	
	private static PersistenceSession getPersistenceSession() {
		return NakedObjectsContext.getPersistenceSession();
	}

	private static AdapterManager getAdapterManager() {
		return getPersistenceSession().getAdapterManager();
	}


}