/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package monitor.utiles;

import java.io.*;
import java.util.ArrayList;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import static monitor.Ventana.log;

/**
 *
 * @author fernando
 */
public class Global {

    public static String buscarFichero(String carpeta, String nombre) {
        File fil = new File(carpeta, nombre);
        if (!fil.exists()) {
            File dir = new File(carpeta);
            String mNombre = nombre.toLowerCase();
            for (String s : dir.list()) {
                if (s.toLowerCase().equals(mNombre)) {
                    return s;
                }
            }
        }
        return nombre;
    }

    public static String PathCombine(Object... vars) {
        StringBuilder sb = new StringBuilder();
        for (Object var : vars) {
            String comp = var.toString();
            comp = comp.replace("\\", File.separator);
            sb.append(comp);
            if (!comp.endsWith(File.separator)) {
                sb.append(File.separator);
            }
        }
        sb.setLength(sb.length() - 1);
        return sb.toString();
    }

    public static void sleep(long milisecs) {
        Thread.interrupted();
        try {
            Thread.sleep(milisecs);
        } catch (Exception exc) {
            log.log(Level.WARNING, "Error en sleep({0})", milisecs);
        }
    }

    public static String[] split(String s, char sep) {
        ArrayList<String> array = new ArrayList<>();
        int comi = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == sep) {
                array.add(s.substring(comi, i));
                comi = i + 1;
            }
        }
        if (comi < s.length()) {
            array.add(s.substring(comi));
        }
        String[] res = new String[array.size()];
        return array.toArray(res);
    }

    public static String sistemaOperativo() {
        return System.getProperty("os.name").toLowerCase();
    }

    public static boolean esLinux() {
        return sistemaOperativo().contains("linux");
    }
    
    public static String[] FileReadAllLines(File f) throws IOException{
        BufferedReader bf = new BufferedReader(new java.io.FileReader(f));
        ArrayList<String> lineas = new ArrayList<>(100);
        String linea;
        while ((linea = bf.readLine()) != null) {
            lineas.add(linea);
        }
        return lineas.toArray(new String[] {});
    }

    public static String[] FileReadAllLineNotEmpty(File f) throws IOException{
        BufferedReader bf = new BufferedReader(new java.io.FileReader(f));
        ArrayList<String> lineas = new ArrayList<>(100);
        String linea;
        while ((linea = bf.readLine()) != null) {
            if(!linea.trim().isEmpty()) {
                lineas.add(linea);
            }
        }
        return lineas.toArray(new String[] {});
    }
    
    public static String FileReadAllText(File f) throws IOException {
        char[] bufer = new char[(int)f.length()];
        FileReader rdr = new java.io.FileReader(f);
        rdr.read(bufer, 0, bufer.length);
        return String.valueOf(bufer);
    }
    
    public static void extraerRecursoDelJar(String nombre, String nuevoNombre) {
        try {
            JarFile jar = new JarFile("Monitor.jar");
            JarEntry entrada = jar.getJarEntry(nombre);
            if (entrada == null) {
                jar.close();
                return;
            }
            File salida;
            if (nuevoNombre != null) {
                salida = new File(nuevoNombre);
            } else {
                salida = new File(nombre);
            }
            long fechaInterna = entrada.getTime();
            if (salida.exists()) {
                long fechaExterna = salida.lastModified();
                if (fechaInterna == fechaExterna) {
                    jar.close();
                    return;
                }
            }
            InputStream is = new BufferedInputStream(jar.getInputStream(entrada));
            OutputStream os = new BufferedOutputStream(new FileOutputStream(salida));
            byte[] bufer = new byte[2048];
            for (;;) {
                int nBytes = is.read(bufer);
                if (nBytes <= 0) {
                    break;
                }
                os.write(bufer, 0, nBytes);
            }
            os.flush();
            os.close();
            salida.setLastModified(fechaInterna);
            jar.close();
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }

    
}
