package cn.citycraft.AuthMe.task;

import java.security.NoSuchAlgorithmException;

import org.bukkit.entity.Player;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.AuthMe.ConsoleLogger;
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.Messages;
import cn.citycraft.AuthMe.settings.Settings;

public class ChangePasswordTask implements Runnable {

	private final String newPassword;
	private final String oldPassword;
	private final Player player;
	private final AuthMe plugin;

	public ChangePasswordTask(AuthMe plugin, Player player, String oldPassword, String newPassword) {
		this.plugin = plugin;
		this.player = player;
		this.oldPassword = oldPassword;
		this.newPassword = newPassword;
	}

	@Override
	public void run() {
		Messages m = Messages.getInstance();
		try {
			String name = player.getName().toLowerCase();
			String hashnew = PasswordSecurity.getHash(Settings.getPasswordHash, newPassword, name);
			PlayerAuth auth = PlayerCache.getInstance().getAuth(name);
			if (PasswordSecurity.comparePasswordWithHash(oldPassword, auth.getHash(), player.getName())) {
				auth.setHash(hashnew);
				if (PasswordSecurity.userSalt.containsKey(name) && PasswordSecurity.userSalt.get(name) != null) {
					auth.setSalt(PasswordSecurity.userSalt.get(name));
				} else {
					auth.setSalt("");
				}
				if (!plugin.database.updatePassword(auth)) {
					m.send(player, "error");
					return;
				}
				plugin.database.updateSalt(auth);
				PlayerCache.getInstance().updatePlayer(auth);
				m.send(player, "pwd_changed");
				ConsoleLogger.info(player.getName() + " changed his password");
			} else {
				m.send(player, "wrong_pwd");
			}
		} catch (NoSuchAlgorithmException ex) {
			ConsoleLogger.showError(ex.getMessage());
			m.send(player, "error");
		}
	}
}
