-- lua clientside functions to save personal settings
-- most options will be contained entirely clientside
-- author: robo

clientSettings = {};

-- to prevent nils
-- remember always use STRINGS so that they save, other value types will not go out to text without converting.
-- remember everything goes to lowercase
local defaultSettings = {};
defaultSettings["healthregen"] = "1"; -- 1 = regen, 0 = do not. Sent using concommands. Server uses opposite for convienience ( 0 = on, else = off );
defaultSettings["paperdoll"] = "1"; -- 1 = enabled, 0 = disabled
--defaultSettings["fontsize"] = "14";
--defaultSettings["chatframefont"] = "ChatFont";

-- will send neccessary items to the server that need to be loaded asap
function transmitSettings()

	-- transmit
	RunConsoleCommand("rp_setRegen", defaultSettings["healthRegen"], "\n");

end

function CLIENTLoadSettings()
	
	clientSettings  = {  };
	
	if( file.Exists( "PhoenixScript/PlayerSettings/settings.txt" ) ) then
		
		-- Read the data from their settings file
		-- make SURE we get it since it exists
		local Data_Raw = ""
		while ( Data_Raw == "" ) do
			Data_Raw = file.Read( "PhoenixScript/PlayerSettings/settings.txt" );
		end
		
		-- Insert the table into the data table
		clientSettings = util.KeyValuesToTable( Data_Raw );
		
		-- This will check and set default settings all have valid entries
		for field, default in pairs( defaultSettings ) do
		
			if( clientSettings[field] == nil ) then
				Msg( "Missing data field '" .. tostring( field ) .. " - inserting with default value of '" .. tostring(default) .. "\n" );
				clientSettings[field] = default;
			end
		
		end
		
		--CLIENT.SaveSettings();
		
	else
		
		clientSettings = {};
		
		-- Let's get the default fields and add them to the table.
		for fieldname, default in pairs( defaultSettings ) do
			
			if( clientSettings[fieldname] == nil ) then
			
				clientSettings[fieldname] = default;
				
			end
			
		end
		
		--CLIENT.SaveSettings(ply);
		
	end
	
	CLIENTSaveSettings(ply);
	transmitSettings();
	--ChatOverrideFont();
	
end

function CLIENTSaveSettings()

	local keys = util.TableToKeyValues( clientSettings );
	file.Write( "PhoenixScript/PlayerSettings/settings.txt" , keys);
	
end

function drawOptionsTab()

	-- we need playerMenu built first
	if( CAKE.PlayerMenu == nil ) then
		return;
	end
	
	local clientSettingsPanel = vgui.Create( "DPanelList" );
	clientSettingsPanel:SetPadding(20);
	clientSettingsPanel:SetSpacing(20);
	clientSettingsPanel:EnableHorizontal(false);
	clientSettingsPanel:EnableVerticalScrollbar(true);
	
	-- ADD OPTION BUTTONS HERE FOR EACH SETTING
	
	/*
	-- CHAT FRAME FONT OPTION --
	local chatTextOption = vgui.Create( "DButton" );
	chatTextOption:SetSize( 300, 50 );
	chatTextOption:SetText( clientSettings["chatframefont"] );
	chatTextOption.DoClick = function ( btn )
		local MenuButtonOptions = DermaMenu();
		MenuButtonOptions:AddOption( "DefaultBold", function()
														clientSettings["chatframefont"] = "DefaultBold";
														chatTextOption:SetText( "DefaultBold" );
														CLIENTSaveSettings();
													end );
		MenuButtonOptions:AddOption( "ChatFont", function()
														clientSettings["chatframefont"] = "ChatFont";
														chatTextOption:SetText( "ChatFont" );
														CLIENTSaveSettings();
													end );
		MenuButtonOptions:Open();
	end
	local chatTextOptionLabel = vgui.Create( "DLabel" );
	chatTextOptionLabel:SetText("Chat Font Option:");
	clientSettingsPanel:AddItem(chatTextOptionLabel);
	clientSettingsPanel:AddItem(chatTextOption);
	*/
	
	/*
	-- CHAT FONT SIZE OPTION --
	local fontSizeSlider = vgui.Create( "DNumSlider" );
	fontSizeSlider:SetSize( 300, 50 );
	fontSizeSlider:SetText("Font Size (must reconnect)");
	fontSizeSlider:SetMin(1);
	fontSizeSlider:SetMax(128);
	fontSizeSlider:SetDecimals(0);
	fontSizeSlider:SetValue( tonumber( clientSettings["fontsize"] ) );
	fontSizeSlider.OnValueChanged = function( val )
										clientSettings["fontsize"] = tostring( fontSizeSlider:GetValue() );
										CLIENTSaveSettings();
										--ChatOverrideFont();
									end
	clientSettingsPanel:AddItem(fontSizeSlider);
	*/
	
	-- REGENERATION OPTION --
	local regenOption = vgui.Create( "DButton" );
	regenOption:SetSize( 300, 50 );
	--chatTextOption:SetPos( parentX*3/5, parentY / 5 ); -- don't need to set pos for a panelList item
	regenOption:SetText( clientSettings["healthregen"] );
	regenOption.DoClick = function ( btn )
		local MenuButtonOptions = DermaMenu();
		MenuButtonOptions:AddOption( "Regen On", function()
														clientSettings["healthregen"] = "1";
														RunConsoleCommand("rp_setRegen", "1", "\n");
														CLIENTSaveSettings();
													end );
		MenuButtonOptions:AddOption( "Regen Off", function()
														clientSettings["healthregen"] = "0";
														RunConsoleCommand("rp_setRegen", "0", "\n");
														CLIENTSaveSettings();
													end );
		MenuButtonOptions:Open();
	end
	local regenOptionLabel = vgui.Create( "DLabel" );
	regenOptionLabel:SetText("Regeneration:");
	clientSettingsPanel:AddItem(regenOptionLabel);
	clientSettingsPanel:AddItem(regenOption);

	-- PAPERDOLL OPTION --
	local dollOption = vgui.Create( "DButton" );
	dollOption:SetSize( 300, 50 );
	--chatTextOption:SetPos( parentX*3/5, parentY / 5 ); -- don't need to set pos for a panelList item
	dollOption:SetText( clientSettings["paperdoll"] );
	dollOption.DoClick = function ( btn )
		local MenuButtonOptions = DermaMenu();
		MenuButtonOptions:AddOption( "Enabled", function()
														clientSettings["paperdoll"] = "1";
														CLIENTSaveSettings();
													end );
		MenuButtonOptions:AddOption( "Disabled", function()
														clientSettings["paperdoll"] = "0";
														CLIENTSaveSettings();
													end );
		MenuButtonOptions:Open();
	end
	local dollOptionLabel = vgui.Create( "DLabel" );
	dollOptionLabel:SetText("Paper doll:");
	clientSettingsPanel:AddItem(dollOptionLabel);
	clientSettingsPanel:AddItem(dollOption);

	CAKE.PropertySheet:AddSheet( "Settings", clientSettingsPanel, "gui/silkicons/magnifier", false, false, false);

end