package io.github.mrjcowman.engine.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.ArrayList;

import org.newdawn.slick.opengl.TextureLoader;

import io.github.mrjcowman.engine.core.Util;
import io.github.mrjcowman.engine.render.Mesh;
import io.github.mrjcowman.engine.render.Texture;
import io.github.mrjcowman.engine.render.Vertex;

public class ResourceLoader {

	public static String loadShader(String fileName){
		StringBuilder shaderSource = new StringBuilder();
		BufferedReader shaderReader;
		
		try{
			shaderReader = new BufferedReader(new FileReader("./res/shaders/"+fileName));
			String line;
			while((line = shaderReader.readLine()) != null){
				shaderSource.append(line).append("\n");
			}
			
			shaderReader.close();
		}catch(Exception e){
			System.out.println("There was an error loading the shader: \""+fileName+"\"");
			e.printStackTrace();
			System.exit(1);
		}
		
		
		return shaderSource.toString();
	}
	
	public static Mesh loadMesh(String fileName){
		String[] splitArray = fileName.split("\\.");
		String ext = splitArray[splitArray.length-1];
		
		if(!ext.equals("obj")){
			System.err.println("Error: File format not supported for mesh data: "+ext);
			new Exception().getStackTrace();
			System.exit(5);
		}
		
		OBJModel test = new OBJModel("./res/models/"+fileName);
		IndexedModel model = test.toIndexedModel();
		
		ArrayList<Vertex> vertices = new ArrayList<Vertex>();
		
		for(int i=0; i<model.getPos().size(); i++){
			vertices.add(new Vertex(model.getPos().get(i),
									model.getNormal().get(i),
									model.getUV().get(i)));
		}

		Mesh res = new Mesh();
		Vertex[] vertexData = new Vertex[vertices.size()];
		vertices.toArray(vertexData);
		

		Integer[] indexData = new Integer[model.getIndices().size()];
		model.getIndices().toArray(indexData);
		
		res.addVertices(vertexData, Util.toIntArray(indexData), true);

//		ArrayList<Vertex> vertices = new ArrayList<Vertex>();
//		ArrayList<Integer> indices = new ArrayList<Integer>();
//		
//		//StringBuilder shaderSource = new StringBuilder();
//		BufferedReader meshReader;
//		
//		try{
//			meshReader = new BufferedReader(new FileReader("./res/models/"+fileName));
//			String line;
//			while((line = meshReader.readLine()) != null){
//				String[] tokens = line.split(" ");
//				tokens = Util.removeEmptyStrings(tokens);
//				
//				if(tokens.length == 0 || tokens[0].equals("#"))
//					continue;
//				else if(tokens[0].equals("v")){
//					vertices.add(new Vertex(new Vector3f(Float.valueOf(tokens[1]),Float.valueOf(tokens[2]),Float.valueOf(tokens[3]) )));
//				}
//				else if(tokens[0].equals("f")){
//					if(tokens[1].split("/").length==1){
//					
//						indices.add(Integer.parseInt(tokens[1].split("/")[0])-1);
//						indices.add(Integer.parseInt(tokens[2].split("/")[0])-1);
//						indices.add(Integer.parseInt(tokens[3].split("/")[0])-1);
//						
//						if(tokens.length > 4){
//							indices.add(Integer.parseInt(tokens[1].split("/")[0])-1);
//							indices.add(Integer.parseInt(tokens[3].split("/")[0])-1);
//							indices.add(Integer.parseInt(tokens[4].split("/")[0])-1);
//						}
//					}else{
//						
//					}
//				}
//			}
//			
//			meshReader.close();
//			
//			Mesh res = new Mesh();
//			Vertex[] vertexData = new Vertex[vertices.size()];
//			vertices.toArray(vertexData);
//
//			Integer[] indexData = new Integer[indices.size()];
//			indices.toArray(indexData);
//			
//			res.addVertices(vertexData, Util.toIntArray(indexData));
//			
//			return res;
//		}catch(Exception e){
//			System.out.println("There was an error loading the mesh: \""+fileName+"\"");
//			e.printStackTrace();
//			System.exit(6);
//		}
		
		return res;
	}

	public static Texture loadTexture(String fileName){
		String[] splitArray = fileName.split("\\.");
		String ext = splitArray[splitArray.length - 1];
		
		try{
			int id = TextureLoader.getTexture(ext, new FileInputStream(new File("./res/textures/"+fileName))).getTextureID();
			return new Texture(id);
		}catch(Exception e){
			e.printStackTrace();
			System.exit(7);
		}
		
		return null;
	}
}
