package persistence.category;

import category.Category;
import category.CategoryId;
import category.CategoryName;
import persistence.csv.CSVSerializable;

import java.util.Objects;

public class CategoryEntity implements CSVSerializable {

	private final String name;
	private final int id;

	public CategoryEntity(String name, int id) {
		this.name = name;
		this.id = id;
	}

	public CategoryEntity(String[] fields) {
		this(fields[0], Integer.parseInt(fields[1]));
	}

	public CategoryEntity(Category category) {
		this(category.getName().getName(), category.getId().id());
	}

	public Category toCategory() {
		return new Category(new CategoryName(name), new CategoryId(id));
	}

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

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

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		CategoryEntity that = (CategoryEntity) o;
		return id == that.id && Objects.equals(name, that.name);
	}

	@Override
	public int hashCode() {
		return Objects.hash(name, id);
	}
}
