/*
 * Copyright 2012 s_wolff.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aegee.runanddine.registration;

import java.util.Date;
import java.util.UUID;

import org.aegee.runanddine.FrontendBasePage;
import org.aegee.runanddine.runanddine.RunAndDine;
import org.aegee.runanddine.util.data.ModelNotExistingException;
import org.aegee.runanddine.util.forms.DoubleTextField;
import org.aegee.runanddine.util.forms.DynamicForm;
import org.aegee.runanddine.util.forms.validators.PhoneNumberValidator;
import org.aegee.runanddine.util.forms.validators.PostalCodeValidator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.validation.validator.EmailAddressValidator;

/**
 * Formular page to register for a RunAndDine as single participant
 * 
 * @author s_wolff
 */
public class SingleRegistrationPage extends FrontendBasePage
{
	private static final long serialVersionUID = 1L;

	private static final Log LOG = LogFactory.getLog(SingleRegistrationPage.class);

	public SingleRegistrationPage(PageParameters parameters)
	{
		super(parameters);

		final RunAndDine event = getObjectOr404();

		// create model instance
		final SingleRegistration reg = new SingleRegistration();

		// if there is an event you should be able to sign up for
		if (event.getRegistrationDeadline().compareTo(new Date()) >= 0)
		{
			// create Form
			DynamicForm form = new DynamicForm("editor")
			{

				@Override
				protected void onSubmit()
				{
					reg.setRunAndDineId(event.getId());
					reg.setValidationToken(UUID.randomUUID().toString());
					SingleRegistrationManager.getInstance().save(reg);
					this.setResponsePage(new SuccessfulRegistrationPage(null, reg));
				}
			};

			add(new Label(
				"text",
				"This registration form is intended for all those, who want to participate in the next Run&Dine and have no cooking partner. If you register with this form you will get a cooking partner some days before the Run&Dine takes place."));
			form.addHeadline("Who are you?");
			form.addTextField(reg, "firstName", "First Name", true);
			form.addTextField(reg, "lastName", "Last Name", true);
			form.addTextField(reg, "email", "eMail", true).addInputBehaviour(EmailAddressValidator.getInstance());
			form.addTextField(reg, "phone", "Phone", true).addInputBehaviour(new PhoneNumberValidator());

			form.addHeadline("Where's your home?",
				"If you want to cook somewhere else please enter that address below", true);
			form.addDoubleTextField(reg, "streetName", "streetNo", "Street Address", true,
				DoubleTextField.GRID_WIDTH_80, DoubleTextField.GRID_WIDTH_20);
			form.addDoubleTextField(reg, "postalCode", "city", "Town", true, DoubleTextField.GRID_WIDTH_20,
				DoubleTextField.GRID_WIDTH_80).addLeftInputBehaviour(new PostalCodeValidator());
			form.addTextArea(reg, "addressAddition", "Address Addition<br>(e.g. room no.)", false, 3);
			form.addYesNoChoice(reg, "kitchenAvailable", "Kitchen available?", true);

			form.addHeadline("Misc");
			form.addTextArea(reg, "allergies", "Allergies and things<br>you'll never eat", false, 3);
			form.addYesNoChoice(reg, "vegetarian", "Are you Vegetarian?", true);
			form.addYesNoChoice(reg, "accommodation", "Accommodation needed?", true);
			form.addYesNoChoice(reg, "tvAllowed", "May we film you?", true);

			form.addHeadline("Any comments?");
			form.addTextArea(reg, "comment", "Comments", false, 8);

			form.addSubmitButton("Register");
			this.add(form);
		}
		else
		{
			add(new Label("text", "Unfortunately there is no Run&Dine to sign up for at the moment.")
				.add(new AttributeAppender("class", "ym-fbox-text ym-error")));
			add(new Label("editor", ""));
		}
	}

	private static RunAndDine getObjectOr404()
	{
		try
		{
			return RunAndDine.OBJECTS.getLatest();
		}
		catch (ModelNotExistingException e)
		{
			LOG.error("ModelNotExistingException");
			throw new AbortWithHttpErrorCodeException(404);
		}
	}
}
