package ru.job4j.monitoreSync;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * Class SearchThread.
 * @author Andrey Kustov
 * @version $1.0$
 * @since 26.07.2017
 */
public class SearchThread extends Thread {
    /**
     * text.
     */
    private String text;
    /**
     * adresses.
     */
    private List<String> adresses;

    /**
     * constructor.
     * @param text text.
     * @param adresses adresses.
     */
    public SearchThread(String text, List<String> adresses) {
        this.text = text;
        this.adresses = adresses;
    }

    /**
     * getter for adresses.
     * @return adresses.
     */
    public List<String> getAdresses() {
        return adresses;
    }
    /**
     * run.
     */
    @Override
    public void run() {
        BufferedReader reader;
        String lines = "";
        for (String adres : adresses) {
            FileReader fileReader = null;
            try {
                fileReader = new FileReader(adres);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            reader = new BufferedReader(fileReader);
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    lines += line;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (lines.contains(text)) {
                adresses.add(adres);
            }
        }
    }
}
