package ballistics.utils.math

import kotlin.math.sqrt

class Vector3D(ax: Double, ay: Double, az: Double) {
    var x = ax
    var y = ay
    var z = az

    constructor() : this(0.0, 0.0, 0.0)

    fun setData(
        xVal: Double,
        yVal: Double,
        zVal: Double,
    ) {
        x = xVal
        y = yVal
        z = zVal
    }

    fun module() = sqrt(x * x + y * y + z * z)

    fun basis(): Vector3D {
        var v = Vector3D()
        v.x = x / module()
        v.y = y / module()
        v.z = z / module()
        return v
    }

    override fun toString(): String {
        return "x = $x, y = $y, z = $z"
    }

    operator fun plus(other: Vector3D) = Vector3D(x + other.x, y + other.y, z + other.z)

    operator fun minus(other: Vector3D) = Vector3D(x - other.x, y - other.y, z - other.z)

    operator fun times(value: Double) = Vector3D(x * value, y * value, z * value)

    operator fun times(other: Vector3D) = x * other.x + y * other.y + z * other.z

    operator fun rem(other: Vector3D) =
        Vector3D(
            y * other.z - z * other.y,
            z * other.x - x * other.z,
            x * other.y - y * other.x,
        )

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Vector3D

        if (x != other.x) return false
        if (y != other.y) return false
        if (z != other.z) return false

        return true
    }

    override fun hashCode(): Int {
        var result = x.hashCode()
        result = 31 * result + y.hashCode()
        result = 31 * result + z.hashCode()
        return result
    }
}
