package mezz.jei.gui.input.handlers;

import com.mojang.blaze3d.platform.InputConstants;
import mezz.jei.api.gui.handlers.IGuiProperties;
import mezz.jei.common.input.IInternalKeyMappings;
import mezz.jei.gui.input.IMouseOverable;
import mezz.jei.gui.input.IUserInputHandler;
import mezz.jei.gui.input.UserInput;
import net.minecraft.client.gui.screens.Screen;

import java.util.Optional;

public class SameElementInputHandler implements IUserInputHandler {
	private final IUserInputHandler handler;
	private final IMouseOverable mouseOverable;

	public SameElementInputHandler(IUserInputHandler handler, IMouseOverable mouseOverable) {
		this.handler = handler;
		this.mouseOverable = mouseOverable;
	}

	@Override
	public Optional<IUserInputHandler> handleUserInput(Screen screen, IGuiProperties guiProperties, UserInput input, IInternalKeyMappings keyBindings) {
		double mouseX = input.getMouseX();
		double mouseY = input.getMouseY();
		if (mouseOverable.isMouseOver(mouseX, mouseY)) {
			return this.handler.handleUserInput(screen, guiProperties, input, keyBindings)
				.map(handled -> this);
		}
		return Optional.empty();
	}

	@Override
	public void unfocus() {
		this.handler.unfocus();
	}

	@Override
	public Optional<IUserInputHandler> handleMouseScrolled(double mouseX, double mouseY, double scrollDeltaX, double scrollDeltaY) {
		if (mouseOverable.isMouseOver(mouseX, mouseY)) {
			return this.handler.handleMouseScrolled(mouseX, mouseY, scrollDeltaX, scrollDeltaY);
		}
		return Optional.empty();
	}

	@Override
	public Optional<IUserInputHandler> handleMouseDragged(double mouseX, double mouseY, InputConstants.Key mouseKey, double dragX, double dragY) {
		if (mouseOverable.isMouseOver(mouseX, mouseY)) {
			return this.handler.handleMouseDragged(mouseX, mouseY, mouseKey, dragX, dragY)
				.map(handled -> this);
		}
		return Optional.empty();
	}
}
