package etiquetador;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Prof. Pablo Freire Matos
 * IV simSIS - Simpósio de Sistemas de Informação
 */
public class Util {

    public static ArrayList<String> getFiles(String directory, String[] extList) {

        ArrayList<String> files = new ArrayList<String>();
        File dir = new File(directory);
        String arq[] = dir.list();
        if (arq == null) {
            JOptionPane.showMessageDialog(null, "O diretório está incorreto.\n\n" + directory, "Erro", JOptionPane.ERROR_MESSAGE);
        } else {
            for (int i = 0; i < arq.length; i++) {
                for (int j = 0; j < extList.length; j++) {
                    String string = arq[i].toLowerCase() + "final";
                    String split[] = string.split("." + extList[j]);  // .xml, .html, .pdf
                    // Just get files with extension from extList[]
                    if (split.length > 1) {
                        files.add(arq[i].toLowerCase());
                        break;
                    }
                }
            }
        }
        return files;
    }

    public static ArrayList<String> getSentence(String filename) {

        ArrayList<String> lines = new ArrayList<String>();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "ISO-8859-1"));
            String line = "";
            while ((line = in.readLine()) != null) {  // seleciona por linha
                if (!line.equals("")) {
                    lines.add(line);
                }
            }
            in.close();
            return lines;
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "It's not possible to open the file!"
                    + filename + "!", "File Reading", JOptionPane.ERROR_MESSAGE);
        }
        return null;
    }

    public static void writeFile(String fileName, String data) {

        PrintWriter out;
        try {
            out = new PrintWriter(new FileWriter(fileName));
            out.print(data);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
