package com.goafanti.crawler.callback;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.goafanti.crawler.RequestUtils;

public class TransitFutureCallback extends CrawlerFutureCallback {

	private String regex = null;

	@SuppressWarnings("unused")
	private TransitFutureCallback() {

	}

	public TransitFutureCallback(String regex) {
		this.regex = regex;
	}

	@Override
	protected void handleWebpage(String pageContent) {
		Pattern pattern = Pattern.compile(regex);
		Matcher m = pattern.matcher(pageContent);
		Map<String, CrawlerFutureCallback> cbs = new HashMap<>();
		while (m.find()) {
			cbs.put(m.group(1), new VacationFutureCallback(getResult()));
		}
		try {
			RequestUtils.startRequest(cbs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
