package persistence.customTags;

import persistence.csv.CSVSerializable;
import tag.TagName;
import tag.matcherImplementations.CustomTagMatcher;

public class CustomTagMatcherEntity implements CSVSerializable {

	private final String name;
	private final String regexString;

	public CustomTagMatcherEntity(CustomTagMatcher customTagMatcher) {
		this.name = customTagMatcher.getTagName().name();
		this.regexString = customTagMatcher.getRegex().toString();
	}

	public CustomTagMatcherEntity(String[] fields) {
		this.name = fields[0];
		this.regexString = fields[1];
	}

	public CustomTagMatcherEntity(String name, String regexString) {
		this.name = name;
		this.regexString = regexString;
	}

	public CustomTagMatcher toCustomTagMatcher() {
		return new CustomTagMatcher(new TagName(name), regexString);
	}

	@Override
	public String[] getHeaders() {
		return new String[]{"name", "regexString"};
	}

	@Override
	public String toCSVString() {
		return name + CSVSerializable.seperator + regexString;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		CustomTagMatcherEntity that = (CustomTagMatcherEntity) o;

		if (name != null ? !name.equals(that.name) : that.name != null) return false;
		return regexString != null ? regexString.equals(that.regexString) : that.regexString == null;
	}

	@Override
	public int hashCode() {
		int result = name != null ? name.hashCode() : 0;
		result = 31 * result + (regexString != null ? regexString.hashCode() : 0);
		return result;
	}
}
