package net.new_liberty.pvpranker;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

/**
 * Wrapper class to interact with the database in an object-oriented fashion.
 *
 * Use this class in a different thread so the main thread isn't blocked with
 * database calls.
 */
public class PvPer {
    private final PvPRanker plugin;

    private final String name;

    public PvPer(PvPRanker plugin, String name) {
        this.plugin = plugin;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    /**
     * Gets the player this PvPer represents.
     *
     * @return Returns null if the player is not logged in.
     */
    public Player getPlayer() {
        return Bukkit.getPlayerExact(name);
    }

    /**
     * Gets the score of this PvPer.
     *
     * @return
     */
    public int getScore() {
        String query = "SELECT CAST(SUM(score) AS SIGNED) AS score "
                + "FROM pvpr_scores "
                + "WHERE player = ?";
        Object res = plugin.getDb().get(query, 0, name);
        if (res == null) {
            return 0;
        }
        return ((Integer) res).intValue();
    }

    /**
     * Gets the score of this PvPer at the given milestone.
     *
     * @param milestone
     * @return
     */
    public int getScore(String milestone) {
        String query = "SELECT CAST(SUM(score) AS SIGNED) AS score "
                + "FROM pvpr_scores "
                + "WHERE player = ? AND milestone = ?";
        Object res = plugin.getDb().get(query, 0, name, milestone);
        if (res == null) {
            return 0;
        }
        return ((Integer) res).intValue();
    }

    /**
     * Gets this player's rank.
     *
     * @return
     */
    public Rank getRank() {
        return plugin.getRank(getScore());
    }

    /**
     * Gets this player's rank at the given milestone.
     *
     * @param milestone
     * @return
     */
    public Rank getRank(String milestone) {
        return plugin.getRank(getScore(milestone));
    }

    /**
     * Gets the kill count of this PvPer.
     *
     * @return
     */
    public int getKillCount() {
        String query = "SELECT CAST(COUNT(id) AS SIGNED) AS value "
                + "FROM pvpr_kills "
                + "WHERE player = ?";
        Object res = plugin.getDb().get(query, 0, name);
        if (res == null) {
            return 0;
        }
        return ((Integer) res).intValue();
    }

    /**
     * Gets this player's kill count from the given milestone. Case sensitive.
     *
     * @param milestone
     * @return
     */
    public int getKillCount(String milestone) {
        String query = "SELECT CAST(COUNT(id) AS SIGNED) AS value "
                + "FROM pvpr_kills "
                + "WHERE player = ? AND milestone = ?";
        Object res = plugin.getDb().get(query, 0, name, milestone);
        if (res == null) {
            return 0;
        }
        return ((Integer) res).intValue();
    }

    /**
     * Adds a kill for the player.
     *
     * @param killed The player killed.
     * @param score
     */
    public void addKill(String killed, String milestone) {
        String query = "INSERT INTO pvpr_kills (player, killed, milestone) "
                + "VALUES (?, ?, ?)";
        plugin.getDb().update(query, name, killed, milestone);
    }

    /**
     * Adds a specified amount to the player's score for the given milestone.
     *
     * @param amount
     * @param milestone
     */
    public void addScore(int amount, String milestone) {
        String query = "INSERT INTO pvpr_scores (player, milestone, score) VALUES (?, ?, ?) "
                + "ON DUPLICATE KEY UPDATE score = score + ?";
        plugin.getDb().update(query, name, milestone, amount, amount);
    }
}

@@ -874,37 +874,32 @@
 query = %22SELECT 
-CAST(
 SUM(score) AS SI
@@ -888,35 +888,24 @@
 T SUM(score)
- AS SIGNED)
  AS score %22%0A
@@ -984,36 +984,40 @@
 ?%22;%0A        
-Object res =
+return ((Number)
  plugin.getD
@@ -1043,98 +1043,8 @@
 ame)
-;%0A        if (res == null) %7B%0A            return 0;%0A        %7D%0A        return ((Integer) res
 ).in
@@ -1257,21 +1257,16 @@
 %22SELECT 
-CAST(
 SUM(scor
@@ -1267,27 +1267,16 @@
 M(score)
- AS SIGNED)
  AS scor
@@ -1377,36 +1377,40 @@
 ?%22;%0A        
-Object res =
+return ((Number)
  plugin.getD
@@ -1447,98 +1447,8 @@
 one)
-;%0A        if (res == null) %7B%0A            return 0;%0A        %7D%0A        return ((Integer) res
 ).in
@@ -1967,37 +1967,32 @@
 query = %22SELECT 
-CAST(
 COUNT(id) AS SIG
@@ -1980,35 +1980,24 @@
 CT COUNT(id)
- AS SIGNED)
  AS value %22%0A
@@ -2075,36 +2075,40 @@
 ?%22;%0A        
-Object res =
+return ((Number)
  plugin.getD
@@ -2134,98 +2134,8 @@
 ame)
-;%0A        if (res == null) %7B%0A            return 0;%0A        %7D%0A        return ((Integer) res
 ).in
@@ -2375,13 +2375,8 @@
 ECT 
-CAST(
 COUN
@@ -2384,19 +2384,8 @@
 (id)
- AS SIGNED)
  AS 
@@ -2497,20 +2497,24 @@
     
-Object res =
+return ((Number)
  plu
@@ -2559,98 +2559,8 @@
 one)
-;%0A        if (res == null) %7B%0A            return 0;%0A        %7D%0A        return ((Integer) res
 ).in

Use Number instead of int for casting