package invtweaks.integration;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import net.minecraftforge.fml.common.Loader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Checks if the NEI or JEI item lists are visible on the screen.
 * Used to make sure InvTweaks buttons do not overlap with the item list.
 */
public class ItemListChecker {
	@Nullable
    private Method neiHidden;
	@Nullable
    private Method jeiShown;

	private boolean visible = false;
	private boolean wasVisible = false;

	public ItemListChecker() {
		this.neiHidden = getNeiHidden();
		this.jeiShown = getJeiShown();
	}

	public boolean isVisible() {
		wasVisible = visible;
		visible = isNeiVisible() || isJeiVisible();
		return visible;
	}

	public boolean wasVisible() {
		return wasVisible;
	}

	private static Method getNeiHidden() {
		if (Loader.isModLoaded("NotEnoughItems")) {
			try {
				Class<?> clientConfig = Class.forName("codechicken.nei.NEIClientConfig");
				if (clientConfig != null) {
					return clientConfig.getMethod("isHidden");
				}
			} catch (@NotNull ClassNotFoundException | NoSuchMethodException | SecurityException ignored) {
				return null;
			}
		}
		return null;
	}

	private static Method getJeiShown() {
		if (Loader.isModLoaded("JEI")) {
			try {
				Class<?> clientConfig = Class.forName("mezz.jei.config.Config");
				if (clientConfig != null) {
					return clientConfig.getMethod("isOverlayEnabled");
				}
			} catch (@NotNull ClassNotFoundException | NoSuchMethodException | SecurityException ignored) {
				return null;
			}
		}
		return null;
	}

	private boolean isNeiVisible() {
		if (neiHidden != null) {
			try {
				return !(Boolean) neiHidden.invoke(null);
			} catch (@NotNull IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				neiHidden = null;
				return false;
			}
		}
		return false;
	}

	private boolean isJeiVisible() {
		if (jeiShown != null) {
			try {
				return (Boolean) jeiShown.invoke(null);
			} catch (@NotNull IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				jeiShown = null;
				return false;
			}
		}
		return false;
	}
}

@@ -923,22 +923,11 @@
 ed(%22
-NotEnoughItems
+nei
 %22)) 
@@ -1310,19 +1310,19 @@
 Loaded(%22
-JEI
+jei
 %22)) %7B%0A%09%09

Fix NEI/JEI modids for 1.11.