package fr.xephi.authme.api;

import java.security.NoSuchAlgorithmException;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.AuthMe.Utils;
import cn.citycraft.AuthMe.cache.auth.PlayerAuth;
import cn.citycraft.AuthMe.cache.auth.PlayerCache;
import cn.citycraft.AuthMe.security.PasswordSecurity;
import cn.citycraft.AuthMe.settings.Settings;

public class API {

	public static AuthMe instance;
	public static final String newline = System.getProperty("line.separator");

	@Deprecated
	public API(final AuthMe instance) {
		API.instance = instance;
	}

	/**
	 * @param String
	 *            playerName, String passwordToCheck
	 * @return true if the password is correct , false else
	 */
	@Deprecated
	public static boolean checkPassword(final String playerName, final String passwordToCheck) {
		if (!isRegistered(playerName)) {
			return false;
		}
		final String player = playerName.toLowerCase();
		final PlayerAuth auth = instance.database.getAuth(player);
		try {
			return PasswordSecurity.comparePasswordWithHash(passwordToCheck, auth.getHash(), playerName);
		} catch (final NoSuchAlgorithmException e) {
			return false;
		}
	}

	/**
	 * Force a player to login
	 *
	 * @param Player
	 *            player
	 */
	@Deprecated
	public static void forceLogin(final Player player) {
		instance.management.performLogin(player, "dontneed", true);
	}

	@Deprecated
	public static Location getLastLocation(final Player player) {
		try {
			final PlayerAuth auth = PlayerCache.getInstance().getAuth(player.getName().toLowerCase());

			if (auth != null) {
				final Location loc = new Location(Bukkit.getWorld(auth.getWorld()), auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ());
				return loc;
			} else {
				return null;
			}

		} catch (final NullPointerException ex) {
			return null;
		}
	}

	/**
	 * Hook into AuthMe
	 *
	 * @return AuthMe instance
	 */
	@Deprecated
	public static AuthMe hookAuthMe() {
		if (instance != null) {
			return instance;
		}
		final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("AuthMe");
		if (plugin == null || !(plugin instanceof AuthMe)) {
			return null;
		}
		instance = (AuthMe) plugin;
		return instance;
	}

	/**
	 *
	 * @param player
	 * @return true if player is authenticate
	 */
	@Deprecated
	public static boolean isAuthenticated(final Player player) {
		return PlayerCache.getInstance().isAuthenticated(player.getName());
	}

	/**
	 *
	 * @param playerName
	 * @return true if player is registered
	 */
	@Deprecated
	public static boolean isRegistered(final String playerName) {
		final String player = playerName.toLowerCase();
		return instance.database.isAuthAvailable(player);
	}

	/**
	 *
	 * @param player
	 * @return true if the player is unrestricted
	 */
	@Deprecated
	public static boolean isUnrestricted(final Player player) {
		return Utils.isUnrestricted(player);
	}

	/**
	 * Register a player
	 *
	 * @param String
	 *            playerName, String password
	 * @return true if the player is register correctly
	 */
	@Deprecated
	public static boolean registerPlayer(final String playerName, final String password) {
		try {
			final String name = playerName.toLowerCase();
			final String hash = PasswordSecurity.getHash(Settings.getPasswordHash, password, name);
			if (isRegistered(name)) {
				return false;
			}
			final PlayerAuth auth = new PlayerAuth(name, hash, "198.18.0.1", 0, "your@email.com", playerName);
			if (!instance.database.saveAuth(auth)) {
				return false;
			}
			return true;
		} catch (final NoSuchAlgorithmException ex) {
			return false;
		}
	}

	@Deprecated
	public static void setPlayerInventory(final Player player, final ItemStack[] content, final ItemStack[] armor) {
		try {
			player.getInventory().setContents(content);
			player.getInventory().setArmorContents(armor);
		} catch (final NullPointerException npe) {
		}
	}

	@Deprecated
	public AuthMe getPlugin() {
		return instance;
	}

	/**
	 *
	 * @param player
	 * @return true if player is a npc
	 */
	@Deprecated
	public boolean isaNPC(final Player player) {
		return Utils.isNPC(player);
	}

	/**
	 *
	 * @param player
	 * @return true if player is a npc
	 */
	@Deprecated
	public boolean isNPC(final Player player) {
		return Utils.isNPC(player);
	}

}
