package cn.citycraft.AuthMe.process.logout;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.AuthMe.ConsoleLogger;
import cn.citycraft.AuthMe.cache.limbo.LimboCache;
import cn.citycraft.AuthMe.settings.Messages;
import cn.citycraft.AuthMe.settings.Settings;
import cn.citycraft.AuthMe.task.MessageTask;
import cn.citycraft.AuthMe.task.TimeoutTask;
import fr.xephi.authme.events.LogoutEvent;

public class ProcessSyncronousPlayerLogout implements Runnable {

	private Messages m = Messages.getInstance();
	protected String name;
	protected Player player;
	protected AuthMe plugin;

	public ProcessSyncronousPlayerLogout(Player player, AuthMe plugin) {
		this.player = player;
		this.plugin = plugin;
		this.name = player.getName().toLowerCase();
	}

	@Override
	public void run() {
		if (plugin.sessions.containsKey(name))
			plugin.sessions.get(name).cancel();
		plugin.sessions.remove(name);
		int delay = Settings.getRegistrationTimeout * 20;
		int interval = Settings.getWarnMessageInterval;
		BukkitScheduler sched = player.getServer().getScheduler();
		if (delay != 0) {
			BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), delay);
			LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id);
		}
		BukkitTask msgT = sched.runTaskAsynchronously(plugin, new MessageTask(plugin, name, m.send("login_msg"), interval));
		LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(msgT);
		if (player.isInsideVehicle() && player.getVehicle() != null)
			player.getVehicle().eject();
		if (Settings.applyBlindEffect)
			player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Settings.getRegistrationTimeout * 20, 2));
		player.setOp(false);
		if (!Settings.isMovementAllowed) {
			player.setAllowFlight(true);
			player.setFlying(true);
			if (!Settings.isMovementAllowed && Settings.isRemoveSpeedEnabled) {
				player.setFlySpeed(0.0f);
				player.setWalkSpeed(0.0f);
			}
		}
		// Player is now logout... Time to fire event !
		Bukkit.getServer().getPluginManager().callEvent(new LogoutEvent(player));
		m.send(player, "logout");
		ConsoleLogger.info(player.getName() + " logged out");
	}

}
