package cz.vutbr.feec.utko.xonder02.lda;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Loader {
    private Dictionary dictionary;
    private int documentId = 0;
    private Dictionary emotionDictionary = new Dictionary();
    private ArrayList<Integer> documentEmotions = new ArrayList<Integer>();

    public Loader(Dictionary dictionary) {
        this.dictionary = dictionary;
    }

    public Dictionary getEmotionDictionary() {
        return emotionDictionary;
    }
    
    public int getDocumentEmotionId(Document document) {
        return documentEmotions.get(document.getId());
    }

    public String getDocumentEmotion(Document document) {
        return emotionDictionary.getTokenById(getDocumentEmotionId(document));
    }

    public void readDirectory(Corpus corpus, File folder, String emotion, boolean removeNotExistsTokens) throws IOException {
        for (File file : folder.listFiles()) {
            if (!file.isDirectory()) {

                Document document = createDocumentFromFile(file, removeNotExistsTokens);

                if (document.getTermCount() == 0) {
                    continue;
                }

                document.setId(documentId++);
                documentEmotions.add(emotionDictionary.putToken(emotion));

                corpus.putDocument(document);

                if (corpus.documentCount() % 100 == 0) {
                    System.out.print("Document loaded: ");
                    System.out.println(corpus.documentCount());
                }
            }
        }
    }

    public File readRandomFileFromDirectory(Corpus corpus, File folder, String emotion, boolean removeNotExistsTokens) throws IOException {
        File[] files = folder.listFiles();
        int index = (int) (Math.random() * files.length);
        File file = files[index];

        Document document = createDocumentFromFile(file, removeNotExistsTokens);

        if (document.getTermCount() == 0) {
            return file;
        }

        document.setId(documentId++);
        documentEmotions.add(emotionDictionary.putToken(emotion));

        corpus.putDocument(document);

        return file;
    }

    private String fileToString(File file) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);

        byte[] data = new byte[(int)file.length()];
        fileInputStream.read(data);
        fileInputStream.close();

        return new String(data, "UTF-8");
    }

    private Document createDocumentFromFile(File file, boolean removeNotExistsTokens) throws IOException {
        String fileContent = fileToString(file);
        StringTokenizer stringTokenizer = new StringTokenizer(fileContent);

        LinkedList<String> tokens = new LinkedList<String>();
        while (stringTokenizer.hasMoreTokens()) {
            String token = stringTokenizer.nextToken();
            token = token.toLowerCase();

            tokens.push(token);
        }

        return Document.generate(tokens.toArray(new String[tokens.size()]), dictionary, removeNotExistsTokens);
    }
}
