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.kommons.util.IStruct4d
import ru.dbotthepony.kommons.util.IStruct4f
import ru.dbotthepony.kommons.util.IStruct4i
import kotlin.math.sqrt

data class Vector4i(
	val x: Int = 0,
	val y: Int = 0,
	val z: Int = 0,
	val w: Int = 0,
) : IStruct4i, 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: IStruct2i, z: Int, w: Int) : this(value.component1(), value.component2(), z, w)
	constructor(value: Vector2i, z: Int, w: Int) : this(value.x, value.y, z, w)
	constructor(value: IStruct3i) : this(value.component1(), value.component2(), value.component3())
	constructor(value: Vector3i) : this(value.x, value.y, value.z)
	constructor(value: IStruct3i, w: Int) : this(value.component1(), value.component2(), value.component3(), w)
	constructor(value: Vector3i, w: Int) : this(value.x, value.y, value.z, w)
	constructor(value: IStruct4i) : this(value.component1(), value.component2(), value.component3(), value.component4())
	constructor(value: Vector4i) : this(value.x, value.y, value.z, value.w)

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

	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 a get() = w

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	fun distanceSquared(value: IStruct4i): Double { val (x, y, z, w) = value; return distanceSquared(x, y, z, w) }
	fun distanceSquared(value: Vector4i): Double { val (x, y, z, w) = value; return distanceSquared(x, y, z, w) }
	fun distance(x: Int, y: Int, z: Int, w: Int) = sqrt(distanceSquared(x, y, z, w))
	fun distance(value: IStruct4i): Double { val (x, y, z, w) = value; return sqrt(distanceSquared(x, y, z, w)) }
	fun distance(value: Vector4i): Double { val (x, y, z, w) = value; return sqrt(distanceSquared(x, y, z, w)) }

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

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

	fun coerceIn(minimum: IStruct4i, maximum: IStruct4i) = Vector4i(
		this.x.coerceIn(minimum.component1(), maximum.component1()),
		this.y.coerceIn(minimum.component2(), maximum.component2()),
		this.z.coerceIn(minimum.component3(), maximum.component3()),
		this.w.coerceIn(minimum.component4(), maximum.component4()))

	fun coerceAtLeast(value: IStruct4i): Vector4i { val (x, y, z, w) = value; return coerceAtLeast(x, y, z, w) }
	fun coerceAtMost(value: IStruct4i): Vector4i { val (x, y, z, w) = value; return coerceAtMost(x, y, z, w) }
	fun coerceAtLeast(value: Vector4i): Vector4i { val (x, y, z, w) = value; return coerceAtLeast(x, y, z, w) }
	fun coerceAtMost(value: Vector4i): Vector4i { val (x, y, z, w) = value; return coerceAtMost(x, y, z, w) }

	fun toDoubleVector() = Vector4d(x.toDouble(), y.toDouble(), z.toDouble(), w.toDouble())
	fun toFloatVector() = Vector4f(x.toFloat(), y.toFloat(), z.toFloat(), w.toFloat())

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

	companion object {
		@JvmField val ZERO = Vector4i()
		@JvmField val POSITIVE_X = Vector4i(x = 1)
		@JvmField val NEGATIVE_X = Vector4i(x = -1)
		@JvmField val POSITIVE_Y = Vector4i(y = 1)
		@JvmField val NEGATIVE_Y = Vector4i(y = -1)
		@JvmField val POSITIVE_Z = Vector4i(z = 1)
		@JvmField val NEGATIVE_Z = Vector4i(z = -1)
		@JvmField val POSITIVE_W = Vector4i(w = 1)
		@JvmField val NEGATIVE_W = Vector4i(w = -1)
	}
}
