/*
 * 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.OverviewPage;
import org.aegee.runanddine.status.RunAndDineStatus;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.RepeatingView;

/**
 * Preview for AdvertisementMail
 * @author s_wolff
 */
class AdvertisementMailPreview extends BasePage {

    /**
     * Page constructor
     * @param mail TemporaryMail to be displayed as preview
     */
    AdvertisementMailPreview(final TemporaryMail mail) {
        super(null);

        Form<Void> form = new Form<Void>("form") {

            @Override
            protected void onSubmit() {
                super.onSubmit();
                RunAndDineStatus.setStatusInformation(mail.getRunAndDineId(), AdvertisementMailEditor.class, "Mails written, sending...");
                mail.send();
                this.setResponsePage(OverviewPage.class);
            }
        };

        //form.add(new Label("recipiants", mail.getRecipients().toString()));
        RepeatingView repeater = new RepeatingView("recipiants");
        if (mail.isToRhrk()) {
            repeater.add(new Label(repeater.newChildId(), "RHRK Rundmail"));
        }
        if (mail.isToRhrk()) {
            repeater.add(new Label(repeater.newChildId(), "Archive"));
        }
        for (MailingList list : mail.getRecipients()) {
            repeater.add(new Label(repeater.newChildId(), list.toString()));
        }
        form.add(repeater);

        form.add(new Label("subject", mail.getRenderedSubject()));

        String contentLines[] = mail.getRenderedContent().split("\n");
        RepeatingView rv = new RepeatingView("content");
        for (int i = 0; i < contentLines.length; i++) {
            rv.add(new Label(rv.newChildId(), contentLines[i]));
            rv.add(new Label(rv.newChildId(), "<br>").setEscapeModelStrings(false));
        }
        form.add(rv);

        // back to the editor
        Link backLink = new Link("backLink") {

            @Override
            public void onClick() {
                this.setResponsePage(new AdvertisementMailEditor(mail));
            }
        };
        form.add(backLink);

        this.add(form);
    }
}
