package speiger.src.coreengine.platform.input.window;

import java.util.Objects;

import org.lwjgl.glfw.GLFW;

import speiger.src.collections.longs.lists.LongArrayList;
import speiger.src.collections.longs.lists.LongList;
import speiger.src.coreengine.math.bits.BitUtil;

public class WindowBuilder {
	WindowManager manager;
	long monitor;
	LongList windowHints = new LongArrayList();
	VideoMode fullScreenTarget;
	String title = "";
	int width = 640;
	int height = 480;
	int antiAlis = 4;
	boolean vsync = true;
	boolean fpsCap;
	boolean fullScreen;
	boolean maximized;
	boolean resizable = true;
	boolean floating;
	boolean borderless;
	boolean center = true;
	
	WindowBuilder(WindowManager manager) {
		this.manager = manager;
		monitor = GLFW.glfwGetPrimaryMonitor();
	}

	public WindowBuilder title(String title) {
		this.title = Objects.requireNonNull(title);
		return this;
	}
	
	public WindowBuilder width(int width) {
		this.width = Math.max(1, width);
		return this;
	}
	
	public WindowBuilder height(int height) {
		this.height = Math.max(1, height);
		return this;
	}
	
	public WindowBuilder antialis(int antiAlis) {
		this.antiAlis = Math.max(1, antiAlis);
		return this;
	}
	
	public WindowBuilder fullscreen(boolean fullScreen) {
		this.fullScreen = fullScreen;
		borderless &= !fullScreen;
		floating &= !fullScreen;
		maximized &= !fullScreen;
		center &= !fullScreen;
		return this;
	}
	
	public WindowBuilder maximized(boolean maximized) {
		this.maximized = maximized;
		this.fullScreen &= !maximized;
		center &= !maximized;
		return this;
	}
	
	public WindowBuilder borderless(boolean borderless) {
		this.borderless = borderless;
		fullScreen &= !borderless;
		return this;
	}
	
	public WindowBuilder floating(boolean floating) {
		this.floating = floating;
		fullScreen &= !floating;
		return this;
	}
	
	public WindowBuilder resizeable(boolean resizable) {
		this.resizable = resizable;
		return this;
	}
	
	public WindowBuilder centered(boolean center) {
		this.center = center;
		this.maximized &= !center;
		this.fullScreen &= !center;
		return this;
	}
	
	public WindowBuilder monitor(Monitor monitor) {
		if(monitor == null || monitor.defaultMode() == null) return this;
		this.monitor = monitor.id();
		return this;
	}
	
	public WindowBuilder fullScreenMode(VideoMode mode) {
		this.fullScreenTarget = mode;
		return this;
	}
	
	public WindowBuilder vsync(boolean value) {
		vsync = value;
		return this;
	}
	
	public WindowBuilder fpsCap(boolean cap) {
		fpsCap = cap;
		return this;
	}
	
	public WindowBuilder addCustomHint(int key, int value) {
		windowHints.add(BitUtil.toLong(key, value));
		return this;
	}
			
	public Window build() {
		return manager.create(this);
	}
	
}