package com.teckcoder.crashengine.file;

import com.teckcoder.crashengine.utils.logger.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class FileStream {

	File file = null;
	FileLocationType locationType = null;
	
	private FileStream(String path, FileLocationType locationType){
		this.locationType = locationType;
		file = new File(path);
	}

	public static FileStream loadInternalFile(String path){
		FileStream file = new FileStream(path, FileLocationType.INTERNAL);
		
		return file;
	}
	public static FileStream loadExternalFile(String path){
		FileStream file = new FileStream(path, FileLocationType.EXTERNAL);
		
		return file;
	}
	
	public InputStream getInputStream() {
		if(locationType == FileLocationType.EXTERNAL)
			try {
				return new FileInputStream(file.getPath().replace("\\", "/"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				
				Logger.logError("file missing", "Le fichier n'existe pas!");
			}
		else if(locationType == FileLocationType.INTERNAL)
			return FileStream.class.getResourceAsStream("/"+file.getPath().replace("\\", "/"));
		
		Logger.logError("file location", "FileLocationType erreur");
		
		return null;
	}
	
	public File getFile(){
		return file;
	}
	
	public FileLocationType getFileLocationType(){
		return locationType;
	}
}
