package cn.citycraft.AuthMe.settings;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

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

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.PluginHelper.config.FileConfig;

/**
 *
 * @author Xephi59
 */
public class OtherAccounts extends FileConfig {

	private static OtherAccounts others = null;

	public OtherAccounts() {
		super(AuthMe.getInstance(), "otheraccounts.yml");
		others = this;
	}

	public static OtherAccounts getInstance() {
		if (others == null) {
			others = new OtherAccounts();
		}
		return others;
	}

	public void addPlayer(final String pname) {
		try {
			final Player player = Bukkit.getPlayer(pname);
			if (player == null) {
				return;
			}
			if (!this.getStringList(pname).contains(player.getName())) {
				this.getStringList(pname).add(player.getName());
				save();
			}
		} catch (NoSuchMethodError | Exception e) {
		}
	}

	public void clear(final String pname) {
		set(pname, new ArrayList<String>());
		save();
	}

	public List<String> getAllPlayersByUUID(final UUID uuid) {
		return this.getStringList(uuid.toString());
	}

	public void removePlayer(final String pname) {
		try {
			final Player player = Bukkit.getPlayer(pname);
			if (player == null) {
				return;
			}
			if (this.getStringList(pname).contains(player.getName())) {
				this.getStringList(pname).remove(player.getName());
				save();
			}
		} catch (NoSuchMethodError | Exception e) {
		}
	}
}
