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

import in.twizmwaz.cardinal.GameHandler;
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.CylinderRegion;
import in.twizmwaz.cardinal.util.FireworkUtil;
import in.twizmwaz.cardinal.util.TeamUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerMoveEvent;

import java.util.Random;

public class ProximityAlarm implements Module {

    private final String message;
    private final int flareRadius;
    private final RegionModule region;
    private final FilterModule detect;
    private final FilterModule notify;

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

    protected ProximityAlarm(final String message, final int flareRadius, final RegionModule region, final FilterModule detect, final FilterModule notify) {
        this.message = message;
        this.flareRadius = flareRadius;
        this.region = region;
        this.detect = detect;
        this.notify = notify;
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        if (region.contains(event.getTo().toVector()) && !region.contains(event.getFrom().toVector()) && detect.evaluate(event.getPlayer()).equals(FilterState.ALLOW) && TeamUtils.getTeamByPlayer(event.getPlayer()) != null && !TeamUtils.getTeamByPlayer(event.getPlayer()).isObserver() && GameHandler.getGameHandler().getMatch().isRunning()) {
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (notify.evaluate(player).equals(FilterState.ALLOW)) {
                    player.sendMessage(ChatColor.RED + message);
                    RegionModule radius = new CylinderRegion("radius", region.getCenterBlock().getVector(), flareRadius, 1);
                    int flareAmount = new Random().nextInt(6);
                    for (int f = 0; flareAmount > f; f++) {
                        FireworkUtil.spawnFirework(radius.getRandomPoint().getLocation(), event.getPlayer().getWorld());
                    }
                }
            }
        }
    }

}

@@ -1562,72 +1562,8 @@
 ) &&
- detect.evaluate(event.getPlayer()).equals(FilterState.ALLOW) &&
  Tea
@@ -1813,16 +1813,228 @@
      if 
+(notify == null) %7B%0A                    if (detect.evaluate(player).equals(FilterState.DENY)) %7B%0A                        player.sendMessage(ChatColor.RED + message);%0A                    %7D%0A                %7D else if 
 (notify.
@@ -2156,24 +2156,44 @@
         
+    %7D%0A            %7D%0A
         
 RegionMo
@@ -2184,16 +2184,20 @@
         
+    
 RegionMo
@@ -2305,24 +2305,16 @@
         
-        
 int flar
@@ -2356,32 +2356,24 @@
             
-  
-      
 for (int f =
@@ -2416,24 +2416,16 @@
         
-   
-     
 Firework
@@ -2517,48 +2517,8 @@
 ));%0A
-                    %7D%0A                %7D%0A
     

Set a default if notify is not specified, fixes #607