package dev.fyloz.trial.colorrecipesexplorer.model

import java.util.*
import javax.persistence.*

const val IDENTIFIER_MATERIAL_NAME = "material"

@Entity
data class MixType(
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    override val id: Long?,

    @Column(unique = true)
    override var name: String,

    @OneToOne(cascade = [CascadeType.ALL])
    var material: Material
) : NamedModel {
    constructor(name: String, material: Material) : this(null, name, material)
    constructor(name: String, materialType: MaterialType) : this(
        null,
        name,
        material(name = name, inventoryQuantity = Float.MIN_VALUE, isMixType = true, materialType = materialType)
    )

    override fun equals(other: Any?): Boolean = other is MixType && other.name == name && other.material == material
    override fun hashCode(): Int = Objects.hash(name, material)
}

// ==== DSL ====
fun mixType(
    id: Long? = null,
    name: String = "name",
    material: Material = material(),
    op: MixType.() -> Unit = {}
) = MixType(id, name, material).apply(op)

fun mixType(
    name: String = "name",
    materialType: MaterialType = materialType(),
    op: MixType.() -> Unit = {}
) = MixType(
    name,
    material(name = name, inventoryQuantity = 0f, isMixType = true, materialType = materialType)
).apply(op)
