local module = {}

local function burn_effect(this,boiler,context,burn)
	for index,item in pairs(this.burning) do
		item.Color = Color3.fromRGB(255, 137, 73):Lerp(Color3.fromRGB(115, 61, 33),1-math.abs(burn))
	end
end

function module.new(this,boiler,context)
	this.burning = {}
	this.scale = boiler:GetExtentsSize().Magnitude
	this.exhaust = _G.GetInstance(boiler,"Exhaust")
	for index,item in pairs(boiler:GetChildren()) do
		if item.Name == "Fire" then
			table.insert(this.burning,item)
		end
	end
	return this
end

function module.idle(this,boiler) return true end

local begin = tick()
module.run = function(this,boiler,context)
	local throttle = this.throttle or context.controls[_G.GetValue(boiler,"Binding","throttle")] or this.Throttle or 0
	context.replicate(this,boiler,module,throttle)
	local objects = {}
	context.connected("ConnectorFume","Exhaust",this.exhaust,function(object)
		table.insert(objects,object)
	end)
	for index,object in pairs(objects) do
		context.message(object,"flux",this.scale * math.abs(throttle) * context.delta * (1/#objects))
	end
	return true
end

module.replication_delta = 0.25
function module.replicate(this,boiler,context,burn)
	burn_effect(this,boiler,context,burn)
end

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

module.manifest = {
	"Boiler1",
	"Boiler2"
}

return module
