/*
 * Created on Jan 1, 2005
 * Copyright (C) 2004, 2005 Andrea Schweer
 *
 * This file is part of the Greenstone Alerting Service.
 * Refer to the COPYING file in the base directory of this package
 * for licensing information.
 */
package org.greenstone.gsdlas;

import java.util.*;
import java.util.Map;
import java.util.Set;

import javax.mail.MessagingException;

import org.greenstone.gsdlas.profiles.Subscription;
import org.greenstone.gsdlas.util.Mailer;

/**
 * @author andrea
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 * 
 * Singleton
 */
public class Notifier {

    private static Notifier instance; 
    
    private Notifier () {
        // hide default constructor
    }
    
    /**
     * @return
     */
    public static Notifier getInstance() {
        if (instance == null) instance = new Notifier();
        return instance;
    }

    /**
     * @param event
     * @param matchedSubscriptions
     */
    public void sendNotifications(Map event, Set matchedSubscriptions) {
        // TODO read smtp host and sender from configuration file
        Mailer mailer = new Mailer(System.getProperty("user.name") + "@localhost", "localhost");
        String eventText = eventToEmail(event);
        String subject = "Event for your subscription";
        
        for (Iterator iter = matchedSubscriptions.iterator(); iter.hasNext();) {
            Subscription sub = (Subscription) iter.next();
            if (!sub.wantsEMailNotification()) continue;

            StringBuffer message = new StringBuffer();
            // TODO formatting of notification email via velocity?
            message.append("You've created a subscription at the Greenstone Alerting Service running on " +
            		"localhost:\n\n");
            message.append(sub.toString());
            message.append("\n");
            message.append("An event has occurred that matches this subscription:\n\n");
            message.append(eventText);
            message.append("\n");
            message.append("Go to http://localhost:8080/alerting/service?action=listSubscriptions to " +
            		"change your subscriptions, or to http://localhost:8080/alerting/service?action=showEvents&subscriptionID=" + 
            		sub.getId() + " to see all events that match the subscription.\n");
            
            String to = sub.getMailAddress();
            
            try {
                mailer.sendMail(to, subject, message.toString());
            } catch (MessagingException e) {
                System.err.println("Couldn't notify for subscription " + sub.getId() + ": " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    /**
     * @param event
     * @return
     */
    private String eventToEmail(Map event) {
        StringBuffer message = new StringBuffer();
        for (Iterator iter = event.keySet().iterator(); iter.hasNext();) {
            String key = (String) iter.next();
            String value = (String) event.get(key);
            message.append(key + ": " + value + "\n");
        }
        return message.toString();
    }

}
