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

data class Vector2i(
	val x: Int = 0,
	val y: Int = 0,
) : IStruct2i, Vector() {
	constructor(value: IStruct2i) : this(value.component1(), value.component2())
	constructor(value: Vector2i) : this(value.x, value.y)

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

	override val isFinite: Boolean
		get() = true
	override val isNaN: Boolean
		get() = false

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

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

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

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

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

	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)

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

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

	override fun hashCode(): Int {
		var x = x.rotateLeft(16) xor y
		// 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): Int {
		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: Int, y: Int) = this.x * x + this.y * y
	fun dot(value: IStruct2i): Int { val (x, y) = value; return dot(x, y) }
	fun dot(value: Vector2i): Int { val (x, y) = value; return dot(x, y) }

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

	fun distanceSquared(value: IStruct2i): Double { val (x, y) = value; return distanceSquared(x, y) }
	fun distanceSquared(value: Vector2i): Double { val (x, y) = value; return distanceSquared(x, y) }
	fun distance(x: Int, y: Int) = sqrt(distanceSquared(x, y))
	fun distance(value: IStruct2i): Double { val (x, y) = value; return sqrt(distanceSquared(x, y)) }
	fun distance(value: Vector2i): Double { val (x, y) = value; return sqrt(distanceSquared(x, y)) }

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

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

	fun coerceIn(minimum: IStruct2i, maximum: IStruct2i) = Vector2i(
		this.x.coerceIn(minimum.component1(), maximum.component1()),
		this.y.coerceIn(minimum.component2(), maximum.component2()))

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

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

	companion object {
		@JvmField val ZERO = Vector2i()
		@JvmField val POSITIVE_X = Vector2i(x = 1)
		@JvmField val NEGATIVE_X = Vector2i(x = -1)
		@JvmField val POSITIVE_Y = Vector2i(y = 1)
		@JvmField val NEGATIVE_Y = Vector2i(y = -1)

		@JvmField val POSITIVE_XY = Vector2i(1, 1)
		@JvmField val NEGATIVE_XY = Vector2i(-1, -1)
	}
}
