package cn.citycraft.AuthMe;

import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import cn.citycraft.AuthMe.settings.Settings;
import net.milkbowl.vault.permission.Permission;

public class DataManager {

	public AuthMe plugin;

	public DataManager(final AuthMe plugin) {
		this.plugin = plugin;
	}

	public synchronized OfflinePlayer getOfflinePlayer(final String name) {
		final ExecutorService executor = Executors.newSingleThreadExecutor();
		final Future<OfflinePlayer> result = executor.submit(new Callable<OfflinePlayer>() {

			@Override
			public synchronized OfflinePlayer call() throws Exception {
				OfflinePlayer result = null;
				try {
					for (final OfflinePlayer op : Bukkit.getOfflinePlayers()) {
						if (op.getName().equalsIgnoreCase(name)) {
							result = op;
							break;
						}
					}
				} catch (final Exception e) {
				}
				return result;
			}
		});
		try {
			return result.get();
		} catch (final Exception e) {
			return (null);
		} finally {
			executor.shutdown();
		}
	}

	public boolean isOnline(final Player player, final String name) {
		if (player.isOnline()) {
			return true;
		}
		final ExecutorService executor = Executors.newSingleThreadExecutor();
		final Future<Boolean> result = executor.submit(new Callable<Boolean>() {
			@Override
			public synchronized Boolean call() throws Exception {
				for (final OfflinePlayer op : Utils.getOnlinePlayers()) {
					if (op.getName().equalsIgnoreCase(name)) {
						return true;
					}
				}
				return false;
			}
		});
		try {
			return result.get();
		} catch (final Exception e) {
			return false;
		} finally {
			executor.shutdown();
		}
	}

	public synchronized void purgeAntiXray(final List<String> cleared) {
		int i = 0;
		for (final String name : cleared) {
			try {
				final org.bukkit.OfflinePlayer player = getOfflinePlayer(name);
				if (player == null) {
					continue;
				}
				final String playerName = player.getName();
				final File playerFile = new File("." + File.separator + "plugins" + File.separator + "AntiXRayData" + File.separator + "PlayerData" + File.separator + playerName);
				if (playerFile.exists()) {
					playerFile.delete();
					i++;
				}
			} catch (final Exception e) {
			}
		}
		ConsoleLogger.info("自动清理数据库: 移除 " + i + " 个 AntiXRayData 文件");
	}

	public synchronized void purgeDat(final List<String> cleared) {
		int i = 0;
		for (final String name : cleared) {
			try {
				final org.bukkit.OfflinePlayer player = getOfflinePlayer(name);
				File playerFile = null;
				if (player == null) {
					continue;
				}
				try {
					playerFile = new File(plugin.getServer().getWorldContainer() + File.separator + Settings.defaultWorld + File.separator + "players" + File.separator + player.getUniqueId()
							+ ".dat");
				} catch (final Exception ignore) {
				}
				if (playerFile != null && playerFile.exists()) {
					playerFile.delete();
					i++;
				} else {
					playerFile = new File(plugin.getServer().getWorldContainer() + File.separator + Settings.defaultWorld + File.separator + "players" + File.separator + player.getName() + ".dat");
					if (playerFile.exists()) {
						playerFile.delete();
						i++;
					}
				}
			} catch (final Exception ignore) {
			}
		}
		ConsoleLogger.info("自动清理数据库: 移除 " + i + " 个 .dat 文件");
	}

	@SuppressWarnings("deprecation")
	public void purgeEssentials(final List<String> cleared) {
		int i = 0;
		for (final String name : cleared) {
			try {
				File playerFile = null;
				try {
					playerFile = new File(plugin.ess.getDataFolder() + File.separator + "userdata" + File.separator + plugin.getServer().getOfflinePlayer(name).getUniqueId() + ".yml");
				} catch (final Exception ignore) {
				}
				if (playerFile != null && playerFile.exists()) {
					playerFile.delete();
					i++;
				} else {
					playerFile = new File(plugin.ess.getDataFolder() + File.separator + "userdata" + File.separator + name + ".yml");
					if (playerFile.exists()) {
						playerFile.delete();
						i++;
					}
				}
			} catch (final Exception e) {
			}
		}
		ConsoleLogger.info("自动清理数据库: 移除 " + i + " 个 EssentialsFiles 文件");
	}

	public synchronized void purgeLimitedCreative(final List<String> cleared) {
		int i = 0;
		for (final String name : cleared) {
			try {
				final org.bukkit.OfflinePlayer player = getOfflinePlayer(name);
				if (player == null) {
					continue;
				}
				final String playerName = player.getName();
				File playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + ".yml");
				if (playerFile.exists()) {
					playerFile.delete();
					i++;
				}
				playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + "_creative.yml");
				if (playerFile.exists()) {
					playerFile.delete();
					i++;
				}
				playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + "_adventure.yml");
				if (playerFile.exists()) {
					playerFile.delete();
					i++;
				}
			} catch (final Exception e) {
			}
		}
		ConsoleLogger.info("自动清理数据库: 移除 " + i + " 个 LimitedCreative Survival, Creative and Adventure 文件");
	}

	public synchronized void purgePermissions(final List<String> cleared, final Permission permission) {
		int i = 0;
		for (final String name : cleared) {
			try {
				final OfflinePlayer p = this.getOfflinePlayer(name);
				for (final String group : permission.getPlayerGroups((Player) p)) {
					permission.playerRemoveGroup(null, p, group);
				}
				i++;
			} catch (final Exception e) {
			}
		}
		ConsoleLogger.info("自动清理数据库: 移除 " + i + " 个 Permissions 文件");
	}

	public void run() {
	}
}
