package au.com.mineauz.minigames.minigame.reward;

import java.util.Map;

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

import au.com.mineauz.minigames.MinigamePlayer;
import au.com.mineauz.minigames.config.Flag;
import au.com.mineauz.minigames.menu.Callback;
import au.com.mineauz.minigames.menu.InteractionInterface;
import au.com.mineauz.minigames.menu.Menu;
import au.com.mineauz.minigames.menu.MenuItemBack;
import au.com.mineauz.minigames.menu.MenuItemCustom;
import au.com.mineauz.minigames.minigame.Minigame;
import au.com.mineauz.minigames.minigame.modules.MinigameModule;
import au.com.mineauz.minigames.minigame.reward.scheme.RewardScheme;
import au.com.mineauz.minigames.minigame.reward.scheme.RewardSchemes;
import au.com.mineauz.minigames.minigame.reward.scheme.StandardRewardScheme;
import au.com.mineauz.minigames.stats.StoredGameStats;

public class RewardsModule extends MinigameModule {
	public static final String Name = "rewards";
	private RewardScheme scheme;
	
	public RewardsModule(Minigame minigame) {
		super(minigame);
		
		// Default scheme
		scheme = new StandardRewardScheme();
	}

	@Override
	public String getName() {
		return Name;
	}
	
	public RewardScheme getScheme() {
		return scheme;
	}
	
	public void setRewardScheme(RewardScheme scheme) {
		this.scheme = scheme;
	}
	
	public void awardPlayer(MinigamePlayer player, StoredGameStats data, Minigame minigame, boolean firstCompletion) {
		scheme.awardPlayer(player, data, minigame, firstCompletion);
	}

	@Override
	public Map<String, Flag<?>> getFlags() {
		return scheme.getFlags();
	}

	@Override
	public boolean useSeparateConfig() {
		return false;
	}

	@Override
	public void save(FileConfiguration config) {
		String name = RewardSchemes.getName(scheme.getClass());
		
		ConfigurationSection root = config.getConfigurationSection(getMinigame().getName(false));
		root.set("reward-scheme", name);
		
		ConfigurationSection rewards = root.createSection("rewards");
		scheme.save(rewards);
	}

	@Override
	public void load(FileConfiguration config) {
		ConfigurationSection root = config.getConfigurationSection(getMinigame().getName(false));
		String name = root.getString("reward-scheme", "standard");
		
		scheme = RewardSchemes.createScheme(name);
		if (scheme == null) {
			scheme = new StandardRewardScheme();
		}
		
		ConfigurationSection rewards = root.getConfigurationSection("rewards");
		scheme.load(rewards);
	}

	@Override
	public void addEditMenuOptions(final Menu menu) {
		MenuItemCustom launcher = new MenuItemCustom("Reward Settings", Material.DIAMOND);
		launcher.setClick(new InteractionInterface() {
			@Override
			public Object interact(Object object) {
				Menu submenu = createSubMenu(menu);
				submenu.displayMenu(menu.getViewer());
				return null;
			}
		});
		
		menu.addItem(launcher);
	}
	
	private Menu createSubMenu(final Menu parent) {
		final Menu submenu = new Menu(6, "Reward Settings", parent.getViewer());
		scheme.addMenuItems(submenu);
		
		submenu.addItem(RewardSchemes.newMenuItem("Reward Scheme", Material.PAPER, new Callback<Class<? extends RewardScheme>>() {
			@Override
			public void setValue(Class<? extends RewardScheme> value) {
				scheme = RewardSchemes.createScheme(value);
				// Update the menu
				Menu menu = createSubMenu(parent);
				menu.displayMenu(submenu.getViewer());
			}
			
			@Override
			public Class<? extends RewardScheme> getValue() {
				return scheme.getClass();
			}
		}), submenu.getSize() - 1);
		
		submenu.addItem(new MenuItemBack(parent), submenu.getSize() - 9);
		return submenu;
	}

	@Override
	public boolean displayMechanicSettings(Menu previous) {
		// Not used
		return false;
	}
	
	public static RewardsModule getModule(Minigame minigame) {
		return (RewardsModule)minigame.getModule(Name);
	}
}

@@ -55,507 +55,132 @@
 ort 
-java.util.Map;%0A%0Aimport org.bukkit.Material;%0Aimport org.bukkit.configuration.ConfigurationSection;%0Aimport org.bukkit.configuration.file.FileConfiguration;%0A%0Aimport au.com.mineauz.minigames.MinigamePlayer;%0Aimport au.com.mineauz.minigames.config.Flag;%0Aimport au.com.mineauz.minigames.menu.Callback;%0Aimport au.com.mineauz.minigames.menu.InteractionInterface;%0Aimport au.com.mineauz.minigames.menu.Menu;%0Aimport au.com.mineauz.minigames.menu.MenuItemBack;%0Aimport au.com.mineauz.minigames.menu.MenuItemCustom
+au.com.mineauz.minigames.MinigamePlayer;%0Aimport au.com.mineauz.minigames.config.Flag;%0Aimport au.com.mineauz.minigames.menu.*
 ;%0Aim
@@ -563,16 +563,177 @@
 meStats;
+%0Aimport org.bukkit.Material;%0Aimport org.bukkit.configuration.ConfigurationSection;%0Aimport org.bukkit.configuration.file.FileConfiguration;%0A%0Aimport java.util.Map;
 %0A%0Apublic
@@ -2006,16 +2006,39 @@
 alse));%0A
+%09%09if (root != null) %7B%0A%09
 %09%09String
@@ -2092,17 +2092,19 @@
 rd%22);%0A%09%09
-%0A
+%09%0A%09
 %09%09scheme
@@ -2138,24 +2138,25 @@
 me(name);%0A%09%09
+%09
 if (scheme =
@@ -2165,16 +2165,17 @@
 null) %7B%0A
+%09
 %09%09%09schem
@@ -2212,13 +2212,16 @@
 ;%0A%09%09
+%09
 %7D%0A%09%09
-%0A
+%09%0A%09
 %09%09Co
@@ -2288,24 +2288,25 @@
 ewards%22);%0A%09%09
+%09
 scheme.load(
@@ -2307,32 +2307,36 @@
 .load(rewards);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Override%0A%09

Fix Rewards not having config exceptipn