package ru.dbotthepony.kstarbound.math.vector

import ru.dbotthepony.kommons.util.IStruct2d
import ru.dbotthepony.kommons.util.IStruct2f
import ru.dbotthepony.kommons.util.IStruct2i
import ru.dbotthepony.kommons.util.IStruct3d
import ru.dbotthepony.kommons.util.IStruct3f
import ru.dbotthepony.kommons.util.IStruct3i
import ru.dbotthepony.kstarbound.math.matrix.Matrix3f
import ru.dbotthepony.kstarbound.math.matrix.Matrix4f
import ru.dbotthepony.kstarbound.math.matrix.Matrix4fStack
import kotlin.math.absoluteValue
import kotlin.math.sqrt

data class Vector3f(
	val x: Float = 0f,
	val y: Float = 0f,
	val z: Float = 0f,
) : IStruct3f, Vector() {
	constructor(value: IStruct2f) : this(value.component1(), value.component2())
	constructor(value: Vector2f) : this(value.x, value.y)
	constructor(value: IStruct2f, z: Float) : this(value.component1(), value.component2(), z)
	constructor(value: Vector2f, z: Float) : this(value.x, value.y, z)
	constructor(value: IStruct3f) : this(value.component1(), value.component2(), value.component3())
	constructor(value: Vector3f) : this(value.x, value.y, value.z)

	override val lengthSquared: Double
		get() = x.toDouble() * x + y.toDouble() * y + z.toDouble() * z

	override val isFinite: Boolean
		get() = x.isFinite() && y.isFinite() && z.isFinite()
	override val isNaN: Boolean
		get() = x.isNaN() || y.isNaN() || z.isNaN()

	val unitVector: Vector3f
		get() {
		var length = lengthSquared.toFloat()
		if (length == 0f || length == 1f) return this
		length = sqrt(length)
		return Vector3f(x / length, y / length, z / length)
	}

	val absoluteValue: Vector3f
		get() {
		val x = x.absoluteValue
		val y = y.absoluteValue
		val z = z.absoluteValue
		if (x != this.x || y != this.y || z != this.z) return Vector3f(x, y, z)
		return this
	}

	val r get() = x
	val g get() = y
	val b get() = z

	val s get() = x
	val t get() = y
	val p get() = z

	fun plus(x: Float = 0f, y: Float = 0f, z: Float = 0f) = Vector3f(this.x + x, this.y + y, this.z + z)
	fun minus(x: Float = 0f, y: Float = 0f, z: Float = 0f) = Vector3f(this.x - x, this.y - y, this.z - z)
	fun times(x: Float = 1f, y: Float = 1f, z: Float = 1f) = Vector3f(this.x * x, this.y * y, this.z * z)
	fun div(x: Float = 1f, y: Float = 1f, z: Float = 1f) = Vector3f(this.x / x, this.y / y, this.z / z)

	operator fun plus(value: IStruct3f): Vector3f { val (x, y, z) = value; return plus(x, y, z) }
	operator fun minus(value: IStruct3f): Vector3f { val (x, y, z) = value; return minus(x, y, z) }
	operator fun times(value: IStruct3f): Vector3f { val (x, y, z) = value; return times(x, y, z) }
	operator fun div(value: IStruct3f): Vector3f { val (x, y, z) = value; return div(x, y, z) }
	operator fun plus(value: IStruct2f): Vector3f { val (x, y) = value; return plus(x, y) }
	operator fun minus(value: IStruct2f): Vector3f { val (x, y) = value; return minus(x, y) }
	operator fun times(value: IStruct2f): Vector3f { val (x, y) = value; return times(x, y) }
	operator fun div(value: IStruct2f): Vector3f { val (x, y) = value; return div(x, y) }

	operator fun plus(value: Vector3f): Vector3f { val (x, y, z) = value; return plus(x, y, z) }
	operator fun minus(value: Vector3f): Vector3f { val (x, y, z) = value; return minus(x, y, z) }
	operator fun times(value: Vector3f): Vector3f { val (x, y, z) = value; return times(x, y, z) }
	operator fun div(value: Vector3f): Vector3f { val (x, y, z) = value; return div(x, y, z) }
	operator fun plus(value: Vector2f): Vector3f { val (x, y) = value; return plus(x, y) }
	operator fun minus(value: Vector2f): Vector3f { val (x, y) = value; return minus(x, y) }
	operator fun times(value: Vector2f): Vector3f { val (x, y) = value; return times(x, y) }
	operator fun div(value: Vector2f): Vector3f { val (x, y) = value; return div(x, y) }

	operator fun plus(value: Float) = plus(value, value, value)
	operator fun minus(value: Float) = minus(value, value, value)
	operator fun times(value: Float) = times(value, value, value)
	operator fun div(value: Float) = div(value, value, value)

	fun plus(x: Int = 0, y: Int = 0, z: Int = 0) = Vector3f(this.x + x, this.y + y, this.z + z)
	fun minus(x: Int = 0, y: Int = 0, z: Int = 0) = Vector3f(this.x - x, this.y - y, this.z - z)
	fun times(x: Int = 1, y: Int = 1, z: Int = 1) = Vector3f(this.x * x, this.y * y, this.z * z)
	fun div(x: Int = 1, y: Int = 1, z: Int = 1) = Vector3f(this.x / x, this.y / y, this.z / z)

	operator fun plus(value: IStruct3i): Vector3f { val (x, y, z) = value; return plus(x, y, z) }
	operator fun minus(value: IStruct3i): Vector3f { val (x, y, z) = value; return minus(x, y, z) }
	operator fun times(value: IStruct3i): Vector3f { val (x, y, z) = value; return times(x, y, z) }
	operator fun div(value: IStruct3i): Vector3f { val (x, y, z) = value; return div(x, y, z) }
	operator fun plus(value: IStruct2i): Vector3f { val (x, y) = value; return plus(x, y) }
	operator fun minus(value: IStruct2i): Vector3f { val (x, y) = value; return minus(x, y) }
	operator fun times(value: IStruct2i): Vector3f { val (x, y) = value; return times(x, y) }
	operator fun div(value: IStruct2i): Vector3f { val (x, y) = value; return div(x, y) }

	operator fun plus(value: Vector3i): Vector3f { val (x, y, z) = value; return plus(x, y, z) }
	operator fun minus(value: Vector3i): Vector3f { val (x, y, z) = value; return minus(x, y, z) }
	operator fun times(value: Vector3i): Vector3f { val (x, y, z) = value; return times(x, y, z) }
	operator fun div(value: Vector3i): Vector3f { val (x, y, z) = value; return div(x, y, z) }
	operator fun plus(value: Vector2i): Vector3f { val (x, y) = value; return plus(x, y) }
	operator fun minus(value: Vector2i): Vector3f { val (x, y) = value; return minus(x, y) }
	operator fun times(value: Vector2i): Vector3f { val (x, y) = value; return times(x, y) }
	operator fun div(value: Vector2i): Vector3f { val (x, y) = value; return div(x, y) }

	operator fun plus(value: Int) = plus(value, value, value)
	operator fun minus(value: Int) = minus(value, value, value)
	operator fun times(value: Int) = times(value, value, value)
	operator fun div(value: Int) = div(value, value, value)

	fun plus(x: Double = 0.0, y: Double = 0.0, z: Double = 0.0) = Vector3d(this.x + x, this.y + y, this.z + z)
	fun minus(x: Double = 0.0, y: Double = 0.0, z: Double = 0.0) = Vector3d(this.x - x, this.y - y, this.z - z)
	fun times(x: Double = 1.0, y: Double = 1.0, z: Double = 1.0) = Vector3d(this.x * x, this.y * y, this.z * z)
	fun div(x: Double = 1.0, y: Double = 1.0, z: Double = 1.0) = Vector3d(this.x / x, this.y / y, this.z / z)

	operator fun plus(value: IStruct3d): Vector3d { val (x, y, z) = value; return plus(x, y, z) }
	operator fun minus(value: IStruct3d): Vector3d { val (x, y, z) = value; return minus(x, y, z) }
	operator fun times(value: IStruct3d): Vector3d { val (x, y, z) = value; return times(x, y, z) }
	operator fun div(value: IStruct3d): Vector3d { val (x, y, z) = value; return div(x, y, z) }
	operator fun plus(value: IStruct2d): Vector3d { val (x, y) = value; return plus(x, y) }
	operator fun minus(value: IStruct2d): Vector3d { val (x, y) = value; return minus(x, y) }
	operator fun times(value: IStruct2d): Vector3d { val (x, y) = value; return times(x, y) }
	operator fun div(value: IStruct2d): Vector3d { val (x, y) = value; return div(x, y) }

	operator fun plus(value: Vector3d): Vector3d { val (x, y, z) = value; return plus(x, y, z) }
	operator fun minus(value: Vector3d): Vector3d { val (x, y, z) = value; return minus(x, y, z) }
	operator fun times(value: Vector3d): Vector3d { val (x, y, z) = value; return times(x, y, z) }
	operator fun div(value: Vector3d): Vector3d { val (x, y, z) = value; return div(x, y, z) }
	operator fun plus(value: Vector2d): Vector3d { val (x, y) = value; return plus(x, y) }
	operator fun minus(value: Vector2d): Vector3d { val (x, y) = value; return minus(x, y) }
	operator fun times(value: Vector2d): Vector3d { val (x, y) = value; return times(x, y) }
	operator fun div(value: Vector2d): Vector3d { val (x, y) = value; return div(x, y) }

	operator fun plus(value: Double) = plus(value, value, value)
	operator fun minus(value: Double) = minus(value, value, value)
	operator fun times(value: Double) = times(value, value, value)
	operator fun div(value: Double) = div(value, value, value)

	operator fun unaryMinus(): Vector3f = Vector3f(-x, -y, -z)

	fun dot(x: Float, y: Float, z: Float) = this.x * x + this.y * y + this.z * z
	fun dot(value: IStruct3f): Float { val (x, y, z) = value; return dot(x, y, z) }
	fun dot(value: Vector3f): Float { val (x, y, z) = value; return dot(x, y, z) }

	fun distanceSquared(x: Float, y: Float, z: Float): Double {
		val dx = (this.x - x).toDouble()
		val dy = (this.y - y).toDouble()
		val dz = (this.z - z).toDouble()
		return dx * dx + dy * dy + dz * dz
	}

	fun distanceSquared(value: IStruct3f): Double { val (x, y, z) = value; return distanceSquared(x, y, z) }
	fun distanceSquared(value: Vector3f): Double { val (x, y, z) = value; return distanceSquared(x, y, z) }
	fun distance(x: Float, y: Float, z: Float) = sqrt(distanceSquared(x, y, z))
	fun distance(value: IStruct3f): Double { val (x, y, z) = value; return sqrt(distanceSquared(x, y, z)) }
	fun distance(value: Vector3f): Double { val (x, y, z) = value; return sqrt(distanceSquared(x, y, z)) }

	fun coerceAtLeast(x: Float, y: Float, z: Float) = Vector3f(this.x.coerceAtLeast(x), this.y.coerceAtLeast(y), this.z.coerceAtLeast(z))
	fun coerceAtMost(x: Float, y: Float, z: Float) = Vector3f(this.x.coerceAtMost(x), this.y.coerceAtMost(y), this.z.coerceAtMost(z))

	fun coerceIn(minimum: Vector3f, maximum: Vector3f) = Vector3f(
		this.x.coerceIn(minimum.x, maximum.x),
		this.y.coerceIn(minimum.y, maximum.y),
		this.z.coerceIn(minimum.z, maximum.z))

	fun coerceIn(minimum: IStruct3f, maximum: IStruct3f) = Vector3f(
		this.x.coerceIn(minimum.component1(), maximum.component1()),
		this.y.coerceIn(minimum.component2(), maximum.component2()),
		this.z.coerceIn(minimum.component3(), maximum.component3()))

	fun coerceAtLeast(value: IStruct3f): Vector3f { val (x, y, z) = value; return coerceAtLeast(x, y, z) }
	fun coerceAtMost(value: IStruct3f): Vector3f { val (x, y, z) = value; return coerceAtMost(x, y, z) }
	fun coerceAtLeast(value: Vector3f): Vector3f { val (x, y, z) = value; return coerceAtLeast(x, y, z) }
	fun coerceAtMost(value: Vector3f): Vector3f { val (x, y, z) = value; return coerceAtMost(x, y, z) }

	fun cross(x: Float, y: Float, z: Float): Vector3f {
		return Vector3f(
			this.y * z - this.z * y,
			this.z * x - this.x * z,
			this.x * y - this.y * x,
		)
	}

	fun cross(value: IStruct3f): Vector3f { val (x, y, z) = value; return cross(x, y, z) }
	fun cross(value: Vector3f): Vector3f { val (x, y, z) = value; return cross(x, y, z) }

	operator fun times(value: Matrix4f): Vector3f {
		return Vector3f(
			x * value.c00 + y * value.c10 + z * value.c20 + value.c30,
			x * value.c01 + y * value.c11 + z * value.c21 + value.c31,
			x * value.c02 + y * value.c12 + z * value.c22 + value.c32,
		)
	}

	operator fun times(value: Matrix3f): Vector3f {
		return Vector3f(
			x * value.c00 + y * value.c10 + z * value.c20,
			x * value.c01 + y * value.c11 + z * value.c21,
			x * value.c02 + y * value.c12 + z * value.c22,
		)
	}

	operator fun times(value: Matrix4fStack) = times(value.last())

	fun toDoubleVector() = Vector3d(x.toDouble(), y.toDouble(), z.toDouble())
	fun toFloatVector() = this

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

	companion object {
		@JvmField val ZERO = Vector3f()
		@JvmField val POSITIVE_X = Vector3f(x = 1.0f)
		@JvmField val NEGATIVE_X = Vector3f(x = -1.0f)
		@JvmField val POSITIVE_Y = Vector3f(y = 1.0f)
		@JvmField val NEGATIVE_Y = Vector3f(y = -1.0f)
		@JvmField val POSITIVE_Z = Vector3f(z = 1.0f)
		@JvmField val NEGATIVE_Z = Vector3f(z = -1.0f)

		@JvmField val POSITIVE_XYZ = Vector3f(1.0f, 1.0f, 1f)
		@JvmField val NEGATIVE_XYZ = Vector3f(-1.0f, -1.0f, -1f)
	}
}
