// Let's create the variable we need

Promotion_Interval = 0.1 // Time in hours
Warning_Penalty = 30 // Time in minutes 
// Place the groups you want promoted here, in decending order(Highest group to Lowest)
groups_to_promote = 
{ 
	[1] = "superadmin", 
	[2] = "admin", 
	[3] = "operator", 
	[4] = "user", 
}
// Place what you want each group promoted to
/*
Groups you don't want to be promoted from, just make it eqal to "None" (Case Sensitive)
If the group is equal to 1, then "None" is not needed.
Example below: Once the user is promoted to operator, or admin via this script or by an
other admin. They will  not be promoted automatically any further.
*/
promote_to = 
{ 
	["user"] = "operator",
	["operator"] = "None", 
	["admin"] = "None", 
	["superadmin"] = "HighestLevel", 
}
// Do you have Umotd?
HasUmotd = false




//===================================END OF CONFIGURATION====================================//

players = { ["players"] = {}}
player_promotions = {}
// Create the file if it doesn't exist
if SERVER then
	if !file.Exists("player_times.txt") then
		players = { players = {}} 
		file.Write( "player_times.txt", ULib.makeKeyValues(players))
	else
		players = ULib.parseKeyValues ( file.Read("player_times.txt"))
	end
	if !file.Exists("player_promotions.txt") then
		player_promotions = { promotions = {} }
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
	else
		player_promotions = ULib.parseKeyValues ( file.Read("player_promotions.txt"))
		for k,v in pairs( player_promotions.promotions ) do
			player_promotions.promotions[tonumber(k)]=v
		end
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
	end	
end
//timer.Create( "UpdatePromotionsFile", 60, 0, Promotions_Update )
function promotions_init( ply )
	if !(tobool(players.players[ply:Nick()])) then 
		players.players[ply:Nick()] = { time = 0, promote = "none", lvl = 1 } 
		file.Write( "player_times.txt", ULib.makeKeyValues(players))
	end
		timer.Create(ply:Nick().. "GroupCheck", 1, 0, CheckGroup, ply)
		timer.Create(ply:Nick().. "PromotionTimer", 60, 0, SetTime, ply)
		for k, v in ipairs( groups_to_promote ) do
			if ply:IsUserGroup( v ) then
			ply.grouplvl = k
			ply.promote = promote_to[v]
			file.Write( "player_times.txt", ULib.makeKeyValues(players))
			return
			end
		end
end
hook.Add( "PlayerInitialSpawn", "Promotions_Init", promotions_init)

function SetTime( ply )
	players.players[ply:Nick()].promote = ply.promote 
	players.players[ply:Nick()].lvl = ply.grouplvl
	if ply.warn == 0 then
		players.players[ply:Nick()].time = players.players[ply:Nick()].time + 1
		file.Write( "player_times.txt", ULib.makeKeyValues(players))
		if (players.players[ply:Nick()].time > (Promotion_Interval * 60 * (((table.maxn(groups_to_promote)) - ply.grouplvl + 1)))) then
			Promote( ply )
		end
	else
		if ply.warn >= 5 then
			if !(players.players[ply:Nick()].time - (ply.warn * 10) < 0) then
				players.players[ply:Nick()].time = players.players[ply:Nick()].time - (ply.warn * 10)
				file.Write( "player_times.txt", ULib.makeKeyValues(players))
			else
				players.players[ply:Nick()].time = 0
				file.Write( "player_times.txt", ULib.makeKeyValues(players))
			end
		end
	end
end
function Promote( ply )
	if !(ply.grouplvl == 1) and !(players.players[ply:Nick()].promote == "None") then
		players.players[ply:Nick()].time = 0
		//local topromote = string.Explode( " ", ply:Nick())
		//local topromote = string.sub( topromote[1], -math.abs((string.len( topromote[1] )/2)))
		local topromote = string.format( "%q", ply:Nick() )
		Msg( "ulx adduser " ..topromote.. " " ..players.players[ply:Nick()].promote.. "\n" )
//		ULib.ucl.addUser(ply:Nick(), "steamid", ply:SteamID(), ply.promotegroup)
		game.ConsoleCommand( "ulx adduserid " ..topromote.. " " ..players.players[ply:Nick()].promote.. " " ..ply:SteamID().. "\n")
		game.ConsoleCommand( "ulx csay Player: " ..ply:Nick().. " has been promoted to " ..players.players[ply:Nick()].promote.. " for exceptional behaviour \n")
		table.insert( player_promotions.promotions,"<li> Player: " ..ply:Nick().. " has been promoted to " ..players.players[ply:Nick()].promote.. " for exceptional behaviour" )
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
		ply.grouplvl = ply.grouplvl - 1
		ply.promote = promote_to[groups_to_promote[ply.grouplvl]]
		players.players[ply:Nick()].promote = ply.promote
		players.players[ply:Nick()].lvl = ply.grouplvl
		file.Write( "player_times.txt", ULib.makeKeyValues(players))
	end
end

function RemoveTimers( ply )
	if timer.IsTimer( ply:Nick().. "PromotionTimer" ) then
		timer.Destroy( ply:Nick().. "PromotionTimer" )
	end
	if timer.IsTimer( ply:Nick().. "GroupCheck" ) then
		timer.Destroy( ply:Nick().. "GroupCheck" )
	end
end
hook.Add( "PlayerDisconnected", "RemoveTimersHook", RemoveTimers )
function Player_Promotes()	
	local LimitTable = table.Copy(player_promotions)
	while table.Count(LimitTable.promotions) > 3 do
		table.remove( LimitTable.promotions, 1)
	end
	if table.Count(LimitTable.promotions) <= 3 then
		promotesout = table.concat( LimitTable.promotions, " ")
		if string.find( promotesout, "<ol>", 1, true) then
			string.gsub( promotesout, "<ol>", " ")
			file.Write( "latestpromotes.txt", "<ol> " ..promotesout.. " </ol>")
			return promotesout
		else
			file.Write( "latestpromotes.txt", "<ol> " ..promotesout.. " </ol>")
			return promotesout
		end
	end
end
timer.Create("Player_Promotes_Timer", 1, 0, Player_Promotes)

function CheckGroup( ply )
	for k, v in ipairs(groups_to_promote) do
		if ply:IsUserGroup( v ) then
		ply.grouplvl = k
		ply.promote = promote_to[v]
		return
		end
	end
end	


function Promotions_Update()
	if !file.Exists("player_promotions.txt") then
		player_promotions = { promotions = {} }
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
	else
		player_promotions = ULib.parseKeyValues ( file.Read("player_promotions.txt"))
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
	end
end



		



