package link;

import category.CategoryId;
import tag.Tag;
import tag.TagName;
import user.Username;

import java.util.Set;

public class Link {

	private final LinkId id;
	private final Username creator;
	private final LinkUrl url;
	private final Set<CategoryId> categoryIds;
	private final Set<Tag> tags;

	public Link(LinkId id, Username creator, LinkUrl url, Set<CategoryId> categoryIds, Set<Tag> tags) {
		this.id = id;
		this.creator = creator;
		this.url = url;
		this.categoryIds = categoryIds;
		this.tags = tags;
	}

	public LinkId getId() {
		return id;
	}

	public Username getCreator() {
		return creator;
	}

	public LinkUrl getUrl() {
		return url;
	}

	public boolean hasCategoryId(CategoryId categoryId) {
		return categoryIds.contains(categoryId);
	}

	public Set<CategoryId> getCategoryIds() {
		return categoryIds;
	}

	public boolean hasTagName(TagName tagName) {
		return tags.stream().anyMatch(tag -> tag.getName().equals(tagName));
	}

	public boolean wasCreatedBy(Username username) {
		return this.creator.equals(username);
	}

	public Set<Tag> getTags() {
		return tags;
	}

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

		Link link = (Link) o;

		if (!id.equals(link.id)) return false;
		return url.equals(link.url);
	}

	@Override
	public String toString() {
		return url +
				" created by =" + creator +
				", categoryIds=" + categoryIds +
				", tags=" + tags +
				'}';
	}

	@Override
	public int hashCode() {
		int result = id.hashCode();
		result = 31 * result + url.hashCode();
		return result;
	}
}
