/*
 * 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.registrationvalidation;

import java.util.Date;

import org.aegee.runanddine.BasePage;
import org.aegee.runanddine.runanddine.RDManager;
import org.aegee.runanddine.runanddine.RunAndDine;
import org.aegee.runanddine.util.data.ModelNotExistingException;
import org.aegee.runanddine.util.forms.DynamicForm;
import org.aegee.runanddine.util.forms.FurtherReadingHeadline;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
 * Editor for the RegistrationValidationMail
 * 
 * @author s_wolff
 */
public class RegistrationValidationMailEditor extends BasePage
{
	private static final long serialVersionUID = 1L;

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

	public RegistrationValidationMailEditor(RunAndDine event)
	{
		this(new TemporaryMail(event.getId()));
	}

	public RegistrationValidationMailEditor(PageParameters parameters)
	{
		this(new TemporaryMail(parameters.get("id").toInt()));
	}

	RegistrationValidationMailEditor(final TemporaryMail am)
	{
		super(null);

		Date currentDate = new Date();
		Date registrationDeadline;
		try
		{
			registrationDeadline = RDManager.getInstance().getById(am.getRunAndDineId()).getRegistrationDeadline();
		}
		catch (ModelNotExistingException e)
		{
			LOG.error("ModelNotExistingException");
			throw new AbortWithHttpErrorCodeException(404);
		}

		WebMarkupContainer warning = new WebMarkupContainer("warning");
		warning.setVisible(currentDate.before(registrationDeadline));
		this.add(warning);

		DynamicForm form = new DynamicForm("editor")
		{

			@Override
			protected void onSubmit()
			{
				super.setResponsePage(new RegistrationValidationMailPreview(am));
			}
		};

		Link link = createHowToWriteAnAbstractMailPopup(FurtherReadingHeadline.ID_LINK,
			RegistrationValidationMailRenderer.getRenderableKeys());
		form.addFurtherReadingHeadline("Write the Mail!",
			"You might want to read this Tutorial about how to write an abstract Mail", link);
		form.addTextField(am, "subject", "Subject", true);
		form.addTextArea(am, "content", "Content", true, 25);

		form.addSubmitButton("Continue to Preview");
		this.add(form);
	}
}
