// I/O example by Cyron. Slightly edited by frostiikin, but the vast majority of this is all cyron's doing.
// I/O Support, I don't know how to put them in one file so I make multiple vars
local configplayer--I'm actually not sure if configplayer ever needs to be used tbh, I'm mostly winging it off what I saw in derp code

local function valid(mo)
	return mo and mo.valid
end

local CANDY = "client/honey/candy.dat"--you need a place to save that shit
local HAIRCOLOR = "client/honey/haircolor.dat"--you need a place to save that shit
--local EGGMUSICTOGGLE = "client/eggmusictoggle.dat"--you need a place to save that shit
//local FILETWO = "client/whatever.dat DUMMY"
//

//
addHook("ThinkFrame", do--Cyron can't fucking code but this is IO stuff
	if valid(consoleplayer)
		and not valid(configplayer)--i really dont know if this is needed..
		and consoleplayer.candy == nil	--my codes ass backwards so this will always read nil at launch, that won't work for all types of codes probably
			configplayer = consoleplayer --i really dont know if this is needed..
			local file = io.openlocal(CANDY)--check for the file
            if file--do you actually have a file to read
                local num = file:read("*n")--tells the fucker to OPEN UP, use *a for strings, *n for numbers
					if num == 1
						COM_BufInsertText(consoleplayer, "h_candy ".."on")--tells that fucker to put the value here
					else
						COM_BufInsertText(consoleplayer, "h_candy ".."off")
					end
				file:close()--perish
            else--bitch where the file
                COM_BufInsertText(consoleplayer, "candy off")--this makes a default if theres no file to read
        end
			local file2 = io.openlocal(HAIRCOLOR)--check for the file
			--print("FUCK")
            if file2--do you actually have a file to read
			--print("FUCK")
                local num = file2:read("*a")--tells the fucker to OPEN UP, use *a for strings, *n for numbers
					if num == shet
						COM_BufInsertText(consoleplayer, "haircolor off")--tells that fucker to put the value here
					else
						COM_BufInsertText(consoleplayer, "haircolor off")
					end
				file2:close()--perish
            else--bitch where the file
                COM_BufInsertText(consoleplayer, "haircolor off")--this makes a default if theres no file to read
        end
	end
end)


//The Command Shit
local function CandyMode(player, arg)--Conjunction junction, what's your function?
	if player.mo and player.mo.valid
		if arg
			if arg == "on" or arg == "1" or arg == "true" or arg == "yes"--for the dummy these are numbers, but they can be strings, but you'll have to call the read differently
				player.candy =  1
				CONS_Printf(player, "You are now using the Candy alt!")
				if io and player == consoleplayer--this section calls to save the data
					local file = io.openlocal(CANDY, "w+")--w+ neans to write and overwrite all data in the file.
					file:write(player.candy)--put that value in the file, idfk how to make boolean functions save so i force put "true" or "false" here cuz im shit
					file:close()--perish
				end
			elseif arg == "off" or arg == "false" or arg == "0" or arg == "no"--same thing but different answer
				player.candy =  2
				CONS_Printf(player, "You are no longer using the Candy alt.")
				if io and player == consoleplayer
					local file = io.openlocal(CANDY, "w+")
					file:write(player.candy)
					file:close()
				end
			end
		else
			CONS_Printf(player, "Allows Honey to access her Candy alt. Your dress, undyed hair, and super colors will all change when activated.")
		end
	else
		CONS_Printf(player, "You can only use this in a level!")
	end
end

COM_AddCommand("h_candy", function(player, arg)--This adds the command
	CandyMode(player, arg)--Tells command what function to do
end)
COM_AddCommand("h_candy2", function(player, arg)--This adds the command
	if(players[1])
		CandyMode(players[1], arg)
	end
end)



//The Command Shit
local function HairColor(player, arg)--Conjunction junction, what's your function?
	if player.mo and player.mo.valid
		if player.haircolor == nil
			player.haircolor = "string"
		end
		if arg
			--if arg == "on" or arg == "1" or arg == "true" or arg == "yes"--for the dummy these are numbers, but they can be strings, but you'll have to call the read differently
				if R_GetColorByName(arg) 
				and (R_GetColorByName(arg) != SKINCOLOR_NONE)
					player.haircolor = arg
				else
					if arg == "off" or arg == "false" or arg == "0" or arg == "no"
						player.haircolor = "none"
					end
				end
				if R_GetColorByName(arg)
					CONS_Printf(player, "Hair-color set to " .. player.haircolor .. ".")
				else
					CONS_Printf(player, "This is not a Valid color. Reverting to Default.")
				end
				if io and player == consoleplayer--this section calls to save the data
					local file = io.openlocal(HAIRCOLOR, "w+")--w+ neans to write and overwrite all data in the file.
					file:write(player.haircolor)--put that value in the file, idfk how to make boolean functions save so i force put "true" or "false" here cuz im shit
					file:close()--perish
				end
			--end
		else
			CONS_Printf(player, "Sets Honey's hair color. Can either be set to a valid skincolor, or to off in order to remove your hair dye.")
		end
	else
		CONS_Printf(player, "You can only use this in a level!")
	end
end

COM_AddCommand("h_haircolor", function(player, arg)--This adds the command
	HairColor(player, arg)--Tells command what function to do
end)
COM_AddCommand("h_haircolor2", function(player, arg)--This adds the command
	if(players[1])
		HairColor(players[1], arg)--Tells command what function to do
	end
end)
/*addHook("PlayerThink", function(p)
	if p.mo and p.mo.health and p.mo.valid
		if R_GetColorByName(p.haircolor) and R_GetColorByName(p.haircolor) != SKINCOLOR_NONE
			--print(p.haircolor)
		end
	end
end)*/

//#
local h_allowhairdye = false
//#

addHook("NetVars", function(network)
	h_allowhairdye = network($)
end)

local function c_h_allowhairdye(p, a)
	if p.mo and p.mo.valid
		if a != nil
			if a == "true"
			or a == "yes"
			or a == "1"
			or a == "on"
				h_allowhairdye = true
				for pl in players.iterate
					pl.nohairdye = false
				end
				print("Honey can now dye her hair.")
			end
			if a == "false"
			or a == "no"
			or a == "0"
			or a == "off"
				h_allowhairdye = false
				for pl in players.iterate
					pl.nohairdye = true
				end
				print("Honey can no longer dye her hair.")
			end
		else
			CONS_Printf(p, "Disallows Honey players from changing their hair color. Currently ".. tostring(h_allowhairdye))
		end
	else
		CONS_Printf(p, "You can only use this in a level!")
	end
end

COM_AddCommand("h_allowhairdye", c_h_allowhairdye, COM_ADMIN)