package me.loganb1max.minionsplus;

import com.earth2me.essentials.Essentials;
import lombok.Getter;
import me.loganb1max.minionsplus.command.MinionCommand;
import me.loganb1max.minionsplus.manager.LinkingManager;
import me.loganb1max.minionsplus.manager.MinionManager;
import me.loganb1max.minionsplus.model.Fuel;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Set;
import java.util.stream.Collectors;

public class MinionsPlus extends JavaPlugin {

	@Getter private static MinionsPlus instance;
	@Getter private static Economy econ;
	@Getter private static Essentials essentials;
	@Getter private MinionManager minionManager;
	@Getter private LinkingManager linkingManager;
	private MinionCommand minionCommand;

	@Override
	public void onEnable() {
		instance = this;
		saveDefaultConfig();
		setupEconomy();
		essentials = getPlugin(Essentials.class);
		this.minionManager = new MinionManager(this);
		this.linkingManager = new LinkingManager(this);
		this.minionCommand = new MinionCommand();
		getCommand("minions").setExecutor(minionCommand);
		getCommand("minions").setTabCompleter(minionCommand);
	}

	@Override
	public void onDisable() {
		this.minionManager.close();
		this.linkingManager.close();
	}

	private boolean setupEconomy() {
		if (getServer().getPluginManager().getPlugin("Vault") == null) {
			return false;
		}
		RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
		if (rsp == null) {
			return false;
		}
		econ = rsp.getProvider();
		return econ != null;
	}

	public Set<Fuel> getFuels() {
		return getConfig()
				.getConfigurationSection("Fuel")
				.getKeys(false)
				.stream()
				.map(s -> Fuel.fromConfigSection(getConfig().getConfigurationSection("Fuel." + s)))
				.collect(Collectors.toSet());
	}

}
