package ru.dbotthepony.mc.otm.client.render

import ru.dbotthepony.mc.otm.core.FloatSupplier
import kotlin.math.roundToInt

private operator fun FloatSupplier.div(other: Float): Float {
	return getAsFloat() / other
}

private operator fun Float.minus(other: FloatSupplier): Float {
	return this - other.getAsFloat()
}

enum class RenderGravity {
	TOP_LEFT {
		override fun x(x: Float, width: FloatSupplier): Float {
			return x
		}

		override fun y(y: Float, height: FloatSupplier): Float {
			return y
		}
		
		override fun x(x: Float, width: Float): Float {
			return x
		}

		override fun y(y: Float, height: Float): Float {
			return y
		}
	},
	TOP_CENTER {
		override fun x(x: Float, width: FloatSupplier): Float {
			return x - width / 2f
		}

		override fun y(y: Float, height: FloatSupplier): Float {
			return y
		}
		
		override fun x(x: Float, width: Float): Float {
			return x - width / 2f
		}

		override fun y(y: Float, height: Float): Float {
			return y
		}
	},
	TOP_RIGHT {
		override fun x(x: Float, width: FloatSupplier): Float {
			return x - width
		}

		override fun y(y: Float, height: FloatSupplier): Float {
			return y
		}

		override fun x(x: Float, width: Float): Float {
			return x - width
		}

		override fun y(y: Float, height: Float): Float {
			return y
		}
	},

	CENTER_LEFT {
		override fun x(x: Float, width: FloatSupplier): Float {
			return x
		}

		override fun y(y: Float, height: FloatSupplier): Float {
			return y - height / 2f
		}

		override fun x(x: Float, width: Float): Float {
			return x
		}

		override fun y(y: Float, height: Float): Float {
			return y - height / 2f
		}
	},
	CENTER_CENTER {
		override fun x(x: Float, width: FloatSupplier): Float {
			return x - width / 2f
		}

		override fun y(y: Float, height: FloatSupplier): Float {
			return y - height / 2f
		}

		override fun x(x: Float, width: Float): Float {
			return x - width / 2f
		}

		override fun y(y: Float, height: Float): Float {
			return y - height / 2f
		}
	},
	CENTER_RIGHT {
		override fun x(x: Float, width: FloatSupplier): Float {
			return x - width
		}

		override fun y(y: Float, height: FloatSupplier): Float {
			return y - height / 2f
		}

		override fun x(x: Float, width: Float): Float {
			return x - width
		}

		override fun y(y: Float, height: Float): Float {
			return y - height / 2f
		}
	},

	BOTTOM_LEFT {
		override fun x(x: Float, width: FloatSupplier): Float {
			return x
		}

		override fun y(y: Float, height: FloatSupplier): Float {
			return y - height
		}

		override fun x(x: Float, width: Float): Float {
			return x
		}

		override fun y(y: Float, height: Float): Float {
			return y - height
		}
	},
	BOTTOM_CENTER {
		override fun x(x: Float, width: FloatSupplier): Float {
			return x - width / 2f
		}

		override fun y(y: Float, height: FloatSupplier): Float {
			return y - height
		}

		override fun x(x: Float, width: Float): Float {
			return x - width / 2f
		}

		override fun y(y: Float, height: Float): Float {
			return y - height
		}
	},
	BOTTOM_RIGHT {
		override fun x(x: Float, width: FloatSupplier): Float {
			return x - width
		}

		override fun y(y: Float, height: FloatSupplier): Float {
			return y - height
		}

		override fun x(x: Float, width: Float): Float {
			return x - width
		}

		override fun y(y: Float, height: Float): Float {
			return y - height
		}
	};

	abstract fun x(x: Float, width: FloatSupplier): Float
	abstract fun y(y: Float, height: FloatSupplier): Float

	abstract fun x(x: Float, width: Float): Float
	abstract fun y(y: Float, height: Float): Float

	fun x(x: Float, width: Int): Float {
		return x(x, width.toFloat()).roundToInt().toFloat()
	}

	fun y(y: Float, height: Int): Float {
		return y(y, height.toFloat()).roundToInt().toFloat()
	}
}
