---@class Stack
---@field maxSize number
---@field values table<any>
aura_env.Stack = {
	---@param maxSize number
	---@return Stack
	new = function(maxSize)
		local self = setmetatable({}, {
			__index = aura_env.Stack,
		})
		self.maxSize = maxSize
		self.values = {}
		return self
	end,
	---@param self Stack
	---@param value number
	Push = function(self, value)
		table.insert(self.values, 0, value)
		if #self.values > self.maxSize then table.remove(self.values, #self.values) end
	end,
	---@param self Stack
	---@return number
	PerSecond = function(self)
		local first = self.values[#self.values]
		local last = time()

		if first and last then return #self.values / (math.max(last - first, 1)) end
		return 0
	end,
	---@param self Stack
	---@param threshold number?
	CleanOld = function(self, threshold)
		local timeThresh = time() - (threshold or aura_env.config.combatMemoryTime)
		-- print(string.format("Clean %d %d", #self.values, timeThresh))
		for i = #self.values, 1, -1 do
			if self.values[i] < timeThresh then table.remove(self.values, i) end
		end
		-- print(string.format("Clean after %d", #self.values))
	end,
}

aura_env.SoulShardStack = aura_env.Stack.new(aura_env.config.soulShardStackMaxSize)
aura_env.AgonyStack = aura_env.Stack.new(aura_env.config.agonyStackMaxSize)
aura_env.Display = ""
aura_env.SoulShards = 0
aura_env.PlayerName = UnitName("player")

aura_env.UpdateDisplay = function()
	local shards = string.format("%-8s %-7.2f/m", "Shards: ", aura_env.SoulShardStack:PerSecond() * 60)
	local agony = string.format("%-8s %-7.2f/m", "Agony: ", aura_env.AgonyStack:PerSecond() * 60)
	aura_env.Display = string.format("%s\n%s", shards, agony)
end
