/*
 * 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.aegee.runanddine.util.forms.validators.StreetNumberValidator;
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 a group
 * 
 * @author s_wolff
 */
public class GroupRegistrationPage extends FrontendBasePage
{
	private static final long serialVersionUID = 1L;

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

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

		final RunAndDine event = getObjectOr404();

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

		if (event.getRegistrationDeadline().compareTo(new Date()) >= 0)
		{
			// create Form
			DynamicForm form = new DynamicForm("editor")
			{

				@Override
				protected void onSubmit()
				{
					reg.setRunAndDineId(event.getId());
					reg.setValidationToken1(UUID.randomUUID().toString());
					reg.setValidationToken2(UUID.randomUUID().toString());
					GroupRegistrationManager.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 a cooking partner."));
			form.addHeadline("Teammate 1: Who are you?");
			form.addTextField(reg, "firstName1", "First Name", true);
			form.addTextField(reg, "lastName1", "Last Name", true);
			form.addTextField(reg, "email1", "eMail", true).addInputBehaviour(EmailAddressValidator.getInstance());
			form.addTextField(reg, "phone1", "Phone", true).addInputBehaviour(new PhoneNumberValidator());

			form.addHeadline("Teammate 1: Misc");
			form.addTextArea(reg, "allergies1", "Allergies and things<br>you'll never eat", false, 3);
			form.addYesNoChoice(reg, "vegetarian1", "Are you Vegetarian?", true);
			form.addYesNoChoice(reg, "accommodation1", "Accommodation needed?", true);

			form.addHeadline("Teammate 2: Who are you?");
			form.addTextField(reg, "firstName2", "First Name", true);
			form.addTextField(reg, "lastName2", "Last Name", true);
			form.addTextField(reg, "email2", "eMail", true).addInputBehaviour(EmailAddressValidator.getInstance());
			form.addTextField(reg, "phone2", "Phone", true).addInputBehaviour(new PhoneNumberValidator());

			form.addHeadline("Teammate 2: Misc");
			form.addTextArea(reg, "allergies2", "Allergies and things<br>you'll never eat", false, 3);
			form.addYesNoChoice(reg, "vegetarian2", "Are you Vegetarian?", true);
			form.addYesNoChoice(reg, "accommodation2", "Accommodation needed?", true);

			form.addHeadline("Where will you cook?");
			form.addDoubleTextField(reg, "streetName", "streetNo", "Street Adress", true,
				DoubleTextField.GRID_WIDTH_80, DoubleTextField.GRID_WIDTH_20).addRightInputBehaviour(
				new StreetNumberValidator());
			form.addDoubleTextField(reg, "postalCode", "city", "Town", true, DoubleTextField.GRID_WIDTH_20,
				DoubleTextField.GRID_WIDTH_80).addLeftInputBehaviour(new PostalCodeValidator());
			form.addTextArea(reg, "adressAddition", "Address Addition<br>(e.g. room no.)", false, 3);

			form.addHeadline("Any comments?");
			form.addYesNoChoice(reg, "tvAllowed", "May we film you?", true);
			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);
		}
	}
}
