local meta = FindMetaTable( "Player" );

function meta:HandleUID()

	local query = "SELECT * FROM `STALKERUsers` WHERE `SteamID` = '" .. self:SteamID() .. "'";
	
	local user = sql.Query( query )[1]

	self:SetField( "uid", user["uid"] )
	
	if( string.find( user["HasToolTrust"], "advanced" ) ) then
	
		self:Give( "gmod_tool" );
		self:GetTable().AdvancedTT = true;
		self:SetField( "group_max_props", 20 );
		
		end

	if( string.find( user["CanDupe"], "yes" ) ) then
	
		self:GetTable().CanUseDuplicator = true;
		
	end	
	
	self:SetPlayerFactionFlags( user["FactionFlags"] )
	
	print( "Handled UID Information" )
	
	
end

function meta:SaveContainers()

	local cmd = "UPDATE `STALKERChars` SET `containers` = '";
	
	for n = 1, TS.MaxInventories do
	
		local cont = self:GetTable().Inventories[n].Name or ""
		--fields["containers"] = fields["containers"] .. cont .. ";";
		cmd = cmd .. cont .. ";"
		
	end	
	
	cmd = cmd .. "' WHERE SteamID = '" .. self:SteamID() .. "' AND id = '" .. self:GetField( "charid" ) .. "';";
	
	sql.Query( cmd );
	
end	

function meta:SaveItems()
	
	local cmd = "UPDATE `STALKERChars` SET `Items` = '";
	
	for i, v in pairs( self:GetTable().InventoryGrid ) do
	
		for x, c in pairs( v ) do
		
			for y, u in pairs( c ) do
				
				if( self:GetTable().InventoryGrid[i][x][y].ItemData ) then
				
				local item = self:GetTable().InventoryGrid[i][x][y].ItemData.ID
				
				cmd = cmd .. item .. ";"
				
				end
				
			end
			
		end

	end

	cmd = cmd .. "' WHERE SteamID = '" .. self:SteamID() .. "' AND id = '" .. self:GetField( "charid" ) .. "';";
	
	sql.Query( cmd );
	
end	

function meta:SaveCharacter()

	local cmd = "INSERT INTO `STALKERChars` ";
	
	local fields = { }
	fields["RPName"] = self:GetNWString( "RPName" )
	fields["Model"] = self:GetNWString( "PlayerModel" )
	fields["SteamID"] = self:SteamID()
	fields["Rubles"] = self:GetPlayerRubles()
	fields["Title"] = self:GetNWString( "title" )
	fields["SecondTitle"] = self:GetNWString( "secondtitle" )
	fields["Age"] = self:GetField( "CCAge" )
	fields["STATAim"] = self:GetField( "CCAim" )
	fields["STATStrength"] = self:GetField( "CCStrength" )
	fields["STATSpeed"] = self:GetField( "CCSpeed" )
	fields["STATEndurance"] = self:GetField( "CCEndurance" )

	cmd = cmd .. "( ";

	local comma = false;

	for k, v in pairs( fields ) do
	
		if( comma ) then
			cmd = cmd .. ", ";
		end
	
		cmd = cmd .. k;
		comma = true;
	
	end
	
	cmd = cmd .. ") VALUES ( ";
	
	local comma = false;
	
	for k, v in pairs( fields ) do
	
		if( comma ) then
			cmd = cmd .. ", ";
		end
	
		cmd = cmd .. "'" .. v .. "'";
		comma = true;
	
	end	

	cmd = cmd .. "); ";

	sql.Query( cmd );
	
	local id = sql.Query( "SELECT id FROM STALKERChars WHERE SteamID = '" .. self:SteamID() .. "' AND RPName = '" .. self:GetRPName() .. "'" )[1]["id"];
 	id = tonumber( id );
 	
 	self:SetField( "charid", id );
 	
end

function meta:UpdateCharacter()

	local cmd = "UPDATE `STALKERChars` SET ";
	
	local fields = { }
	fields["Rubles"] = self:GetPlayerRubles()
	fields["Title"] = self:GetNWString( "title" )
	fields["SecondTitle"] = self:GetNWString( "secondtitle" )

	cmd = cmd .. "( ";
	local comma = false;

	for k, v in pairs( fields ) do
	
		if( comma ) then
			cmd = cmd .. ", ";
		end
	
		cmd = cmd .. k .. " = '" .. v .. "'";
		comma = true;
		
	end
	
	cmd = cmd .. " WHERE SteamID = '" .. self:SteamID() .. "' AND id = '" .. self:GetField( "charid" ) .. "';";
	sql.Query( cmd );

 	self:SaveItems()
	self:SaveContainers()
	
end

	
function meta:GetNumCharacters( steamid )

	local list = sql.Query( "SELECT * FROM STALKERChars WHERE SteamID = '" .. steamid .. "'" )
		
	if( !list ) then
		return 0
	end	

	return #list
	
end	

function meta:GetCharacterList()

	local cmd = "SELECT id FROM `STALKERChars` WHERE SteamID = '" .. self:SteamID() .. "'";

	return doQuery( cmd );

end

function meta:HasCharacter( name )

	local result = sql.Query( "SELECT id FROM `STALKERChars` WHERE SteamID = '" .. self:SteamID() .. "' AND RPName = '" .. name .. "'" )
	
	if( !result ) then 
		return false 
	end
	
	return true

end

function meta:SaveExists( nick )

	local query = "SELECT `id` FROM `STALKERChars` WHERE `id` = '" .. self:GetField( "charid" ) .. "' AND `RPName` = '" .. nick .. "'";
	
	if( query ) then
	
		return true
		
	end	

end

function meta:SaveData( name, val )

	local cmd = "UPDATE `STALKERChars` SET " .. name .. " = '" .. val .. "' WHERE SteamID = '" .. self:SteamID() .. "' AND id = '" .. self:GetField( "charid" ) .. "';";
	
	sql.Query( cmd );	

end		


function meta:LoadCharacter( id )

	local query = "SELECT * FROM `STALKERChars` WHERE id = '" .. id .. "'";

	local character = sql.Query( query )[1];
	
	self:SetPlayerStrength( tonumber( character["STATStrength"] ) )
	self:SetPlayerSpeed( tonumber( character["STATSpeed"] ) )
	self:SetPlayerEndurance( tonumber( character["STATEndurance"] ) )
	self:SetPlayerAim( tonumber( character["STATAim"] ) )
	self:SetPlayerAge( tonumber( character["Age"] ) )	
	
	--we do this because when CharacterInitialize() is called it resets their data 
	local function SetField()
	
		self:SetNWString( "title", tostring( character["Title"] ) )
		self:SetNWString( "secondtitle", tostring( character["SecondTitle"] ) )
		self:GetTable().CitizenModel = character["Model"]
		self:GetTable().CCModel = character["Model"]
		self:SetModel( self:GetTable().CCModel )

	end	
	
	timer.Simple( 5, function() SetField() end );
	
	local function SetField2()

		self:SetPlayerRubles( tonumber( character["Rubles"] ) )
		
		local containers = string.Explode( ";", character["containers"] );
	
		for k, v in pairs( containers ) do	
	
		    if( v and v ~= "" and v ~= "Old Jacket" ) then	
				
				if v == "Advanced PMC Uniform" then		
				
					self:WearItem( "clothes_pmc_1_sg" );
					
				end
				if v == "Basic PMC Uniform" then		
				
					self:WearItem( "clothes_pmc_1_s" );
					
				end
				if v == "Light PMC Uniform" then		
				
					self:WearItem( "clothes_pmc_1_sp" );
					
				end
			end
			
		end
		
		local items = string.Explode( ";", character["Items"] );
			
			
		for k, v in pairs( items ) do
		
		    if( v and v ~= "" ) then		
			
				local w = TS.ItemsData[v].Width;
				local h = TS.ItemsData[v].Height;
				
				if( self:IsCitizen() ) then
				
					if( v ~= "crradio" ) then

						self:ConCommand( "eng_givesaveditem " .. v .. " " .. w .. " " .. h );					

					end
					
				else
				
					if( not string.find( v, "ts2_" ) and v ~= "radio" ) then
									
						self:ConCommand( "eng_givesaveditem " .. v .. " " .. w .. " " .. h );
					
					end	
					
				end
				
			end
			
		end
		
	end

	timer.Simple( 6, function() SetField2() end )	
		
	self:SetNWString( "RPName", character["RPName"] )
	self:SetField( "charid", id );
	
	
end	
	
	