package speiger.src.coreengine.rendering.inputOld.bindings;

import java.util.Map;

import org.lwjgl.glfw.GLFW;

import speiger.src.collections.objects.maps.interfaces.Object2ObjectMap;
import speiger.src.coreengine.rendering.inputOld.Mouse;
import speiger.src.coreengine.rendering.inputOld.bindings.utils.BindingType;

public class Axis
{
	static final Map<String, Axis> AXIS_MAP = Object2ObjectMap.builder().linkedMap();
	public static final IAxisSource MAIN_SCROLL_WEEL = new ScrollAxis(false);
	public static final IAxisSource SECOND_SCROLL_WEEL = new ScrollAxis(true);
	public static final Axis XMOVEMENT = new Axis("XMovement", BindingType.KEYBOARD, GLFW.GLFW_KEY_S, GLFW.GLFW_KEY_W);
	public static final Axis STRAVE = new Axis("Strave", BindingType.KEYBOARD, GLFW.GLFW_KEY_D, GLFW.GLFW_KEY_A);
	public static final Axis YAW = new Axis("Yaw", BindingType.KEYBOARD, GLFW.GLFW_KEY_E, GLFW.GLFW_KEY_Q);
	public static final Axis PITCH = new Axis("Pitch", BindingType.KEYBOARD, GLFW.GLFW_KEY_X, GLFW.GLFW_KEY_C);
	public static final Axis ZOOM = new Axis("Zoom", MAIN_SCROLL_WEEL);
	
	String name;
	IAxisSource source;
	
	public Axis(String name) {
		this(name, null);
	}
	
	public Axis(String name, BindingType type, int pos, int neg) {
		this(name, new KeySource(type, neg, pos));
	}
	
	public Axis(String id, IAxisSource provider) {
		name = id;
		source = provider;
		AXIS_MAP.put(id, this);
	}
	
	public void setSource(BindingType type, int pos, int neg) {
		setSource(new KeySource(type, neg, pos));
	}
	
	public void setSource(IAxisSource provider) {
		source = provider;
	}
	
	public static Axis getAxis(String name) {
		return AXIS_MAP.get(name);
	}
	
	public IAxisSource getSource() {
		return source;
	}
	
	public float getValue() {
		return source != null ? source.getValue() : 0F;
	}
	
	public String getName() {
		return name;
	}
	
	public static interface IAxisSource {
		public float getValue();
		public boolean isButton();
		public String getButtonName(boolean first);
		public String getAxisName();
		public boolean isAlive();
	}
	
	public static class ScrollAxis implements IAxisSource
	{
		boolean first;
		
		public ScrollAxis(boolean data) {
			first = data;
		}
		
		@Override
		public float getValue() { return first ? Mouse.INSTANCE.getScroll().x() : Mouse.INSTANCE.getScroll().y(); }
		@Override
		public boolean isAlive() { return true; }
		@Override
		public boolean isButton() { return false; }
		@Override
		public String getButtonName(boolean first) { return "Not Present"; }
		@Override
		public String getAxisName() { return "ScrollWeel "+(first ? 0 : 1); }
	}
	
	public static class KeySource implements IAxisSource {
		BindingType type;
		int neg;
		int pos;
		
		public KeySource(BindingType type, int neg, int pos) {
			this.type = type;
			this.neg = neg;
			this.pos = pos;
		}
		
		@Override
		public float getValue() { return (type.isPressed(pos) ? 1F : 0F) - (type.isPressed(neg) ? 1F : 0F); }
		@Override
		public boolean isButton() { return true; }
		@Override
		public boolean isAlive() { return true; }
		@Override
		public String getButtonName(boolean first) { return type.getName(first ? pos : neg); }
		@Override
		public String getAxisName() { return "Not Present"; }
	}
}
