/*
 * 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.text.SimpleDateFormat;
import java.util.*;

import org.aegee.runanddine.mail.MailRenderContext;
import org.aegee.runanddine.runanddine.RunAndDine;
import org.aegee.runanddine.util.data.ModelNotExistingException;

/**
 * A mail renderer for the registrationValidationMails
 *
 * @author c_stangohr
 */
public class RegistrationValidationMailRenderer extends MailRenderContext {

    private static final String ERROR_VALUE = "__ERROR__";
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
    private static final List<String> keys = Arrays.asList(
            "date", "validationDeadline",
            "motto", "validationLink", "name");

    /**
     * Create a mail renderer for the RegistrationValidationMail
     *
     * @param evt A run&dine event
     * @param valLink The full Link to registration validation-Page for the
     * participant. (registrationvalidation/RegistrationValidationPage/?token=
     * *insert here the user token*)
     * @param name the name of the recipant of the mail
     * @return
     */
    public static RegistrationValidationMailRenderer create(RunAndDine evt, String valLink, String name) {
        return new RegistrationValidationMailRenderer(
                evt.getDate(), evt.getRegistrationValidationDeadline(),
                evt.getMotto(), valLink, name);

    }

    /**
     * Shortcut for
     * <code>create(RunAndDine.OBJECTS.getById(runAndDineId));</code>
     *
     * @param runAndDineId
     * @return
     */
    public static RegistrationValidationMailRenderer create(int runAndDineId, String link, String name) throws ModelNotExistingException {
        return create(RunAndDine.getOBJECTS().getById(runAndDineId), link, name);
    }

    /**
     * Provides a list of keys that can be rendered by every
     * <code>registrationValidationMailRenderer</code> instance.
     *
     * @return list of keys that can be rendered
     */
    public static List<String> getRenderableKeys() {
        return new ArrayList<String>(keys);
    }

    /**
     * @TODO: add JavaDoc
     * @param values 
     */
    private RegistrationValidationMailRenderer(Object... values) {
        this(createDictFromValues(values));
    }

    /**
     * @TODO: add JavaDoc
     * @param context 
     */
    private RegistrationValidationMailRenderer(Map<String, String> context) {
        super(context);
    }

    private static Map<String, String> createDictFromValues(Object[] values) {
        assert values.length == keys.size();
        Map<String, String> dict = new HashMap<String, String>(values.length);
        for (int i = 0; i < values.length; i++) {
            if (values[i] instanceof Date) {
                dict.put(keys.get(i), DATE_FORMAT.format((Date) values[i]));
            } else {
                dict.put(keys.get(i), values[i] == null ? ERROR_VALUE : values[i].toString());
            }
        }
        return dict;
    }
}
