local CONFIG_FILE_PATH = "client/Rival Boost/Config.txt"

local function SavePlayerConfig(player)
    if not (player and player.valid and player == consoleplayer) then return end
	local scfg = io.openlocal(CONFIG_FILE_PATH, "w+")
    scfg:write("boostsound " .. tostring(player.boostsound or 1) .. "\n")
    scfg:write("stompbutton " .. tostring(player.stompbutton or 0) .. "\n")
    scfg:write("boostbutton " .. tostring(player.boostbutton or 1) .. "\n")
    scfg:write("menu_boost_color " .. tostring(player.menu_boost_color or 0) .. "\n")    
    scfg:close()
end

local function LoadPlayerConfig(player)
    if not (player and player.valid and player == consoleplayer) then return end
    local conf = io.openlocal(CONFIG_FILE_PATH, "r")
	if not conf then
        SavePlayerConfig(player)
		return
	end
	for line in conf:lines() do
		local key, val = line:match("([^%s]+)%s+([^%s]+)")
		if key and val then
			local numVal = tonumber(val)
			if numVal ~= nil then
				if key == "boostsound" then player.boostsound = numVal
				elseif key == "stompbutton" then player.stompbutton = numVal
				elseif key == "boostbutton" then player.boostbutton = numVal
				elseif key == "menu_boost_color" then player.menu_boost_color = numVal
				end
			end
		end
	end
	if player.menu_boost_color ~= nil then
		COM_BufInsertText(player, "bvfx " .. tostring(player.menu_boost_color))
	end
	print("\203Rival Boost Config has been Loaded successfully.")
	conf:close()
end


addHook("PlayerSpawn", function(player)
    if player == consoleplayer then
        LoadPlayerConfig(player)
    end
end)

addHook("PlayerThink", function(player)
    if not (player.mo and player.mo.valid) then return end
	if player.menu_open
		player.mo.rivalsave = true
	elseif player.mo.rivalsave
        SavePlayerConfig(player)
		CONS_Printf(player,"\203Rival Boost Config has been Saved successfully.")
		player.mo.rivalsave = nil
    end
end)



freeslot("sfx_loaded")

local booststtartupsound = false
addHook("ThinkFrame",function()
	if not booststtartupsound
		booststtartupsound = true
		S_StartSound(nil,sfx_loaded)
	end
end)
