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

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

/**
 * A mail renderer for the advertisement mails.
 *
 * @author s_wolff
 */
public class AdvertisementMailRenderer extends MailRenderContext {

    private static final String NO_VALUE_DEFAULT_VALUE = "__ERROR__";
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
    private static final List<String> keys = Arrays.asList(
            "date", "registrationDeadline",
            "motto", "mottoDescription", "finalDestination",
            "finalDestinationDescription", "finalDestinationStreet",
            "finalDestinationHouseNo", "finalDestinationGeolink");

    /**
     * Create a mail renderer for advertisement mails for the given Run&Dine.
     *
     * @param evt the event that renderer belongs to - the render context is
     * gained from this event
     * @return a mail renderer that renders according to the information in the
     * passed event
     */
    public static AdvertisementMailRenderer create(RunAndDine evt) {
        return new AdvertisementMailRenderer(
                evt.getDate(), evt.getRegistrationDeadline(),
                evt.getMotto(), evt.getMottoDescription(), evt.getFinalDestination(),
                evt.getFinalDestinationDescription(), evt.getFinalDestinationStreet(),
                evt.getFinalDestinationHouseNo(), evt.getFinalDestinationGeolink());
    }

    /**
     * Shortcut for
     * <code>create(RunAndDine.OBJECTS.getById(runAndDineId));</code>
     *
     * @param runAndDineId
     * @return
     */
    public static AdvertisementMailRenderer create(int runAndDineId) throws ModelNotExistingException {
        return create(RunAndDine.OBJECTS.getById(runAndDineId));
    }

    /**
     * Provides a list of keys that can be rendered by every
     * <code>AdvertisementMailRenderer</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 AdvertisementMailRenderer(Object... values) {
        this(createDictFromValues(values));
    }

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

    /**
     * @TODO: add JavaDoc
     * @param values
     * @return 
     */
    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 ? NO_VALUE_DEFAULT_VALUE : values[i].toString());
            }
        }
        return dict;
    }
}
