package net.glowstone.util;

import static com.google.common.base.Preconditions.checkArgument;

import java.util.HashMap;
import java.util.Map;
import net.glowstone.constants.GlowAchievement;
import net.glowstone.constants.GlowStatistic;
import net.glowstone.net.message.play.game.StatisticMessage;
import org.bukkit.Achievement;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.Statistic.Type;
import org.bukkit.entity.EntityType;

/**
 * A container for achievement and statistic tracking.
 */
public final class StatisticMap {

    private final Map<String, Integer> values = new HashMap<>();

    public StatisticMessage toMessage() {
        return new StatisticMessage(values);
    }

    ////////////////////////////////////////////////////////////////////////////
    // Helpers

    private int getValue(String key) {
        return values.getOrDefault(key, 0);
    }

    private void setValue(String key, int value) {
        values.put(key, value < 0 ? 0 : value);
    }

    private String name(Statistic stat) {
        return GlowStatistic.getName(stat);
    }

    private String name(Statistic stat, Material mat) {
        if (mat.isBlock()) {
            checkArgument(stat.getType() == Type.BLOCK, "Statistic " + stat + " is not a block statistic");
        } else {
            checkArgument(stat.getType() == Type.ITEM, "Statistic " + stat + " is not an item statistic");
        }
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private String name(Statistic stat, EntityType type) {
        checkArgument(stat.getType() == Type.ENTITY, "Statistic " + stat + " is not an entity statistic");
        throw new UnsupportedOperationException("Not yet implemented");
    }

    ////////////////////////////////////////////////////////////////////////////
    // Achievement and stat accessors

    public boolean hasAchievement(Achievement achievement) {
        return getValue(GlowAchievement.getName(achievement)) != 0;
    }

    public void setAchievement(Achievement achievement, boolean has) {
        setValue(GlowAchievement.getName(achievement), has ? 1 : 0);
    }

    public int get(Statistic stat) {
        return getValue(name(stat));
    }

    public int get(Statistic stat, Material material) {
        return getValue(name(stat, material));
    }

    public int get(Statistic stat, EntityType entityType) {
        return getValue(name(stat, entityType));
    }

    public void set(Statistic stat, int value) {
        setValue(name(stat), value);
    }

    public void set(Statistic stat, Material material, int value) {
        setValue(name(stat, material), value);
    }

    public void set(Statistic stat, EntityType entityType, int value) {
        setValue(name(stat, entityType), value);
    }

    public void add(Statistic stat, int modify) {
        String name = name(stat);
        setValue(name, getValue(name) + modify);
    }

    public void add(Statistic stat, Material material, int modify) {
        String name = name(stat, material);
        setValue(name, getValue(name) + modify);
    }

    public void add(Statistic stat, EntityType entityType, int modify) {
        String name = name(stat, entityType);
        setValue(name, getValue(name) + modify);
    }

    public Map<String, Integer> getValues() {
        return values;
    }
}

@@ -1199,32 +1199,53 @@
   checkArgument(
+%0A                    
 stat.getType() =
@@ -1257,16 +1257,36 @@
 e.BLOCK,
+%0A                   
  %22Statis
@@ -1365,32 +1365,53 @@
   checkArgument(
+%0A                    
 stat.getType() =
@@ -1645,16 +1645,33 @@
 rgument(
+%0A                
 stat.get

Fix CheckStyle issues in util/StatisticMap.java (#737)