/* 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 2, or (at your option)
 * any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.l2dot.gameserver.util;

import java.util.logging.Logger;

import com.l2dot.Config;
import com.l2dot.gameserver.GameTimeController;

import javolution.util.FastMap;
import javolution.util.FastMap.Entry;

/**
 * Flood protector
 *
 * @author L2Dot
 */
public class FloodProtector
{
	private static final Logger _log = Logger.getLogger(FloodProtector.class.getName());
	private static FloodProtector _instance;

	public static final FloodProtector getInstance()
	{
		if (_instance == null)
		{
			_instance = new FloodProtector();
		}
		return _instance;
	}

	// =========================================================
	// Data Field
	private FastMap<Integer, Integer[]> _floodClient;

	 // =========================================================  
 		        // Enum  
 		        private static final int PROTECTEDACTIONSIZE = 8; 
	
 		        // reuse delays for protected actions (in game ticks 1 tick = 100ms)
	private static final int[] REUSEDELAY = new int[]{ Config.USEITEM_DELAY, Config.ROLLDICE_DELAY, Config.FIREWORK_DELAY, Config.ITEMPETSUMMON_DELAY, Config.HEROVOICE_DELAY, Config.MULTISELL_DELAY, Config.SUBCLASS_DELAY, Config.BUFF_DELAY, Config.GLOBAL_CHAT_DELAY, Config.UNKNOWN_PACKET_DELAY};
																	
	// protected actions
	public static final int PROTECTED_USEITEM		= 0;
	public static final int PROTECTED_ROLLDICE		= 1;
	public static final int PROTECTED_FIREWORK		= 2;
	public static final int PROTECTED_ITEMPETSUMMON	= 3;
	public static final int PROTECTED_HEROVOICE		= 4;
	public static final int PROTECTED_MULTISELL     = 6;
	public static final int PROTECTED_SUBCLASS      = 6;
	public static final int PROTECTED_STUCK_BUFF    = 7;
	public static final int PROTECTED_GLOBALCHAT	= 8;
	public static final int PROTECTED_UNKNOWNPACKET = 9; 
	
	// =========================================================
	// Constructor
	private FloodProtector()
	{
		_log.info("Initializing FloodProtector");
		_floodClient = new FastMap<Integer, Integer[]>(Config.FLOODPROTECTOR_INITIALSIZE).setShared(true);
	}

	/**
	 * Add a new player to the flood protector (should be done for all players
	 * when they enter the world)
	 * 
	 * @param playerObjId
	 */
	public void registerNewPlayer(int playerObjId)
	{
		// create a new array
		Integer[] array = new Integer[REUSEDELAY.length];
		for (int i=0; i<array.length; i++)
			array[i] = 0;

		// register the player with an empty array
		_floodClient.put(playerObjId, array);
	}

	/**
	 * Remove a player from the flood protector (should be done if player loggs
	 * off)
	 * 
	 * @param playerObjId
	 */
	public void removePlayer(int playerObjId)
	{
		_floodClient.remove(playerObjId);
	}

	/**
	 * Return the size of the flood protector
	 * 
	 * @return size
	 */
	public int getSize()
	{
		return _floodClient.size();
	}

	/**
	 * Try to perform the requested action
	 *
	 * @param playerObjId
	 * @param action
	 * @return true if the action may be performed
	 */
	public boolean tryPerformAction(int playerObjId, int action)
	{
		Entry<Integer, Integer[]> entry = _floodClient.getEntry(playerObjId);
				if (entry == null)
						return false; // player just disconnected
		Integer[] value = entry.getValue();

		if (value[action] < GameTimeController.getGameTicks())
		{
			value[action] = GameTimeController.getGameTicks() + REUSEDELAY[action];
			entry.setValue(value);
			return true;
		}
		return false;
	}
}