package gameEngine;

import java.util.ArrayList;

import org.joml.Vector3f;

import gameEngine.camera.Camera;
import gameEngine.items.GroupItem;
import gameEngine.light.SceneLight;
import gameEngine.skyBox.SkyBox;
import static org.lwjgl.glfw.GLFW.GLFW_FALSE;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import static org.lwjgl.opengl.GL11.*;


public class World implements Runnable{
	Camera c ;
	ArrayList<Camera> listCamera;
	Renderer renderer;
	SceneVertex sc ;
	SceneLight sl;
	Callback callback;
	ArrayList<GroupItem> listGroup;

	private SkyBox sk = null;
	
	public Camera getCamera(){
		return c;
	}
	
	public void setCamera(Camera c){
		this.c=c;
	}
	
	public ArrayList<Camera> getListCamera(){
		return listCamera;
	}
	
	public void setSkyBox(float width, float length, float height, float r,float g,float b){
		SkyBox sky=new SkyBox(width,length,height,new Vector3f(r,g,b));
		if(this.sk != null)
			removeSkyBox();
		this.sk = sky;
	}
	
	public void setSkyBox(SkyBox sk){
		if(this.sk != null)
			removeSkyBox();
		this.sk = sk;
	}
	
	public void removeSkyBox(){
		this.sk.cleanUp();
		this.sk = null;
	}
	
	public SceneVertex getSceneVertex(){
		return sc;
	}
	public ArrayList<GroupItem> getListGroup(){
		return listGroup;
	}
	public SceneLight getSceneLight(){
		return sl;
	}
	public Callback getCallback(){
		return callback;
	}
	
	public void init() throws Exception{
		this.listCamera=new ArrayList<Camera>();
		this.c= new Camera();
		this.sc= new SceneVertex();
		this.sl= new SceneLight();
		this.callback=new Callback();
		this.listGroup=new ArrayList<GroupItem>();
	}
	
	public void emptyListCamera() {
		listCamera=new ArrayList<Camera>();
	}
	
	public void setCamera(int index, Camera camera) {
		if (index == 0)
			c = camera;
		listCamera.add(index, camera);
	}
	
	public void run(){
		Window.init();
		try {
			this.renderer= new Renderer();
		} catch (Exception e1) {
			e1.printStackTrace();
			return;
		}
		
		try{
			//Initialisation of the window we currently use
			glViewport(0, 0, 500,500);
			while ( glfwWindowShouldClose(Window.window) == GLFW_FALSE ) {
				Thread.sleep(20);
				c.update();
				callback.update();
				//Clean the window
				boolean isResized = Window.clear();
				synchronized(sc){
			
				//Update the world
				renderer.render(sc, sl, isResized,c, sk);
				}
				//Thread.sleep(1000);
				//Update the window's picture
				Window.update();
			}
		}catch(Exception e){System.out.println(e.getMessage());}
		finally{
			renderer.cleanUp();
			sc.cleanUp();
			if(sk!= null)
				sk.cleanUp();
			//On desaloue le materiel de la fenetre
			Window.cleanUp();
		}
	}
}


@@ -540,16 +540,48 @@
 stGroup;
+%0A%09ArrayList%3CSkyBox%3E skyToRemove;
 %0A%0A%09priva
@@ -945,35 +945,45 @@
 != null)
+%7B
 %0A%09%09%09
-removeSkyBox();
+skyToRemove.add(sk);%0A%09%09%7D
 %0A%09%09this.
@@ -1057,29 +1057,34 @@
 ull)%0A%09%09%09
-removeSkyBox(
+skyToRemove.add(sk
 );%0A%09%09thi
@@ -1134,24 +1134,26 @@
 %7B%0A%09%09
-this.sk.cleanUp(
+skyToRemove.add(sk
 );%0A%09
@@ -1641,24 +1641,68 @@
 oupItem%3E();%0A
+%09%09this.skyToRemove=new ArrayList%3CSkyBox%3E();%0A
 %09%7D%0A%09%0A%09public
@@ -2345,16 +2345,16 @@
 c)%7B%0A%09%09%09%0A
-
 %09%09%09%09//Up
@@ -2368,16 +2368,68 @@
 e world%0A
+%09%09%09%09%09for(SkyBox s : skyToRemove)%0A%09%09%09%09%09%09s.cleanUp();%0A
 %09%09%09%09rend

Fix on skyBox