package ru.tehkode.permissions.bukkit;

import org.apache.commons.lang.ObjectUtils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import ru.tehkode.permissions.EntityType;
import ru.tehkode.permissions.data.MatcherGroup;
import ru.tehkode.permissions.data.Qualifier;
import ru.tehkode.permissions.events.MatcherGroupEvent;
import ru.tehkode.permissions.events.PermissionEntityEvent;

/**
 * Event transformer that will
 */
public class EventConversionListener implements Listener {
	private final PermissionsEx plugin;

	public EventConversionListener(PermissionsEx plugin) {
		this.plugin = plugin;
	}
	/*
		TIMEDPERMISSION_EXPIRED,
		RANK_CHANGED,
		DEFAULTGROUP_CHANGED,
		WEIGHT_CHANGED,
	 */
	/**
	 *
	 * @param event
	 */
	@EventHandler
	@SuppressWarnings("deprecation")
	public void handleMatcherGroup(final MatcherGroupEvent event) {
		if (PermissionEntityEvent.getHandlerList().getRegisteredListeners().length == 0) { // No listeners, don't pass on the event
			return;
		}

		PermissionEntityEvent.Action legacyEvent = null;
		final EntityType type;
		final String identifier;
		final MatcherGroup activeGroup = getActiveGroup(event);
		if (activeGroup.getQualifiers().containsKey(Qualifier.USER)) {
			type = EntityType.USER;
			identifier = activeGroup.getQualifiers().get(Qualifier.USER).iterator().next();
		} else if (activeGroup.getQualifiers().containsKey(Qualifier.GROUP)) {
			type = EntityType.GROUP;
			identifier = activeGroup.getQualifiers().get(Qualifier.GROUP).iterator().next();
		} else {
			return;
		}

		switch (event.getAction()) {
			case REMOVE:
				legacyEvent = PermissionEntityEvent.Action.REMOVED;
				break;
			case CREATE:
			case CHANGE_ENTRIES:
				if (event.getNewGroup().getEntries().isEmpty()) {
					legacyEvent = PermissionEntityEvent.Action.SAVED;
				} else {
					switch (event.getNewGroup().getName()) {
						case MatcherGroup.INHERITANCE_KEY:
							legacyEvent = PermissionEntityEvent.Action.INHERITANCE_CHANGED;
							break;
						case MatcherGroup.OPTIONS_KEY:
							legacyEvent = PermissionEntityEvent.Action.OPTIONS_CHANGED;
							if (difference(event, "prefix") || difference(event, "suffix")) {
								legacyEvent = PermissionEntityEvent.Action.INFO_CHANGED;
							}
							break;
						case MatcherGroup.PERMISSIONS_KEY:
							legacyEvent = PermissionEntityEvent.Action.PERMISSIONS_CHANGED;
							break;
					}
				}
				break;
			case CHANGE_QUALIFIERS:
				break;
		}

		if (legacyEvent != null) {
			if (event.isAsynchronous()) {
				final PermissionEntityEvent.Action finalLegacyEvent = legacyEvent;
				plugin.getServer().getScheduler().runTask(plugin, new Runnable() {
					@Override
					public void run() {
						plugin.getServer().getPluginManager().callEvent(new PermissionEntityEvent(event.getSourceUUID(), identifier, type, finalLegacyEvent));
					}
				});
			} else {
				plugin.getServer().getPluginManager().callEvent(new PermissionEntityEvent(event.getSourceUUID(), identifier, type, legacyEvent));
			}
		}
	}

	private MatcherGroup getActiveGroup(MatcherGroupEvent event) {
		return event.getNewGroup() == null ? event.getOldGroup() : event.getNewGroup();
	}

	private boolean difference(MatcherGroupEvent event, String key) {
		if (event.getOldGroup() == null) {
			return event.getNewGroup().getEntries().containsKey(key);
		} else if (event.getNewGroup() == null) {
			return event.getOldGroup().getEntries().containsKey(key);
		} else {
			return !ObjectUtils.equals(event.getOldGroup().getEntries().get(key), event.getNewGroup().getEntries().get(key));
		}
	}
}

@@ -659,54 +659,20 @@
 ,%0A%09%09
-RANK_CHANGED,%0A%09%09DEFAULTGROUP_CHANGED,%0A%09%09WEIGHT
+DEFAULTGROUP
 _CHA
@@ -2203,24 +2203,288 @@
 FO_CHANGED;%0A
+%09%09%09%09%09%09%09%7D else if (difference(event, %22rank%22) %7C%7C difference(event, %22rank-ladder%22)) %7B%0A%09%09%09%09%09%09%09%09legacyEvent = PermissionEntityEvent.Action.RANK_CHANGED;%0A%09%09%09%09%09%09%09%7D else if (difference(event, %22weight%22)) %7B%0A%09%09%09%09%09%09%09%09legacyEvent = PermissionEntityEvent.Action.WEIGHT_CHANGED;%0A
 %09%09%09%09%09%09%09%7D%0A%09%09%09

Add in event actions that were missed the first time around