/*
 * 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 a GroupRegistration
 * @author s_wolff
 */
public class GroupRegistrationEditor extends BasePage {

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

        boolean createNew = super.createNew();
        final GroupRegistration reg = super.getObjectOrNew(GroupRegistration.OBJECTS);
        if (createNew) {
            reg.setRunAndDineId(id);
            reg.setValidationToken1(UUID.randomUUID().toString());
            reg.setValidationToken2(UUID.randomUUID().toString());
        }

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

            @Override
            protected void onSubmit() {
                GroupRegistrationManager.getInstance().save(reg);
                this.setResponsePage(ParticipantManager.class, new PageParameters().add("id", reg.getRunAndDineId()));
            }
        };

        form.addHeadline("Teammember 1");
        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.addTextArea(reg, "allergies1", "Allergies, ...", false, 3);
        form.addYesNoChoice(reg, "vegetarian1", "Is Veggi", true);
        form.addYesNoChoice(reg, "accommodation1", "Needs Accommodation", true);
        form.addYesNoChoice(reg, "isValidated1", "Validated", true);
        form.addTextField(reg, "validationToken1", "Token", false).setEnabled(false);

        form.addHeadline("Teammember 2");
        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.addTextArea(reg, "allergies2", "Allergies, ...", false, 3);
        form.addYesNoChoice(reg, "vegetarian2", "Is Veggi", true);
        form.addYesNoChoice(reg, "accommodation2", "Needs Accommodation", true);
        form.addYesNoChoice(reg, "isValidated2", "Validated", true);
        form.addTextField(reg, "validationToken2", "Token", false).setEnabled(false);

        form.addHeadline("Kitchen address");
        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", false, 3);

        form.addHeadline("Comments");
        form.addYesNoChoice(reg, "tvAllowed", "Allow TV", true);
        form.addTextArea(reg, "comment", "Comments", false, 8);
        form.addSubmitButton(createNew ? "Create" : "Update");

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