package net.mc_pandacraft.java.plugin.pandacraftutils.config.elements;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;

import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer;

public class AutoMessagesConfigEntry implements Comparable<AutoMessagesConfigEntry> {
	private final int order;
	private final String message;
	private final List<String> permissions;
	
	public AutoMessagesConfigEntry(int o, String perms, List<String> m) {
		if (m == null) throw new IllegalArgumentException("message ne doit pas être null");
		message = ChatColor.translateAlternateColorCodes('&', StringUtils.join(m, '\n'));
		permissions = ConfigManager.splitPermissionsString(perms);
		order = o;
	}

	
	public String getMessage() { return message; }
	
	
	
	public boolean hasPlayerPermission(OnlinePlayer op) {
		if (op == null) throw new IllegalArgumentException("le joueur ne doit pas être null");
		if (permissions == null)
			return true;
		
		for (String pe : permissions) {
			if (op.hasPermission(pe)) return true;
		}
		return false;
	}


	
	
	@Override
	public int compareTo(AutoMessagesConfigEntry o) {
		if (o == null) throw new NullPointerException();
		
		if (order < o.order) return -1;
		if (order > o.order) return 1;
		
		
		return 0;
	}
}
