package fyloz.trial.ColorRecipesExplorer.model;

import org.hibernate.annotations.ColumnDefault;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Entity
public class MaterialType extends BeanModel implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer materialTypeID;

    @Column(unique = true)
    @NotNull
    private String materialTypeName;

    @NotNull
    private String prefix;

    @NotNull
    @ColumnDefault("false")
    private Boolean usePercentages = false;

    @OneToMany
    @JoinColumn(name = "material_type")
    private List<Material> materials;

    public MaterialType() {

    }

    public MaterialType(@NotNull String materialTypeName, @NotNull String prefix, @NotNull Boolean usePercentages) {
        this.materialTypeName = materialTypeName;
        this.prefix = prefix.toUpperCase();
        this.usePercentages = usePercentages;
    }

    public Integer getMaterialTypeID() {
        return materialTypeID;
    }

    public void setMaterialTypeID(Integer materialTypeID) {
        this.materialTypeID = materialTypeID;
    }

    public String getMaterialTypeName() {
        return materialTypeName;
    }

    public void setMaterialTypeName(String materialTypeName) {
        this.materialTypeName = materialTypeName;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix.toUpperCase();
    }

    public Boolean getUsePercentages() {
        return usePercentages;
    }

    public void setUsePercentages(Boolean usePercentages) {
        this.usePercentages = usePercentages;
    }

    public List<Material> getMaterials() {
        return materials;
    }

    @Override
    public Integer getID() {
        return materialTypeID;
    }

    @Override
    public String toString() {
        return "MaterialType{" +
                "materialTypeID=" + materialTypeID +
                ", materialTypeName='" + materialTypeName + '\'' +
                ", prefix='" + prefix + '\'' +
                ", usePercentages=" + usePercentages +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MaterialType that = (MaterialType) o;
        return Objects.equals(materialTypeID, that.materialTypeID) &&
                Objects.equals(materialTypeName, that.materialTypeName) &&
                Objects.equals(prefix, that.prefix) &&
                Objects.equals(usePercentages, that.usePercentages);
    }
}
