/**
 * Copyright (C) 2014 t7seven7t
 */
package net.galaxygaming.dispenser.event;

import net.galaxygaming.dispenser.GameDispenser;
import net.galaxygaming.dispenser.game.Game;
import net.galaxygaming.dispenser.game.GameManager;
import net.galaxygaming.util.FormatUtil;
import net.galaxygaming.util.SelectionUtil;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;

/**
 * @author t7seven7t
 */
class Events implements Listener {

    /*
     * Control what messages players can see while in game
     */
    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onAsyncPlayerChat(AsyncPlayerChatEvent event) {
        Game game = GameManager.getInstance().getGameForPlayer(event.getPlayer());
        if (game == null)
            return;
        
        game.broadcast(event.getFormat());
        event.setCancelled(true);
    }
    
    /*
     * Prevent players from using blacklisted commands while in game
     */
    @EventHandler(ignoreCancelled = true)
    public void onPlayerCommandPreproces(PlayerCommandPreprocessEvent event) {
        String command = event.getMessage()
                .replaceAll("^/*", "") // Remove forward slash if exists
                .replaceAll(" .*", ""); // Remove anything after first word
        
        for (String s : GameDispenser.getInstance().getBlacklistedCommands()) {
            if (s.equalsIgnoreCase(command)) {
                event.getPlayer().sendMessage(FormatUtil.format("&4Cannot use that command while playing a game."));
                event.setCancelled(true);
                return;
            }
        }
    }
    
    /*
     * Selecting blocks for arena creation
     */
	@EventHandler
	private void onPlayerInteractEvent(final PlayerInteractEvent event) {
		final Player player = event.getPlayer();
		if ((player.getItemInHand().getType() == SelectionUtil.getInstance()
				.getWand())) {
			if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
				// TODO: Send message from messages.properties saying point one was set
				SelectionUtil.getInstance().getSelection(player)
						.setPointOne(event.getClickedBlock().getLocation());
				event.setCancelled(true);
			} else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
				// TODO: Send message from messages.properties saying point two was set
				SelectionUtil.getInstance().getSelection(player)
						.setPointTwo(event.getClickedBlock().getLocation());
				event.setCancelled(true);
			}
		}
	}
}
@@ -263,16 +263,59 @@
 atUtil;%0A
+import net.galaxygaming.util.LocationUtil;%0A
 import n
@@ -2296,80 +2296,239 @@
 %0A%09%09%09
-%09// TODO: Send m
+    player.sendM
 essage
- from messages.properti
+(FormatUtil.format(GameDispenser.getInstance()%0A%09%09%09            .getMessages().getM
 es
- 
 sa
-ying point one was set
+ge(%22selection.pointSelected%22), %0A%09%09%09            %221%22, LocationUtil.serializeLocation(event.getClickedBlock().getLocation())));
 %0A%09%09%09
@@ -2733,83 +2733,269 @@
 ) %7B%0A
-%09%09%09%09// TODO: Send m
+                player.sendM
 essage
- from messages.properti
+(FormatUtil.format(GameDispenser.getInstance()%0A                        .getMessages().getM
 es
- 
 sa
-ying point two was set
+ge(%22selection.pointSelected%22), %0A                        %222%22, LocationUtil.serializeLocation(event.getClickedBlock().getLocation())));
 %0A%09%09%09

add message when making selection