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

import speiger.src.coreengine.math.vector.doubles.Vec2d;
import speiger.src.coreengine.math.vector.ints.Vec2i;
import speiger.src.coreengine.rendering.inputOld.Mouse;
import speiger.src.coreengine.rendering.inputOld.events.MouseEvent;

public class ScaledResolution
{
	Window window;
	int scaledWidth;
	int scaledHeight;
	int scale;
	
	public ScaledResolution(Window wind) {
		window = wind;
		updateScale();
	}
	
	void updateScale() {
		scale = (Math.min(1 + calculateScreenScale(), window.uiScale == 0 ? 99 : window.uiScale));
		scaledWidth = window.getWidth() / scale;
		scaledHeight = window.getHeight() / scale;
	}
	
	public int calculateScreenScale() { return calculateMaxScales(window.getWidth(), window.getHeight()); }
	public int calculateMaxScales(int width, int height) { return Math.max(0, Math.min((int)(width / 320F), (int)(height / 240F)) - 1); }
	public int getScaledWidth() { return scaledWidth; }
	public int getScaledHeight() { return scaledHeight; }
	public double getCurrentScale() { return scale; }
	
	public Vec2i getScaledMouse() {
		return !Mouse.isActive() || scaledWidth == 0 || scaledHeight == 0 ? Vec2i.MINUS_ONE : Vec2i.of(Mouse.getX() * scaledWidth / window.getWidth(), Mouse.getY() * scaledHeight / window.getHeight());
	}
	
	public MouseEvent getScaledMouseEvent(MouseEvent evt) {
		if(window.getWidth() != 0 && window.getHeight() != 0) {
			evt.setX(evt.getOriginX() * scaledWidth / window.getWidth());
			evt.setY(evt.getOriginY() * scaledHeight / window.getHeight());
		}
		return evt;
	}
	
	public Vec2i getReverseScale(int xPos, int yPos) {
		return Vec2i.of(xPos * window.getWidth() / scaledWidth, yPos * window.getHeight() / scaledHeight);
	}
	
	public Vec2d getScaleVec() {
		return Vec2d.of((double)window.getWidth() / scaledWidth, (double)window.getHeight() / scaledHeight);
	}
}
