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

import in.twizmwaz.cardinal.GameHandler;
import in.twizmwaz.cardinal.event.ScoreUpdateEvent;
import in.twizmwaz.cardinal.module.Module;
import in.twizmwaz.cardinal.module.modules.team.TeamModule;
import in.twizmwaz.cardinal.util.TeamUtils;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PlayerDeathEvent;

public class ScoreModule implements Module {

    private final TeamModule team;
    private int score;
    private final int pointsPerKill;
    private final int pointsPerDeath;
    private final int max;

    public ScoreModule(final TeamModule team, final int pointsPerKill, final int pointsPerDeath, final int max) {
        this.team = team;
        this.score = 0;
        this.pointsPerKill = pointsPerKill;
        this.pointsPerDeath = pointsPerDeath;
        this.max = max;
    }

    @Override
    public void unload() {
    }

    public int getScore() {
        return score;
    }

    public int getPointsPerKill() {
        return pointsPerKill;
    }

    public int getPointsPerDeath() {
        return pointsPerDeath;
    }

    public int getMax() {
        return max;
    }

    public TeamModule getTeam() {
        return team;
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        if (event.getEntity().getKiller() != null) {
            if (TeamUtils.getTeamByPlayer(event.getEntity().getKiller()) != null) {
                if (TeamUtils.getTeamByPlayer(event.getEntity().getKiller()) == team) {
                    score += pointsPerKill;
                    Bukkit.getServer().getPluginManager().callEvent(new ScoreUpdateEvent(this));
                }
            }
        } else {
            if (TeamUtils.getTeamByPlayer(event.getEntity()) != null) {
                if (TeamUtils.getTeamByPlayer(event.getEntity()) == team) {
                    score -= pointsPerDeath;
                    Bukkit.getServer().getPluginManager().callEvent(new ScoreUpdateEvent(this));
                }
            }
        }
    }

    public static boolean matchHasScoring() {
        return matchHasPointsPerKill() || matchHasPointsPerDeath() || matchHasMax();
    }

    public static boolean matchHasPointsPerKill() {
        for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
            if (score.getPointsPerKill() != 0) {
                return true;
            }
        }
        return false;
    }

    public static boolean matchHasPointsPerDeath() {
        for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
            if (score.getPointsPerDeath() != 0) {
                return true;
            }
        }
        return false;
    }

    public static boolean matchHasMax() {
        for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
            if (score.getMax() != 0) {
                return true;
            }
        }
        return false;
    }

    public static int pointsPerKill() {
        int pointsPerKill = 0;
        for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
            pointsPerKill = score.getPointsPerKill();
        }
        return pointsPerKill;
    }

    public static int pointsPerDeath() {
        int pointsPerDeath = 0;
        for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
            pointsPerDeath = score.getPointsPerDeath();
        }
        return pointsPerDeath;
    }

    public static int max() {
        int max = 0;
        for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
            max = score.getMax();
        }
        return max;
    }
}

@@ -1329,24 +1329,61 @@
 nt event) %7B%0A
+        if (matchHasScoring()) %7B%0A    
         if (
@@ -1427,32 +1427,36 @@
 ) %7B%0A            
+  
+  
 if (TeamUtils.ge
@@ -1503,32 +1503,36 @@
 r()) != null) %7B%0A
+    
                 
@@ -1619,24 +1619,28 @@
             
+ 
+   
 score += poi
@@ -1663,32 +1663,36 @@
                 
+   
+ 
 Bukkit.getServer
@@ -1760,34 +1760,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A  
@@ -1784,32 +1784,36 @@
               %7D%0A
+    
         %7D else %7B
@@ -1817,32 +1817,36 @@
 e %7B%0A            
+    
 if (TeamUtils.ge
@@ -1897,32 +1897,36 @@
                 
+   
+ 
 if (TeamUtils.ge
@@ -1985,24 +1985,28 @@
             
+    
 score -= poi
@@ -2030,32 +2030,36 @@
                 
+ 
+   
 Bukkit.getServer
@@ -2111,32 +2111,54 @@
 teEvent(this));%0A
+                    %7D%0A
                 

Fix non-TDMs ending when the "score" is changed, fixes #73