package in.twizmwaz.cardinal.module.modules.scorebox;

import in.twizmwaz.cardinal.GameHandler;
import in.twizmwaz.cardinal.chat.ChatConstant;
import in.twizmwaz.cardinal.chat.LocalizedChatMessage;
import in.twizmwaz.cardinal.chat.UnlocalizedChatMessage;
import in.twizmwaz.cardinal.event.ScoreUpdateEvent;
import in.twizmwaz.cardinal.module.Module;
import in.twizmwaz.cardinal.module.modules.filter.FilterModule;
import in.twizmwaz.cardinal.module.modules.filter.FilterState;
import in.twizmwaz.cardinal.module.modules.regions.RegionModule;
import in.twizmwaz.cardinal.module.modules.regions.type.BlockRegion;
import in.twizmwaz.cardinal.module.modules.score.ScoreModule;
import in.twizmwaz.cardinal.module.modules.team.TeamModule;
import in.twizmwaz.cardinal.util.ChatUtils;
import in.twizmwaz.cardinal.util.TeamUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;

public class Scorebox implements Module {

    private final RegionModule region;
    private final int points;
    private final FilterModule filter;
    private final HashMap<ItemStack, Integer> redeemables;

    protected Scorebox(final RegionModule region, final int points, final FilterModule filter, final HashMap<ItemStack, Integer> redeemables) {
        this.region = region;
        this.points = points;
        this.filter = filter;
        this.redeemables = redeemables;
    }

    @Override
    public void unload() {
        HandlerList.unregisterAll(this);
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerMove(PlayerMoveEvent event) {
        if (region.contains(new BlockRegion(null, event.getTo().toVector()))) {
            if (filter == null || filter.evaluate(event.getPlayer()).equals(FilterState.ALLOW)) {
                int points = 0;
                if (redeemables.size() > 0) {
                    for (ItemStack item : redeemables.keySet()) {
                        if (event.getPlayer().getInventory().contains(item)) {
                            for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
                                TeamModule playerTeam = TeamUtils.getTeamByPlayer(event.getPlayer());
                                if (playerTeam != null && score.getTeam() == playerTeam) {
                                    event.getPlayer().getInventory().remove(item);
                                    points += redeemables.get(item);
                                }
                            }
                        }
                    }
                }
                points += this.points;
                if (points != 0) {
                    TeamModule playerTeam = TeamUtils.getTeamByPlayer(event.getPlayer());
                    if (playerTeam != null) {
                        for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
                            if (score.getTeam() == playerTeam) {
                                score.setScore(score.getScore() + points);
                                Bukkit.getServer().getPluginManager().callEvent(new ScoreUpdateEvent(score));
                                ChatUtils.getGlobalChannel().sendLocalizedMessage(new UnlocalizedChatMessage(ChatColor.GRAY + "{0}", new LocalizedChatMessage(ChatConstant.UI_SCORED_FOR, new UnlocalizedChatMessage(playerTeam.getColor() + event.getPlayer().getDisplayName() + ChatColor.GRAY), new UnlocalizedChatMessage(ChatColor.DARK_AQUA + "{0}" + ChatColor.GRAY, points == 1 ? new LocalizedChatMessage(ChatConstant.UI_ONE_POINT) : new LocalizedChatMessage(ChatConstant.UI_POINTS, points + "" + ChatColor.GRAY)), new UnlocalizedChatMessage(playerTeam.getCompleteName()))));
                            }
                        }
                    }
                }
            }
        }
    }

}

@@ -1778,24 +1778,95 @@
 nt event) %7B%0A
+        if (GameHandler.getGameHandler().getMatch().isRunning()) %7B%0A    
         if (
@@ -1929,24 +1929,28 @@
 ctor()))) %7B%0A
+    
             
@@ -2051,16 +2051,20 @@
         
+ 
+   
 int poin
@@ -2071,16 +2071,20 @@
 ts = 0;%0A
+    
         
@@ -2137,24 +2137,28 @@
             
+    
 for (ItemSta
@@ -2211,24 +2211,28 @@
             
+  
+  
 if (event.ge
@@ -2270,24 +2270,28 @@
 ns(item)) %7B%0A
+    
             
@@ -2441,32 +2441,36 @@
                 
+    
 TeamModule playe
@@ -2547,32 +2547,36 @@
                 
+  
+  
 if (playerTeam !
@@ -2610,32 +2610,36 @@
 = playerTeam) %7B%0A
+    
                 
@@ -2733,32 +2733,36 @@
                 
+    
 points += redeem
@@ -2802,34 +2802,42 @@
                 
+   
+ 
 %7D%0A
+    
                 
@@ -2866,34 +2866,41 @@
                 
-%7D%0A
+    %7D%0A   
                 
@@ -2895,34 +2895,39 @@
                 
+ 
 %7D%0A
+    
                 
@@ -2920,32 +2920,36 @@
               %7D%0A
+    
                 
@@ -2963,32 +2963,36 @@
 += this.points;%0A
+    
                 
@@ -3022,32 +3022,36 @@
                 
+    
 TeamModule playe
@@ -3116,32 +3116,36 @@
                 
+    
 if (playerTeam !
@@ -3150,24 +3150,28 @@
  != null) %7B%0A
+    
             
@@ -3317,24 +3317,28 @@
             
+    
 if (score.ge
@@ -3354,32 +3354,36 @@
 = playerTeam) %7B%0A
+    
                 
@@ -3473,16 +3473,20 @@
         
+    
 Bukkit.g
@@ -3551,24 +3551,28 @@
 nt(score));%0A
+    
             
@@ -4129,24 +4129,58 @@
 Name()))));%0A
+                                %7D%0A
             

Check that the match is running for scoreboxes, fixes #250