package cc.lukatown.icefox.plugincore;

import net.milkbowl.vault.economy.Economy;
import org.anjocaido.groupmanager.GroupManager;
import org.bukkit.ChatColor;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * *******************************************
 * Created by MXD on 14-8-13.
 * for 2dmmc
 * E-mail : i@mxd.moe
 * ********************************************
 */
public class PluginCore extends JavaPlugin {

    public static PluginCore plugin;
    public static String PluginPrefix = ChatColor.GOLD + "[2dmmc Plugin Core]" + ChatColor.GRAY;
    private PluginManager pluginManager;

    public static Economy economy = null;
    public static GroupManager groupManager;

    public void onEnable() {
        plugin = this;
        pluginManager = this.getServer().getPluginManager();

        getCommand("plugincore").setExecutor(new PluginCoreCommandExecutor());
        getServer().getPluginManager().registerEvents(new PluginCoreListener(this), this);


        SendColorLog(PluginPrefix + ChatColor.YELLOW + "Initialize Vault Hooked ...");
        if (!SetupEconomy()) {
            SendColorLog(PluginPrefix + ChatColor.RED + "Vault error. disabled");
            pluginManager.disablePlugin(this);
            return;
        } else {
            SendColorLog(PluginPrefix + ChatColor.GREEN + "Vault Hooked !");
        }

        SendColorLog(PluginPrefix + ChatColor.YELLOW + "Initialize GroupManager Hooked ...");
        if (!SetupGroupManager()) {
            SendColorLog(PluginPrefix + ChatColor.RED + "GroupManager error. disabled");
            pluginManager.disablePlugin(this);
            return;
        } else {
            SendColorLog(PluginPrefix + ChatColor.GREEN + "GroupManager Hooked !");
        }

        SendColorLog(PluginPrefix + ChatColor.GREEN + "is ready to work !");
    }

    public void onDisable() {
        SendColorLog(PluginPrefix + ChatColor.YELLOW + "was disabled");
    }

    public static void SendColorLog(String Log) {
        ConsoleCommandSender sender = plugin.getServer().getConsoleSender();
        sender.sendMessage(Log);
    }

    private boolean SetupEconomy() {
        if (pluginManager.getPlugin("Vault") == null) {
            return false;
        } else {
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            } else {
                economy = rsp.getProvider();
                return economy != null;
            }
        }
    }

    private boolean SetupGroupManager() {
        if (pluginManager.getPlugin("GroupManager") == null && !pluginManager.getPlugin("GroupManager").isEnabled()) {
            return false;
        } else {
            groupManager = (GroupManager) getServer().getPluginManager().getPlugin("GroupManager");
            return groupManager != null;
        }
    }
}
