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.Matrix3d
import ru.dbotthepony.kstarbound.math.matrix.Matrix4d
import ru.dbotthepony.kstarbound.math.matrix.Matrix4dStack
import kotlin.math.absoluteValue
import kotlin.math.sqrt

data class Vector3d(
	val x: Double = 0.0,
	val y: Double = 0.0,
	val z: Double = 0.0,
) : IStruct3d, Vector() {
	constructor(value: IStruct2d) : this(value.component1(), value.component2())
	constructor(value: Vector2d) : this(value.x, value.y)
	constructor(value: IStruct2d, z: Double) : this(value.component1(), value.component2(), z)
	constructor(value: Vector2d, z: Double) : this(value.x, value.y, z)
	constructor(value: IStruct3d) : this(value.component1(), value.component2(), value.component3())
	constructor(value: Vector3d) : this(value.x, value.y, value.z)

	override val lengthSquared: Double
		get() = x * x + y * y + z * 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: Vector3d
		get() {
		var length = lengthSquared
		if (length == 0.0 || length == 1.0) return this
		length = sqrt(length)
		return Vector3d(x / length, y / length, z / length)
	}

	val absoluteValue: Vector3d
		get() {
		val x = x.absoluteValue
		val y = y.absoluteValue
		val z = z.absoluteValue
		if (x != this.x || y != this.y || z != this.z) return Vector3d(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: 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)

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

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

	operator fun plus(value: Vector3i): Vector3d { val (x, y, z) = value; return plus(x, y, z) }
	operator fun minus(value: Vector3i): Vector3d { val (x, y, z) = value; return minus(x, y, z) }
	operator fun times(value: Vector3i): Vector3d { val (x, y, z) = value; return times(x, y, z) }
	operator fun div(value: Vector3i): Vector3d { val (x, y, z) = value; return div(x, y, z) }
	operator fun plus(value: Vector2i): Vector3d { val (x, y) = value; return plus(x, y) }
	operator fun minus(value: Vector2i): Vector3d { val (x, y) = value; return minus(x, y) }
	operator fun times(value: Vector2i): Vector3d { val (x, y) = value; return times(x, y) }
	operator fun div(value: Vector2i): Vector3d { 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: Float = 0f, y: Float = 0f, z: Float = 0f) = Vector3d(this.x + x, this.y + y, this.z + z)
	fun minus(x: Float = 0f, y: Float = 0f, z: Float = 0f) = Vector3d(this.x - x, this.y - y, this.z - z)
	fun times(x: Float = 1f, y: Float = 1f, z: Float = 1f) = Vector3d(this.x * x, this.y * y, this.z * z)
	fun div(x: Float = 1f, y: Float = 1f, z: Float = 1f) = Vector3d(this.x / x, this.y / y, this.z / z)

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

	operator fun plus(value: Vector3f): Vector3d { val (x, y, z) = value; return plus(x, y, z) }
	operator fun minus(value: Vector3f): Vector3d { val (x, y, z) = value; return minus(x, y, z) }
	operator fun times(value: Vector3f): Vector3d { val (x, y, z) = value; return times(x, y, z) }
	operator fun div(value: Vector3f): Vector3d { val (x, y, z) = value; return div(x, y, z) }
	operator fun plus(value: Vector2f): Vector3d { val (x, y) = value; return plus(x, y) }
	operator fun minus(value: Vector2f): Vector3d { val (x, y) = value; return minus(x, y) }
	operator fun times(value: Vector2f): Vector3d { val (x, y) = value; return times(x, y) }
	operator fun div(value: Vector2f): Vector3d { 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)

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

	fun dot(x: Double, y: Double, z: Double) = this.x * x + this.y * y + this.z * z
	fun dot(value: IStruct3d): Double { val (x, y, z) = value; return dot(x, y, z) }
	fun dot(value: Vector3d): Double { val (x, y, z) = value; return dot(x, y, z) }

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

	fun distanceSquared(value: IStruct3d): Double { val (x, y, z) = value; return distanceSquared(x, y, z) }
	fun distanceSquared(value: Vector3d): Double { val (x, y, z) = value; return distanceSquared(x, y, z) }
	fun distance(x: Double, y: Double, z: Double) = sqrt(distanceSquared(x, y, z))
	fun distance(value: IStruct3d): Double { val (x, y, z) = value; return sqrt(distanceSquared(x, y, z)) }
	fun distance(value: Vector3d): Double { val (x, y, z) = value; return sqrt(distanceSquared(x, y, z)) }

	fun coerceAtLeast(x: Double, y: Double, z: Double) = Vector3d(this.x.coerceAtLeast(x), this.y.coerceAtLeast(y), this.z.coerceAtLeast(z))
	fun coerceAtMost(x: Double, y: Double, z: Double) = Vector3d(this.x.coerceAtMost(x), this.y.coerceAtMost(y), this.z.coerceAtMost(z))

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

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

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

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

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

	operator fun times(value: Matrix4d): Vector3d {
		return Vector3d(
			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: Matrix3d): Vector3d {
		return Vector3d(
			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: Matrix4dStack) = times(value.last())

	fun toDoubleVector() = this
	fun toFloatVector() = Vector3f(x.toFloat(), y.toFloat(), z.toFloat())

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

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

		@JvmField val POSITIVE_XYZ = Vector3d(1.0, 1.0, 1.0)
		@JvmField val NEGATIVE_XYZ = Vector3d(-1.0, -1.0, -1.0)
	}
}
