-- Stats

local stats = {
	{
		Name = "Wins",
		Type = "IntValue",
		DefaultValue = "0",
	},
}

function LeaderboardManager.new(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	for i, stat in pairs(stats) do
		local leaderstat = Instance.new(stat.Type)
		leaderstat.Name = stat.Name
		leaderstat.Value = stat.DefaultValue
		leaderstat.Parent = leaderstats
	end
end

function LeaderboardManager:getStat(player, stat)
	local leaderstats = player:FindFirstChild("leaderstats")
	if not leaderstats then return false end
	
	if leaderstats then
		return leaderstats:FindFirstChild(stat)
	end
end

function LeaderboardManager:setStat(player, stat, value)
	local stat = LeaderboardManager:getStat(player, stat)
	
	if stat then
		stat.Value = value 
	end
end

function LeaderboardManager:incrementStat(player, stat, value)
	if not value then value = 1 end
	
	local stat = LeaderboardManager:getStat(player, stat)
	
	if stat then
		stat.Value = stat.Value + value
	end
end

function LeaderboardManager:decrementStat(player, stat, value)
	if not value then value = 1 end
	
	local stat = LeaderboardManager:getStat(player, stat)
	
	if stat then
		stat.Value = stat.Value - value
	end
end

function LeaderboardManager:resetStat(player, stat)
	local stat = LeaderboardManager:getStat(player, stat)
	
	if stat then		
		for i, defaultStat in pairs(stats) do
			if stat == defaultStat.Name then
				stat.Value = defaultStat.DefaultValue	
				return
			end
		end
		
		stat.Value = 0
	end
end

function LeaderboardManager:resetStats(player)
	local leaderstats = player:FindFirstChild("CustomLeaderstats")
	if not leaderstats then return "No leaderstats found" end
		
	  for i, stat in pairs(leaderstats:GetChildren()) do
          LeaderboardManager:resetState(player, stat)
      end
end

return LeaderboardManager
