local module = {}

function module.new(this,piston,context)
	this.servo = _G.GetInstance(piston,"Servo")
	this.extension = _G.GetInstance(piston,"Extension")
	this.binding = _G.GetInstance(piston,"Binding")
	this.inverted = _G.GetInstance(piston,"Inverted")
	this.force = _G.GetInstance(piston,"Force")
	return this
end

local begin = tick()
module.run = _G.Protect(function(this,servo,context)
	local control = (this.control or context.controls[this.binding.Value] or 0)
	if this.inverted.Value then control = 1 - control end
	this.servo.TargetPosition = servo.Extension.Value * control * servo:GetScale() + (this.offset or 0)
	context.replicate(this,servo,module,this.servo.TargetPosition)
end)

module.replication_delta = 0.25
function module.replicate(this,servo,context,value)
	servo.ServoMaxForce = this.force.Value * servo:GetScale() ^ 2
	servo.TargetPosition = value
end

module.messages = {
	control = function(this,object,context,value)
		this.control = value
	end,
	offset = function(this,object,context,value)
		this.offset = value
	end,
}
module.messages.default = module.messages.control

module.manifest = {
	"Piston"
}

return module
