/**
 * <p>
 * Copyright © 2009-2010, Bruce-Robert Pocock
 * </p>
 * <p>
 * Based upon public domain sample code provided by Authorize.net
 * </p>
 * <p>
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * </p>
 * <p>
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * </p>
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * </p>
 * 
 * @author brpocock
 */

package org.starhope.appius.messaging;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import org.starhope.appius.sql.SQLPeerList;

import sun.awt.AppContext;

import com.sun.sgs.app.DataManager;
import com.sun.sgs.app.NameNotBoundException;

/**
 * @author brpocock
 * 
 */
public class BadMailList extends SQLPeerList {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4116058706799325467L;

	/**
	 * @return Returns the singleton, global BadMailList.
	 */
	public static BadMailList get () {
		final DataManager dataManager = AppContext.getDataManager ();
		BadMailList singleton = null;
		try {
			singleton = (BadMailList) dataManager
			.getBinding ("BadMailList/global");
		} catch (final NameNotBoundException e) {
			singleton = null;
		}
		if (singleton == null) {
			singleton = new BadMailList ();
		}
		return singleton;
	}

	/**
	 * An index of bad eMail addresses with their expiry dates (for when
	 * to take them off of this list)
	 */
	private final ConcurrentHashMap <String, Date> badMail;

	/**
	 * Void constructor
	 */
	public BadMailList () {
		badMail = new ConcurrentHashMap <String, Date> ();
		final DataManager dataManager = AppContext.getDataManager ();
		dataManager.setBinding ("BadMailList/global", this);
	}

	/**
	 * Ban an eMail address from being used for a certain amount of
	 * time. This will also close any User accounts which are associated
	 * with it.
	 * 
	 * @param mail The address to ban
	 * @param until The date at which this ban expires
	 */
	public void add (final String mail, final Date until) {
		badMail.put (mail, until);
		// FIXME u.close (UserCloseReason.BAD_MAIL);

	}

	/**
	 * @param mail The eMail address to be tested
	 * @return true, if the eMail address is on the bad mail list and
	 *         not yet expired from it.
	 */
	public boolean isBad (final String mail) {
		if (badMail.contains (mail)) {
			final Date now = new Date ();
			if (badMail.get (mail).compareTo (now) <= 0) {
				badMail.remove (mail);
				return false;
			}
			return true;
		}
		return false;
	}

	/**
	 * Remove an eMail address from the banned list: administrative
	 * function.
	 * 
	 * @param mail The eMail address which will once again be allowed.
	 */
	public void remove (final String mail) {
		badMail.remove (mail);
	}
}
