--[[
================================================================================================================================
											This code was made by JSD
================================================================================================================================
]]--
local M = {}

local myTimer = 0
local Snd_play = false
local toggle_flame_tune = 1

local function onInit(jbeamData)
	electrics.values["flames3"] = 0
	myTimer = 0
    print("FLAME TUNE MOD - Creating Sounds...")
    sounds.createSFXSource("vehicles/ekhv36/sustained.wav", 'AudioDefault3D', "FlameLoop", v.data.refNodes[0].ref)
end

local function onReset()
	onInit()
end

local function toggle_flames()
	if toggle_flame_tune >= 1 then
		guihooks.message("Flame Tune enabled!", 5, nil, "whatshot") 
		toggle_flame_tune = 0
	else
	toggle_flame_tune = toggle_flame_tune + 1
	guihooks.message("Flame Tune disabled!", 5, nil, "whatshot") 
	end
end

local function flame_sound()
	if Snd_play == true then
		print("Sound!!")
		sounds.playSoundOnceAtNode("FlameLoop", v.data.refNodes[0].ref, 100)
		Snd_play = false
	end
end

local function updateGFX(dt)

	local function timer()
		myTimer = myTimer + 0.01
		if myTimer >= 0.25 then
			myTimer = 0
			electrics.values["flames3"] = 0
		end
	end
	
	if electrics.values.throttle_input <= 0.5 and electrics.values.rpm >= 6000 then
		electrics.values["flames3"] = 1
		timer()
		flame_sound()
	else
		myTimer = 0
		electrics.values["flames3"] = 0
		timer()
	end

if electrics.values.airspeed <= 15 then
	
	if electrics.values.throttle_input <= 0.5 and electrics.values.rpm >= 4000 then
		electrics.values["flames3"] = 1
		flame_sound()
	else
		myTimer = 0
		electrics.values["flames3"] = 0
		Snd_play = true
	end

end

if electrics.values.airspeed >= 25 then
	
	if electrics.values.throttle_input <= 0.5 and electrics.values.rpm >= 5000 then
		electrics.values["flames3"] = 1
		flame_sound()
	else
		myTimer = 0
		electrics.values["flames3"] = 0
		Snd_play = true
	end

end

if toggle_flame_tune == 1 then
	electrics.values["flames3"] = 0
	Snd_play = false
end

end

-- public interface
M.onReset = onReset
M.onInit = onInit
M.updateGFX = updateGFX
M.toggle_flames = toggle_flames

return M
