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.kstarbound.math.matrix.Matrix2f
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.acos
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	operator fun unaryMinus(): Vector2f = Vector2f(-x, -y)

	override fun equals(other: Any?): Boolean {
		return other is Vector2f && x == other.x && y == other.y
	}

	override fun hashCode(): Int {
		var x = x.hashCode().rotateLeft(16) xor y.hashCode()
		// avalanche bits using murmur3 hash
		x = x xor (x ushr 16)
		x *= -0x7a143595
		x = x xor (x ushr 13)
		x *= -0x3d4d51cb
		x = x xor (x ushr 16)
		return x
	}

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

	/**
	 * 2D vector cross product (Z coordinate)
	 */
	fun cross(other: IStruct2f): Float {
		return x * other.component2() - other.component1() * y
	}

	/**
	 * 2D vector cross product (Z coordinate)
	 */
	fun cross(other: IStruct2i): Float {
		return x * other.component2() - other.component1() * y
	}

	/**
	 * 2D vector cross product (Z coordinate)
	 */
	fun cross(other: IStruct2d): Double {
		return x * other.component2() - other.component1() * y
	}

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

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

	fun distanceSquared(value: IStruct2f): Double { val (x, y) = value; return distanceSquared(x, y) }
	fun distanceSquared(value: Vector2f): Double { val (x, y) = value; return distanceSquared(x, y) }
	fun distance(x: Float, y: Float) = sqrt(distanceSquared(x, y))
	fun distance(value: IStruct2f): Double { val (x, y) = value; return sqrt(distanceSquared(x, y)) }
	fun distance(value: Vector2f): Double { val (x, y) = value; return sqrt(distanceSquared(x, y)) }

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

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

	fun coerceIn(minimum: IStruct2f, maximum: IStruct2f) = Vector2f(
		this.x.coerceIn(minimum.component1(), maximum.component1()),
		this.y.coerceIn(minimum.component2(), maximum.component2()))

	fun coerceAtLeast(value: IStruct2f): Vector2f { val (x, y) = value; return coerceAtLeast(x, y) }
	fun coerceAtMost(value: IStruct2f): Vector2f { val (x, y) = value; return coerceAtMost(x, y) }
	fun coerceAtLeast(value: Vector2f): Vector2f { val (x, y) = value; return coerceAtLeast(x, y) }
	fun coerceAtMost(value: Vector2f): Vector2f { val (x, y) = value; return coerceAtMost(x, y) }

	fun rotate(angle: Float): Vector2f {
		val s = sin(angle)
		val c = cos(angle)

		return Vector2f(x * c - s * y, s * x + c * y)
	}

	fun toAngle(zeroAngle: Vector2f): Float {
		val dot = unitVector.dot(zeroAngle.unitVector)

		return if (y > 0.0) {
			acos(dot)
		} else {
			-acos(dot)
		}
	}

	fun toAngle(zeroAngle: IStruct2f): Float {
		return toAngle(Vector2f(zeroAngle))
	}

	fun toAngle(): Float {
		return atan2(y, x)
	}

	operator fun times(value: Matrix4f): Vector2f {
		return Vector2f(
			x * value.c00 + y * value.c10 + value.c30,
			x * value.c01 + y * value.c11 + value.c31,
		)
	}

	operator fun times(value: Matrix3f): Vector2f {
		return Vector2f(
			x * value.c00 + y * value.c10 + value.c20,
			x * value.c01 + y * value.c11 + value.c21,
		)
	}

	operator fun times(value: Matrix2f): Vector2f {
		return Vector2f(
			x * value.c00 + y * value.c10,
			x * value.c01 + y * value.c11,
		)
	}

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

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

	companion object {
		@JvmField val ZERO = Vector2f()
		@JvmField val POSITIVE_X = Vector2f(x = 1.0f)
		@JvmField val NEGATIVE_X = Vector2f(x = -1.0f)
		@JvmField val POSITIVE_Y = Vector2f(y = 1.0f)
		@JvmField val NEGATIVE_Y = Vector2f(y = -1.0f)

		@JvmField val POSITIVE_XY = Vector2f(1.0f, 1.0f)
		@JvmField val NEGATIVE_XY = Vector2f(-1.0f, -1.0f)
	}
}
