package net.mc_pandacraft.java.plugin.pandacraftutils.afk;

import net.mc_pandacraft.java.plugin.pandacraftutils.plugin_interface.EssentialsInterface;

import org.bukkit.entity.Player;

import com.earth2me.essentials.Essentials;

public class AfkPlayer {
	private Player player;
	private long timeLastAction;
	private boolean afk;
	
	
	public AfkPlayer(Player p)
	{
		player = p;
		afk = false;
		timeLastAction = System.currentTimeMillis();
	}
	
	
	public Player getPlayer() { return player; }
	
	public synchronized boolean isAfk() { return afk; }
	
	public synchronized void setAfk(boolean a)
	{
		if (afk == a)
			return;
		afk = a;
		String message;
		
		// on affiche pas le status AFK des joueurs vanish
		if (EssentialsInterface.isPlayerVanished(player)) return;
		
		// affichage du message
		if (afk)
			message = player.getDisplayName()+"§r est désormais AFK.";
		else
			message = player.getDisplayName()+"§r n'est plus AFK.";
		player.getServer().broadcastMessage(message);
	}
	
	public void performAfkCommand()
	{
		
		
		setAfk(!isAfk());
		if (!isAfk())
			isDoingAction();
	}
	
	public void adminToggleAfk()
	{
		setAfk(!isAfk());
		if (!isAfk())
			isDoingAction();
	}
	
	
	
	public double getDurationSinceLastAction()
	{
		return (System.currentTimeMillis() - timeLastAction)/1000D;
	}
	
	public void isDoingAction()
	{
		timeLastAction = System.currentTimeMillis();
		setAfk(false);
	}
}
