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 kotlin.math.sqrt

data class Vector3i(
	val x: Int = 0,
	val y: Int = 0,
	val z: Int = 0,
) : IStruct3i, Vector() {
	constructor(value: IStruct2i) : this(value.component1(), value.component2())
	constructor(value: Vector2i) : this(value.x, value.y)
	constructor(value: IStruct2i, z: Int) : this(value.component1(), value.component2(), z)
	constructor(value: Vector2i, z: Int) : this(value.x, value.y, z)
	constructor(value: IStruct3i) : this(value.component1(), value.component2(), value.component3())
	constructor(value: Vector3i) : 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() = true
	override val isNaN: Boolean
		get() = false

	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: Int = 0, y: Int = 0, z: Int = 0) = Vector3i(this.x + x, this.y + y, this.z + z)
	fun minus(x: Int = 0, y: Int = 0, z: Int = 0) = Vector3i(this.x - x, this.y - y, this.z - z)
	fun times(x: Int = 1, y: Int = 1, z: Int = 1) = Vector3i(this.x * x, this.y * y, this.z * z)
	fun div(x: Int = 1, y: Int = 1, z: Int = 1) = Vector3i(this.x / x, this.y / y, this.z / z)

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

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

	fun dot(x: Int, y: Int, z: Int) = this.x * x + this.y * y + this.z * z
	fun dot(value: IStruct3i): Int { val (x, y, z) = value; return dot(x, y, z) }
	fun dot(value: Vector3i): Int { val (x, y, z) = value; return dot(x, y, z) }

	fun distanceSquared(x: Int, y: Int, z: Int): 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: IStruct3i): Double { val (x, y, z) = value; return distanceSquared(x, y, z) }
	fun distanceSquared(value: Vector3i): Double { val (x, y, z) = value; return distanceSquared(x, y, z) }
	fun distance(x: Int, y: Int, z: Int) = sqrt(distanceSquared(x, y, z))
	fun distance(value: IStruct3i): Double { val (x, y, z) = value; return sqrt(distanceSquared(x, y, z)) }
	fun distance(value: Vector3i): Double { val (x, y, z) = value; return sqrt(distanceSquared(x, y, z)) }

	fun coerceAtLeast(x: Int, y: Int, z: Int) = Vector3i(this.x.coerceAtLeast(x), this.y.coerceAtLeast(y), this.z.coerceAtLeast(z))
	fun coerceAtMost(x: Int, y: Int, z: Int) = Vector3i(this.x.coerceAtMost(x), this.y.coerceAtMost(y), this.z.coerceAtMost(z))

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

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

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

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

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

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

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

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

		@JvmField val POSITIVE_XYZ = Vector3i(1, 1, 1)
		@JvmField val NEGATIVE_XYZ = Vector3i(-1, -1, -1)
	}
}
