package lc.minecraft.link;

import lc.mecha.log.MechaLogger;
import lc.mecha.log.MechaLoggerFactory;
import lc.mecha.util.UniversalJob;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import javax.jms.JMSException;
import java.io.IOException;
import java.util.UUID;

/**
 * This class implements a Bukkit plugin providing a RESTful API service compatible with the
 * LEIGH&CO Enhanced Services Platform (ESP). The service may be used directly or may be placed
 * onto an ESP grid behind a Kabe server.
 * <p>
 * The purpose of the service is to provide the means to author Minecraft extensions / plugins that
 * run in a distributed fashion independently of the Bukkit server instance. This allows plugins
 * to be added, removed, restarted dynamically without interfering or restarting the Bukkit server,
 * provides insulation for the external services to be immune from server version changes, and opens
 * the opportunity for the external services to be written in any programming language.
 *
 * @author Alex Leigh
 */
public class LinkPlugin extends JavaPlugin implements Listener {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(LinkPlugin.class);
    public static final String CFG_KEY_SERVERNAME = "serverName";


    public LinkPlugin() throws IOException, JMSException {
        UniversalJob.banner(logger, "Link mk2");
    }

    public void onEnable() {
        getLogger().info("MinecraftLink mk1 Enabled");
        saveDefaultConfig();
        FileConfiguration config = getConfig();
        String serverName = config.getString(CFG_KEY_SERVERNAME);

        if (StringUtils.isEmpty(serverName)) throw new IllegalStateException("serverName must be provided.");

        try {
            getServer().getPluginManager().registerEvents(new LinkListener(serverName), this);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public void onDisable() {
        getLogger().info("MinecraftLink Disabled");
    }

    /**
     * Send a message to the given player name on the Bukkit server, if they are logged in.
     */
    public void sendMessage(String playerName, String msg) {
        new BukkitRunnable() {
            @Override
            public void run() {
                Player p = Bukkit.getPlayer(playerName);
                logger.info("Found player: {}", p);
                if (p != null) p.sendMessage(msg);
            }
        }.run();
    }

    public void sendMessage(UUID playerId, String msg) {
        new BukkitRunnable() {
            @Override
            public void run() {
                Player p = Bukkit.getPlayer(playerId);
                logger.info("Found player: {}", p);
                if (p != null) p.sendMessage(msg);
            }
        }.run();
    }
}