package tag.matcherImplementations;

import link.LinkUrl;
import tag.OptionalTag;
import tag.Tag;
import tag.TagMatcher;
import tag.TagName;

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

public class CustomTagMatcher extends TagMatcher {

	private final Pattern regex;

	public CustomTagMatcher(TagName name, String regexString) {
		this.regex = Pattern.compile(regexString); // TODO this could throw??
		this.tagName = name;
	}

	public Pattern getRegex() {
		return regex;
	}

	@Override
	public OptionalTag ifMatches(LinkUrl linkUrl) {
		Matcher matcher = regex.matcher(linkUrl.toString());

		if (matcher.find()) {
			return OptionalTag.of(new Tag(this.tagName));
		}
		return OptionalTag.empty();
	}
}
