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

import org.aegee.runanddine.BasePage;
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.wicket.markup.html.basic.Label;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.validation.validator.EmailAddressValidator;

/**
 * Page to add or edit SingleRegistrations
 * @author s_wolff
 */
public class SingleRegistrationEditor extends BasePage {

    public SingleRegistrationEditor(PageParameters parameters) {
        super(parameters);
        final int id = parameters.get("id").toInt();

        // check wheter this editor shall create a new run and dine event
        boolean createNew = super.createNew();
        final SingleRegistration reg = super.getObjectOrNew(SingleRegistration.OBJECTS);
        if (createNew) {
            reg.setRunAndDineId(id);
            reg.setValidationToken(UUID.randomUUID().toString());
        }

        // create Form
        DynamicForm form = new DynamicForm("editor") {

            @Override
            protected void onSubmit() {
                // response page should be set accoding to some GET information -> depends on the awesomeness of the GroupRegistration editor
                SingleRegistrationManager.getInstance().save(reg);
                this.setResponsePage(ParticipantManager.class, new PageParameters().add("id", reg.getRunAndDineId()));
            }
        };

        form.addHeadline("Participan Informaion");
        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("Participant Address");
        form.addDoubleTextField(reg, "streetName", "streetNo", "Street Address", 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, "addressAddition", "Address Addition", false, 3);
        form.addYesNoChoice(reg, "kitchenAvailable", "Has Kitchen", true);

        form.addHeadline("Misc");
        form.addTextArea(reg, "allergies", "Allergies", false);
        form.addYesNoChoice(reg, "vegetarian", "Is Veggi", true);
        form.addYesNoChoice(reg, "accommodation", "Needs Accommodation", true);
        form.addYesNoChoice(reg, "tvAllowed", "Allows TV", true);

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

        form.addHeadline("Validation information");
        form.addYesNoChoice(reg, "isValidated", "Validated", true);
        form.addTextField(reg, "validationToken", "Token", false).setEnabled(false);

        form.addSubmitButton(createNew ? "Create" : "Update");

        this.add(form);
        this.add(new Label("headline", createNew ? "Create new" : "Update"));
    }
}
