package cn.citycraft.AuthMe.task;

import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.AuthMe.Utils;
import cn.citycraft.AuthMe.cache.auth.PlayerCache;
import cn.citycraft.AuthMe.cache.limbo.LimboCache;

public class MessageTask implements Runnable {

	private int interval;
	private String[] msg;
	private String name;
	private AuthMe plugin;

	public MessageTask(AuthMe plugin, String name, String[] strings, int interval) {
		this.plugin = plugin;
		this.name = name;
		this.msg = strings;
		this.interval = interval;
	}

	@Override
	public void run() {
		if (PlayerCache.getInstance().isAuthenticated(name))
			return;

		for (Player player : Utils.getOnlinePlayers()) {
			if (player.getName().toLowerCase().equals(name)) {
				for (String ms : msg) {
					player.sendMessage(ms);
				}
				BukkitTask late = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, this, interval * 20);
				if (LimboCache.getInstance().hasLimboPlayer(name)) {
					LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(late);
				}
				return;
			}
		}
	}
}
