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

import org.aegee.runanddine.BasePage;
import org.aegee.runanddine.runanddine.RunAndDine;
import org.aegee.runanddine.util.forms.DynamicForm;
import org.aegee.runanddine.util.forms.FurtherReadingHeadline;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
 * Editor for AdvertisementMail
 * @author s_wolff
 */
public class AdvertisementMailEditor extends BasePage {

    /**
     * Page constructor for explicit RunAndDine object
     * @param event RunAndDine to be promoted
     */
    public AdvertisementMailEditor(RunAndDine event) {
        this(new TemporaryMail(event.getId()));
    }
    
    /**
     * Page constructor for RunAndDine id in page parameters
     * @param parameters HTTP parameters
     */
    public AdvertisementMailEditor(PageParameters parameters) {
        this(parameters, new TemporaryMail(parameters.get("id").toInt()));
    }

    /**
     * Page constructor for temporary mail
     * @param mail 
     */
    AdvertisementMailEditor(TemporaryMail mail) {
        this(null, mail);
    }

    /**
     * Page constructor for page parameters plus temporary mail
     * @param parameters HTTP parameters
     * @param am TemporaryMail
     */
    AdvertisementMailEditor(PageParameters parameters, final TemporaryMail am) {
        super(parameters);

        DynamicForm form = new DynamicForm("editor") {

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

        form.addHeadline("Who should get the Mail?");
        form.addYesNoChoice(am, "toRhrk", "RHRK Rundmail", true);
        form.addYesNoChoice(am, "toArchive", "Archive", true);
        form.addPanel(new MailingListSelectorPanel(form.newChildId(), am));

        Link link = createHowToWriteAnAbstractMailPopup(FurtherReadingHeadline.ID_LINK, AdvertisementMailRenderer.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);
    }
}
