package com.untamedears.citadel;

import java.util.LinkedHashMap;

import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;

import com.untamedears.citadel.entity.Reinforcement;
import com.untamedears.citadel.entity.ReinforcementMaterial;

/**
 * User: JonnyD
 * Date: 07/18/12
 * Time: 11:57 PM
 */
public class ConfigManager {
	
	private int flashLength;
	private int autoModeReset;
	private boolean verboseLogging;
	private double redstoneDistance;
	private int groupsAllowed;
	private long cacheMaxAge;
	private int cacheMaxChunks;

	public void load(){
		Citadel.getPlugin().reloadConfig();
		FileConfiguration config = Citadel.getPlugin().getConfig();
		config.options().copyDefaults(true);
        flashLength = config.getInt("general.flashLength");
        autoModeReset = config.getInt("general.autoModeReset");
        verboseLogging = config.getBoolean("general.verboseLogging");
        redstoneDistance = config.getDouble("general.redstoneDistance");
        groupsAllowed = config.getInt("general.groupsAllowed");
        cacheMaxAge = config.getLong("caching.max_age");
        cacheMaxChunks = config.getInt("caching.max_chunks");
        for (Object obj : config.getList("materials")) {
            LinkedHashMap map = (LinkedHashMap) obj;
            ReinforcementMaterial.put(new ReinforcementMaterial(map));
        }
        for (String name : config.getStringList("additionalSecurable")) {
            Material material = Material.matchMaterial(name);
            if (material != null) {
            	Reinforcement.SECURABLE.add(material.getId());
            } else {
            	try {
            		Reinforcement.SECURABLE.add(Integer.parseInt(name));
            	} catch (NumberFormatException e) {
            		Citadel.warning("Invalid additionalSecurable material " + name);
            	}
            }
        }
        for (String name : config.getStringList("nonReinforceable")) {
        	Material material = Material.matchMaterial(name);
            if (material != null) {
            	Reinforcement.NON_REINFORCEABLE.add(material.getId());
            } else {
            	try {
            		Reinforcement.NON_REINFORCEABLE.add(Integer.parseInt(name));
            	} catch (NumberFormatException e) {
            		Citadel.warning("Invalid nonReinforceable material " + name);
            	}
            }
        }
	ConfigurationSection hardenedMaterials = config.getConfigurationSection("hardenedMaterials");
	for (String materialName : hardenedMaterials.getKeys(false)) {
            Material material = Material.matchMaterial(materialName);
            if (material == null) {
                Citadel.warning("Invalid hardenedMaterials material " + materialName);
            } else {
		int breakCount = hardenedMaterials.getInt(materialName);
		// .1 sec * 100 == 10 sec max to break the quickest blocks seems reasonable.
		if (breakCount < 2 || breakCount > 100) {
                    Citadel.warning("Invalid hardenedMaterials breakCount " +
				    materialName + " " + Integer.toString(breakCount));
		} else {
                    Reinforcement.HARDENED_BREAK_COUNTS.put(material.getId(), breakCount);
		}
            }
	}
	}
	
	public double getRedstoneDistance(){
		return this.redstoneDistance;
	}
	
	public int getAutoModeReset(){
		return this.autoModeReset;
	}
	
	public int getFlashLength(){
		return this.flashLength;
	}
	
	public int getGroupsAllowed(){
		return this.groupsAllowed;
	}
	
	public boolean getVerboseLogging(){
		return this.verboseLogging;
	}
	
	public long getCacheMaxAge(){
		return this.cacheMaxAge;
	}
	
	public int getCacheMaxChunks(){
		return this.cacheMaxChunks;
	}

	public int getMaterialBreakCount(int materialId){
		Integer breakCount = Reinforcement.HARDENED_BREAK_COUNTS.get(materialId);
		if (breakCount == null) {
			return 1;
		}
		return (int)breakCount;
	}
}

@@ -2653,24 +2653,66 @@
 (false)) %7B%0D%0A
+%09    int materialId = Integer.MIN_VALUE;%0D%0A
             
@@ -2774,40 +2774,196 @@
 );%0D%0A
+%09
     
-        if (material == null
+if (material != null) %7B%0D%0A%09        materialId = material.getId();%0D%0A%09    %7D else %7B%0D%0A%09        try %7B%0D%0A%09%09    materialId = Integer.parseInt(materialName);%0D%0A%09%09%7D catch (NumberFormatException e
 ) %7B%0D
@@ -2971,32 +2971,36 @@
                 
+ 
+   
 Citadel.warning(
@@ -3055,34 +3055,70 @@
 Name);%0D%0A
-            %7D else
+%09%09%7D%0D%0A%09    %7D%0D%0A%09    if (materialId != Integer.MIN_VALUE)
  %7B%0D%0A%09%09in
@@ -3191,16 +3191,16 @@
 c * 
-1
+6
 00 == 
-1
+6
 0 se
@@ -3290,17 +3290,17 @@
 Count %3E 
-1
+6
 00) %7B%0D%0A 
@@ -3518,24 +3518,18 @@
 material
-.getId()
+Id
 , breakC

Add custom block ID support to hardened material configuration to support other plugins.