package au.com.mineauz.minigamesregions.conditions;

import java.util.Map;

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

import au.com.mineauz.minigames.MinigamePlayer;
import au.com.mineauz.minigames.MinigameUtils;
import au.com.mineauz.minigames.config.IntegerFlag;
import au.com.mineauz.minigames.menu.Menu;
import au.com.mineauz.minigames.menu.MenuItemPage;
import au.com.mineauz.minigames.menu.MenuItemTime;
import au.com.mineauz.minigames.minigame.Minigame;
import au.com.mineauz.minigamesregions.Node;
import au.com.mineauz.minigamesregions.Region;

public class MinigameTimerCondition extends ConditionInterface{
	
	private IntegerFlag minTime = new IntegerFlag(5, "minTime");
	private IntegerFlag maxTime = new IntegerFlag(10, "maxTime");

	@Override
	public String getName() {
		return "MINIGAME_TIMER";
	}

	@Override
	public String getCategory() {
		return "Minigame Conditions";
	}
	
	@Override
	public void describe(Map<String, Object> out) {
		out.put("Time", MinigameUtils.convertTime(minTime.getFlag(), true) + " - " + MinigameUtils.convertTime(maxTime.getFlag(), true));
	}

	@Override
	public boolean useInRegions() {
		return true;
	}

	@Override
	public boolean useInNodes() {
		return true;
	}

	@Override
	public boolean checkRegionCondition(MinigamePlayer player, Region region) {
		return check(player.getMinigame());
	}

	@Override
	public boolean checkNodeCondition(MinigamePlayer player, Node node) {
		return check(player.getMinigame());
	}
	
	private boolean check(Minigame mg){
        return mg.getMinigameTimer().getTimeLeft() >= minTime.getFlag() &&
                mg.getMinigameTimer().getTimeLeft() <= maxTime.getFlag();
    }

	@Override
	public void saveArguments(FileConfiguration config, String path) {
		minTime.saveValue(path, config);
		maxTime.saveValue(path, config);
		saveInvert(config, path);
	}

	@Override
	public void loadArguments(FileConfiguration config, String path) {
		minTime.loadValue(path, config);
		maxTime.loadValue(path, config);
		loadInvert(config, path);
	}

	@Override
	public boolean displayMenu(MinigamePlayer player, Menu prev) {
		Menu m = new Menu(3, "Minigame Timer", player);
		
		m.addItem(new MenuItemTime("Min Time", Material.WATCH, minTime.getCallback(), 0, null));
		m.addItem(new MenuItemTime("Max Time", Material.WATCH, maxTime.getCallback(), 0, null));
		
		m.addItem(new MenuItemPage("Back", Material.REDSTONE_TORCH_ON, prev), m.getSize() - 9);
		addInvertMenuItem(m);
		m.displayMenu(player);
		return true;
	}

}

@@ -69,16 +69,59 @@
 l.Map;%0A%0A
+import au.com.mineauz.minigames.Minigames;%0A
 import o
@@ -1589,22 +1589,24 @@
 g)%7B%0A
-        return
+%09%09int timeLeft =
  mg.
@@ -1641,10 +1641,20 @@
 ft()
+;%0A%09%09int min
  
-%3E
 = mi
@@ -1672,84 +1672,371 @@
 ag()
- &&%0A                mg.getMinigameTimer().getTimeLeft() %3C= maxTime.getFlag()
+;%0A%09%09int max = maxTime.getFlag();%0A%09%09if(Minigames.plugin.isDebugging())%7B%0A%09%09%09Minigames.plugin.getLogger().info(%22Timer Condition Check: %22 +%0A%09%09%09%09%09mg.getName(false) +%22 mech: %22 + mg.getMechanicName() +%0A%09%09%09%09%09%22 maxTime: %22 maxTime.getFlag() + %22minTime: %22 + minTime.getFlag() + %22 CurrTime: %22 +timeLeft);%0A%0A%09%09%7D%0A        return timeLeft %3E= min &&%0A                timeLeft %3C= max
 ;%0A  

Fix Timer tick