PlaceableVehicle = {
	specEntryName = string.format("spec_%s.vehiclePlaceable", g_currentModName)
}

function PlaceableVehicle.prerequisitesPresent(specializations)
	return true
end

function PlaceableVehicle.registerFunctions(placeableType)
	SpecializationUtil.registerFunction(placeableType, "onVehicleLoaded", PlaceableVehicle.onVehicleLoaded)
	SpecializationUtil.registerFunction(placeableType, "onVehicleDelete", PlaceableVehicle.onVehicleDelete)
end

function PlaceableVehicle.registerOverwrittenFunctions(placeableType)
	SpecializationUtil.registerOverwrittenFunction(placeableType, "canBeSold", PlaceableVehicle.canBeSold)
end

function PlaceableVehicle.registerEventListeners(placeableType)
	SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableVehicle)
	SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableVehicle)
end

function PlaceableVehicle.registerXMLPaths(schema, basePath)
	schema:setXMLSpecializationType("vehiclePlaceable")
	schema:register(XMLValueType.STRING, "placeable.vehicle#xmlFilename", "Vehicle xml filename")
	schema:register(XMLValueType.FLOAT, "placeable.vehicle#heightOffset", "Vehicle height offset")
	schema:register(XMLValueType.FLOAT, "placeable.vehicle#yRotation", "Vehicle Y rotation offset")
	schema:setXMLSpecializationType()
end

function PlaceableVehicle:onLoad(savegame)
	if self.isServer then
		local filename = self.xmlFile:getValue("placeable.vehicle#xmlFilename")

		if filename ~= nil then
			local xmlFilename = Utils.getFilename(filename, self.baseDirectory)
			local posX, posY, posZ = getWorldTranslation(self.rootNode)
			local dx, _, dz = localDirectionToWorld(self.rootNode, 0, 0, 1)
			local rotY = MathUtil.getYRotationFromDirection(dx, dz)
			local heightOffset = self.xmlFile:getValue("placeable.vehicle#heightOffset", 0)
			local yRotation = self.xmlFile:getValue("placeable.vehicle#yRotation", 0)
			local location = {
				x = posX,
				y = posY + heightOffset,
				z = posZ,
				xRot = 0,
				yRot = rotY + math.rad(yRotation),
				zRot = 0
			}

			VehicleLoadingUtil.loadVehicle(xmlFilename, location, true, 0, Vehicle.PROPERTY_STATE_NONE, self:getOwnerFarmId(), nil, nil, self.onVehicleLoaded, self)
		end
	end
end

function PlaceableVehicle:onDelete()
	local spec = self[PlaceableVehicle.specEntryName]

	if spec.vehicle ~= nil then
		g_currentMission:removeVehicle(spec.vehicle)

		spec.vehicle = nil
	end
end

function PlaceableVehicle:canBeSold(superFunc)
	local spec = self[PlaceableVehicle.specEntryName]

	if spec.vehicle ~= nil and spec.vehicle:getIsActive() then
		return false, g_i18n:getText("shop_messageReturnVehicleInUse")
	end

	return superFunc(self)
end

function PlaceableVehicle:onVehicleLoaded(vehicle, vehicleLoadState, args)
	local spec = self[PlaceableVehicle.specEntryName]

	if vehicle ~= nil then
		vehicle.isVehicleSaved = false
		vehicle:addDeleteListener(self, "onVehicleDelete")

		spec.vehicle = vehicle
	end
end

function PlaceableVehicle:onVehicleDelete(vehicle)
	local spec = self[PlaceableVehicle.specEntryName]

	if spec.vehicle == vehicle then
		self:delete()

		spec.vehicle = nil
	end
end