-- From CSG2, It works nicer than G1, damn the lack of conversion packs.

-- All table values here, just for safety

-- Account Vars, probably more later
-- DO NOT put table references in this.
-- ALWAYS use all lower case
CAKE.AccountVars = {};
CAKE.AccountVars["tooltrust"] = "0";

-- Give them defaults for when they don't load
CAKE.CharVars = {};
CAKE.CharVars["info.job"] = "Problem";
CAKE.CharVars["rpname"] = "Error";
CAKE.CharVars["money"] = "0";
CAKE.CharVars["faction"] = "";
CAKE.CharVars["rank"] = "";
--CAKE.CharVars["info.cid"] = "0";

-- This formats a player's SteamID for things such as data file names
-- For example, STEAM_0:1:5947214 would turn into 015947214
function CAKE.FormatSteamID( SteamID )

	local SteamID = CAKE.NilFix(SteamID, "STEAM_0:0:0");

	s = string.gsub( SteamID,"STEAM","" );
	s = string.gsub( s,":","" );
	s = string.gsub( s,"_","" );
	
	return s;
	
end

-- Load a player's data
function CAKE.LoadPlayerDataFile( ply )

Msg("Loading ply: " .. ply:Name() .. "\n");

	local SteamID = CAKE.FormatSteamID( ply:SteamID( ) );
	
	CharTable[ SteamID ]  = {  };
	
	if( file.Exists( "CakeScript/PlayerData/" .. CAKE.FormatSteamID( ply:SteamID() ) .. ".txt" ) ) then
	
Msg("file exists for player.\n");

		--CAKE.CallHook( "LoadPlayerDataFile", ply );
		
		-- CAKE.DayLog( "script.txt", "Loading player data file for " .. ply:SteamID( ) );
		
		-- Read the data from their data file
		-- make SURE we get it since it exists
		local Data_Raw = ""
		while ( Data_Raw == "" ) do
			Data_Raw = file.Read( "CakeScript/PlayerData/" .. CAKE.FormatSteamID( ply:SteamID() ) .. ".txt" );
		end
		
--Msg("The data is: ");
--Msg(util.TableToKeyValues( Data_Raw ));
--Msg("\n");
		
		-- Convert the data into a table
		local Data_Table = CAKE.NilFix(util.KeyValuesToTable( Data_Raw ), { });
		
		-- Insert the table into the data table
		CharTable[ SteamID ] = Data_Table;
		
		-- Retrieve the data table for easier access
		local PlayerTable = CharTable[ SteamID ];
		
		-- This will check and set default account items
		for field, default in pairs( CAKE.AccountVars ) do
		
			if( PlayerTable[field] == nil ) then
				CAKE.DayLog( "script.txt", "Missing character data field '" .. tostring( field ) .. "' in character " .. ply:SteamID( ) .. "- inserting with default value of '" .. tostring(default) .. "'" );
				CharTable[ SteamID ][ field ] = default;
			end
		
		end
		
			-- If any fields were not loaded and they are in the DataFields table, add them.
		for _, char in pairs( PlayerTable ) do
			
			-- make sure we have a character
			-- used in place of: tonumber(char) != nil
			if( type(char) == "table" ) then
			
				for fieldname, default in pairs( CAKE.CharVars ) do
			
					if( char[ fieldname ] == nil ) then
				
						Msg( "Missing character data field '" .. tostring( fieldname ) .. "' in character " .. ply:SteamID( ) .. " char #: " .. _ .. ", inserting with default value of '" .. tostring(default) .. "'\n" );
					
					end
				
				end
				
			end
			
		end
		
		-- If any fields were not loaded and they are in the DataFields table, add them.
		for _, char in pairs( PlayerTable ) do
			
			-- make sure we have a character
			-- used in place of: tonumber(char) != nil
			if( type(char) == "table" ) then
			
				for fieldname, default in pairs( CAKE.CharVars ) do
			
					if( char[ fieldname ] == nil ) then
				
						CAKE.DayLog( "script.txt", "Missing character data field '" .. tostring( fieldname ) .. "' in character " .. ply:SteamID( ) .. " char #: " .. _ .. ", inserting with default value of '" .. tostring(default) .. "'" );
						PlayerTable[ _ ][ fieldname ] = default;
					
					end
				
				end
				
			end
			
		end
		
		for kchar,char in pairs(PlayerTable) do
			if(type(char) == "table" and char["inventory"] ~= nil) then
				local newinv = {};
				for k,v in pairs(char["inventory"]) do
					newinv[tonumber(k)] = v;
				end
				PlayerTable[kchar]["inventory"] = newinv;
			end
		end
		
		CAKE.SavePlayerData(ply);
		
		--CAKE.CallHook( "LoadedPlayerDataFile", ply, Data_Table );
		
	else
	
Msg("file does not exist.\n");
		
		-- Seems they don't have a player table. Let's create a default one for them.
		
		CAKE.DayLog( "script.txt", "Creating new data file for " .. ply:SteamID( ) );
		
		CharTable[ SteamID ] = {  };
		
		-- Let's get the default fields and add them to the table.
		for fieldname, default in pairs( CAKE.AccountVars ) do
			
			if( CharTable[ fieldname ] == nil ) then
			
				CharTable[ SteamID ][ fieldname ] = default;
				
			end
			
		end
		
		-- We won't make a character, obviously. That is done later.
		
		CAKE.SavePlayerData(ply);
		
		-- Technically, we didn't load it, but the data is now there.
		-- CAKE.CallHook( "LoadedPlayerDataFile", ply, Data_Table );
		
	end
	
end

function CAKE.SavePlayerData( ply )

	local keys = util.TableToKeyValues( CharTable[ CAKE.FormatSteamID( ply:SteamID() ) ] );
	file.Write( "CakeScript/PlayerData/" .. CAKE.FormatSteamID( ply:SteamID() ) .. ".txt" , keys);
	
end