/*
 * 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.FrontendBasePage;
import org.aegee.runanddine.registration.GroupRegistration;
import org.aegee.runanddine.registration.SingleRegistration;
import org.aegee.runanddine.runanddine.RDManager;
import org.aegee.runanddine.util.data.ModelNotExistingException;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
 * Page to validate a Single- or GroupRegistration by using the corresponding
 * token
 *
 * @author s_wolff
 */
public class RegistrationValidationPage extends FrontendBasePage {

    public RegistrationValidationPage(PageParameters parameters) {
        super(parameters);


        int radId = parameters.get("id").toInt();
        String providedToken = parameters.get("token").toString();

        Date actualDate = new Date();
        Date radDate;
        try {
            radDate = RDManager.getInstance().getById(radId).getRegistrationValidationDeadline();
        } catch (ModelNotExistingException e) {
            throw new AbortWithHttpErrorCodeException(404, "Das ist nicht das Run&Dine das ihr sucht.");
        }
        if (actualDate.after(radDate)) {
            this.add(new WebMarkupContainer("over")).setVisible(true);
            
            this.add(new WebMarkupContainer("succ").add(new Label("name")).setVisible(false));
            this.add(new WebMarkupContainer("fail").setVisible(false));
        } else {
            SingleRegistration sr;
            GroupRegistration gr;
            try {
                sr = SingleRegistration.OBJECTS.getByValidationToken(providedToken);
            } catch (ModelNotExistingException e) {
                sr = null;
            }
            try {
                gr = GroupRegistration.OBJECTS.getByValidationToken(providedToken);
            } catch (ModelNotExistingException e) {
                gr = null;
            }

            boolean validated = false;
            String name = "";
            if (sr != null && sr.getValidationToken().equalsIgnoreCase(providedToken)) {
                sr.setIsValidated(true);
                name = sr.getFirstName();
                SingleRegistration.OBJECTS.save(sr);
                validated = true;
            } else if (gr != null) {
                if (gr.getValidationToken1().equalsIgnoreCase(providedToken)) {
                    gr.setIsValidated1(true);
                    name = gr.getFirstName1();
                    validated = true;
                } else if (gr.getValidationToken1().equalsIgnoreCase(providedToken)) {
                    gr.setIsValidated2(true);
                    name = gr.getFirstName2();
                    validated = true;
                }
                GroupRegistration.OBJECTS.save(gr);
            }

            WebMarkupContainer succ = new WebMarkupContainer("succ");
            succ.add(new Label("name", name));
            this.add(succ);

            WebMarkupContainer fail = new WebMarkupContainer("fail");
            this.add(fail);
            
            this.add(new WebMarkupContainer("over").setVisible(false));

            fail.setVisible(!validated);
            succ.setVisible(!fail.isVisible());
        }
    }
}
