package cn.citycraft.JoinMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import cn.citycraft.JoinMessage.listen.PlayerJoin;
import cn.citycraft.JoinMessage.listen.PlayerLogin;

public class Main extends JavaPlugin{
	
	List<Map<String, String[]>> perms =  new ArrayList<Map<String, String[]>>();
	
	public boolean authme; 
	
	public void onLoad() {
		this.saveDefaultConfig();
	}

	public void onEnable() {
		this.getLogger().info("登录提示语已加载...");
		PluginManager pm = Bukkit.getPluginManager();
		pm.registerEvents(new PlayerJoin(this), this);
		if (pm.isPluginEnabled("AuthMe")) {
			this.getLogger().info("Find AuthMe Hook...");
			authme = true;
			pm.registerEvents(new PlayerLogin(this), this);
		}
	}
	
	public void sendJoinMessage(Player p) {
		String pn = p.getName();
		String pdn = p.getDisplayName();
		List<Map<?, ?>> perms = this.getConfig().getMapList("Message");
		for (Map<?, ?> smp : perms) {
			for (Entry<?, ?> key : smp.entrySet()) {
				String permission = (String) key.getKey();
				if (p.hasPermission(permission)) {
					String[] msg = ((ArrayList<?>) key.getValue())
							.toArray(new String[0]);
					for (String message : msg) {
						message = message.replaceAll("%name%", pn)
								.replaceAll("%displayname%", pdn)
								.replaceAll("&", "§");
						Bukkit.broadcastMessage(message);
					}
				}
			}
		}
	}
	
}
