local M = {}
local min = math.min
local max = math.max
local flightMode = "engine"

local function init()
	electrics.values.super_speed = 0
    electrics.values.super_throttle = 0
	electrics.values.super_thruster = 0
	electrics.values.super_roll = 0
	electrics.values.swingwing = 0
	electrics.values.superelev = 0
	electrics.values.superelev2 = 0
	electrics.values.superclutch = 1
	
	for k, node in pairs (v.data.nodes) do
		if node.name == "tloc"  then throtmod = k elseif
		   node.name == "r2"    then thrustsd = k elseif
		   node.name == "thrr5" then thrustra = k elseif
		   node.name == "thrr0" then thrustrb = k elseif
		   node.name == "thrl5" then thrustla = k elseif
		   node.name == "thrl0" then thrustlb = k end
	end
	
	if thrustsd ~= nil then
	    rocketsnd = obj:createSFXSource("vehicles/supertact/sounds/noise.ogg", 'AudioDefaultLoop3D', '', thrustsd)
	    obj:stopSFX(rocketsnd)
	end
	
	flightMode = "engine"
	superRocketFuel = 300000
end	

local function reset()
	init()
end

local function updateGFX(dt)
	
	if throtmod ~= nil then
		if electrics.values.throttle > 0.9 then electrics.values.super_thruster = electrics.values.throttle else electrics.values.super_thruster = 0 end
		electrics.values.super_throttle = electrics.values.throttle
		electrics.values.superclutch = electrics.values.clutchRatio
		if superRocketFuel > 0 and electrics.values.super_thruster > 0.9 then
			obj:setVolume(rocketsnd, electrics.values.super_thruster * 6 * electrics.values.superThrustLevel / 10000)
			obj:playSFX(rocketsnd)
			for i = 1,4,1 do
				if i == 1 then obj:addParticleByNodesRelative(thrustra, thrustrb,  -10, 64, 0, electrics.values.throttle * 3) end
				if i == 2 then obj:addParticleByNodesRelative(thrustla, thrustlb,  -10, 64, 0, electrics.values.throttle * 3) end
				if i == 3 then obj:addParticleByNodesRelative(thrustla, thrustlb, electrics.values.throttle * -30, 44, 0, electrics.values.throttle * 3) end
				if i == 4 then obj:addParticleByNodesRelative(thrustra, thrustrb, electrics.values.throttle * -30, 44, 0, electrics.values.throttle * 3) end
			end
			else 
			obj:stopSFX(rocketsnd)
		end
		--rocket fuel
		superRocketFuel = superRocketFuel - (electrics.values.superThrustLevel * electrics.values.super_thruster * dt)
		if superRocketFuel <= 0 then electrics.values.super_thruster = 0 superRocketFuel = 0 end
	
	elseif flightMode == "rocket" then
		electrics.values.swingwing = 1	
		electrics.values.wheelspeed = electrics.values.airspeed
		electrics.values.super_speed = electrics.values.airspeed
		electrics.values.super_roll = electrics.values.steering / 140
			if thrustra ~= nil then
				electrics.values.superclutch = 0
				electrics.values.super_thruster = electrics.values.throttle
				electrics.values.super_throttle = 0
				electrics.values.rpm = superRocketFuel * 0.02667
				if superRocketFuel > 0 then
				    obj:setVolume(rocketsnd, electrics.values.super_thruster * 6 * electrics.values.superThrustLevel / 10000)
				    obj:playSFX(rocketsnd)
					for i = 1,4,1 do
						if i == 1 then obj:addParticleByNodesRelative(thrustra, thrustrb,  -10, 64, 0, electrics.values.throttle * 3) end
					    if i == 2 then obj:addParticleByNodesRelative(thrustla, thrustlb,  -10, 64, 0, electrics.values.throttle * 3) end
					    if i == 3 then obj:addParticleByNodesRelative(thrustla, thrustlb, electrics.values.throttle * -30, 44, 0, electrics.values.throttle * 3) end
					    if i == 4 then obj:addParticleByNodesRelative(thrustra, thrustrb, electrics.values.throttle * -30, 44, 0, electrics.values.throttle * 3) end
					end
				    else 
				    obj:stopSFX(rocketsnd)
				end
				--rocket fuel
				superRocketFuel = superRocketFuel - (electrics.values.superThrustLevel * electrics.values.super_thruster * dt)
				if superRocketFuel <= 0 then electrics.values.super_thruster = 0 superRocketFuel = 0 end
				
				else
				electrics.values.super_throttle = electrics.values.throttle
				
				obj:stopSFX(rocketsnd)
				
			end
		else
		electrics.values.swingwing = 0
		electrics.values.super_thruster = 0
		electrics.values.super_roll = 0
		electrics.values.superelev = 0
		electrics.values.super_throttle = electrics.values.throttle
		electrics.values.super_speed = electrics.values.wheelspeed
		electrics.values.superclutch = electrics.values.clutchRatio
	end	
	
end

local function toggleFlightMode()
    if flightMode == "rocket" then
        flightMode = "engine"
		obj:stopSFX(rocketsnd)
    else
        flightMode = "rocket"
    end
    if thrustra ~= nil then gui.message("Throttle mode: " .. flightMode, 10, "supertact.Flightmode") end
end


M.onInit = init
M.onReset = reset
M.updateGFX = updateGFX
M.toggleFlightMode = toggleFlightMode

return M