package ru.dbotthepony.kstarbound.lua.bindings

import org.classdump.luna.Table
import ru.dbotthepony.kstarbound.lua.LuaEnvironment
import ru.dbotthepony.kstarbound.lua.get
import ru.dbotthepony.kstarbound.lua.luaFunction
import ru.dbotthepony.kstarbound.lua.set
import ru.dbotthepony.kstarbound.lua.toVector2d
import ru.dbotthepony.kstarbound.world.World

fun provideWorldBindings(self: World<*, *>, lua: LuaEnvironment) {
	val callbacks = lua.newTable()
	lua.globals["world"] = callbacks

	callbacks["flyingType"] = luaFunction {
		returnBuffer.setTo(self.sky.flyingType.jsonName)
	}

	callbacks["magnitude"] = luaFunction { arg1: Table, arg2: Table? ->
		if (arg2 == null) {
			returnBuffer.setTo(toVector2d(arg1).length)
		} else {
			returnBuffer.setTo(self.geometry.diff(toVector2d(arg1), toVector2d(arg2)).length)
		}
	}
}
