package CustomSearch;

import DAO.UrlDAO;
import DAO.UserPreferenceDAO;
import EventHandler.EventManager;
import FileHandler.LogsReport;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import SecurityManager.*;
/**
 * Created by amanbansal on 25/06/17.
 */
public class SearchAll {
    private UserPreferenceDAO userPreferenceDAO = new UserPreferenceDAO();
    private ArrayList<String> mailUrls = new ArrayList<String>();
    private Integer index;
    private LogsReport logs = new LogsReport();
    private EventManager eventHandler = new EventManager();
    private HelperClass helperClass = new HelperClass();
    private String date_stamp;
    private RobotHelper robotHelper = new RobotHelper();

    public void FilterAllLinks(UrlDAO urlDAO){

        mailUrls = urlDAO.getMailUrls();
        int size = mailUrls.size();
        index = urlDAO.getMailIndex();

        for (int i = index; i < size; i++) {
            String link = mailUrls.get(i);
            try {
                if(!robotHelper.IsDisallowed(link)){
                    Document doc1 = Jsoup.connect(link).get();
                    Elements date_info = doc1.select("tr[class=\"date\"] > td[class=\"right\"]");
                    date_stamp = date_info.text();

                    helperClass.ExtractMailContentAndAttachement(link,date_stamp);

                    urlDAO.setMailIndex(urlDAO.getMailIndex()+1);
                    eventHandler.incrementProgressIndex(urlDAO);
                }

            } catch (Exception e) {
                logs.LogExceptionMessage(e);
            }
        }
    }


}
