package utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class UtilsFile {

	
	/**
	 * Crea un fichero con el contenido de 'code' con nombre 'name' en el path 'filePath' y con la extension 'fileExtension'
	 * 
	 * @param code
	 * @param pathFile
	 * @param nameFile
	 * @param fileExtension
	 * @return
	 * @throws IOException
	 */
    public static int codeToFile(String code, String pathFile, String nameFile, String fileExtension) throws IOException{
        return codeToFile(code,pathFile,nameFile+fileExtension);
    }
    
	/**
	 * Crea un fichero con el contenido de 'code' con nombre 'name' en el path 'filePath'
	 * 
	 * @param code
	 * @param pathFile
	 * @param nameFile
	 * @return
	 * @throws IOException
	 */
    public static int codeToFile(String code, String pathFile, String nameFile) throws IOException{
        AyudaDebug.printMetodo("codeToFile:init");
        AyudaDebug.print("where? " + pathFile+nameFile);
        
        BufferedWriter out;
        out = new BufferedWriter(new FileWriter(pathFile + nameFile));
        out.write(code);
        out.close();
        
        AyudaDebug.printMetodo("codeToFile:end");
        
        return 0;
    }
    
	/**
	 * Crea un fichero con el contenido de 'code' con nombre 'name' en el path 'filePath'
	 * 
	 * @param code
	 * @param pathFile
	 * @param nameFile
	 * @return
	 * @throws IOException
	 */
    public static int codeToFile(byte[] file, String pathFile, String nameFile) throws IOException{
        AyudaDebug.printMetodo("codeToFile:init");
        AyudaDebug.print("where? " + pathFile+nameFile);
        
        FileOutputStream fos = new FileOutputStream(pathFile + nameFile);
        fos.write(file);
        fos.close();
        
        AyudaDebug.printMetodo("codeToFile:end");
        
        return 0;
    }
    
    /**
     * Recupera en un String la informacion del fichero 'name_file' con camino 'path_file' y extensión 'fileExtension'
     * 
     * @param pathFile
     * @param nameFile
     * @return 
     */
    public static String readFile(String pathFile, String nameFile, String fileExtension){
    	AyudaDebug.printMetodo("readFile:init");
    	
    	AyudaDebug.print("which file?" + pathFile+nameFile+fileExtension);
    	BufferedReader read = null;
	    String readed = null;

	    try {
	        read = new BufferedReader(new FileReader(pathFile+nameFile+fileExtension));
	        String line = "";
	        readed = new String();
	        while((line = read.readLine()) != null){
	            readed += line + "\n";
	        }
	        read.close();
	    }catch (java.io.FileNotFoundException ex){
		    System.out.println("NO ENCONTRADO:" + pathFile+nameFile+fileExtension);
	    } catch (IOException ex) {
	    	System.out.println(ex);
	     
	    
	    } finally {
	        try {
	        	if (read != null)
	        		read.close();
	        } catch (IOException ex) {
	            System.out.println(ex);
	        } catch (java.lang.NullPointerException ex){
	        	System.out.println(ex);
	        }
	        
	    }
	    
	    AyudaDebug.printMetodo("readFile:end");
		return readed;
    }
    
    /**
     * Método que guarda el objeto 'savedObject' en el path 'pathFile' con el nombre 'nameFile'
     * 
     * @param pathFile
     * @param nameFile
     * @param savedObject
     * @throws IOException
     */
    public static void saveObject(String pathFile, String nameFile, Object savedObject) throws IOException{
        AyudaDebug.printMetodo("saveObject:init");
        AyudaDebug.print(pathFile+nameFile);
        
        FileOutputStream fout = null;   
        ObjectOutputStream oss = null;
        
        File fichero = new File(pathFile+nameFile);
        fout = new FileOutputStream(fichero);
        
        oss = new ObjectOutputStream(fout);
        oss.writeObject(savedObject);
        
        fout.close();
        oss.close();
        
        AyudaDebug.printMetodo("saveObject:end");
    }
    
    /**
     * Método que recupera un objecto con nombre 'nameFile' en el path 'tempFile'
     * 
     * @param pathFile
     * @param nameFile
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object getObject(String pathFile, String nameFile) throws IOException, ClassNotFoundException{
        AyudaDebug.printMetodo("getObject:init");
        AyudaDebug.print(pathFile+nameFile);
        Object recuperarObjecto = null;

        File file = new File(pathFile+nameFile);
        FileInputStream fin = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fin);
        recuperarObjecto = (Object) ois.readObject();
       
        fin.close();

        AyudaDebug.printMetodo("getObject:end");
        return recuperarObjecto;
    }
}
