local format = string.format

local classic = require("lib.classic.classic")

---@class Cache : lib.classic.class
---@field private fn function
---@field cache table
---@field count integer
---@field name string
local cache = classic:extend()
cache._VERSION = "0.0.0-alpha"
cache._DESCRIPTION = "A simple and straightforward cache made for LÖVE."
cache._URL = "https://github.com/FNicon/reoof"
cache._LICENSE = [[
  MIT License

  Copyright (c) 2025 FNicon

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
]]

local function default_empty()
end

--- create entity
---@param fn? function lambda to load resource
---@param name? string for debug purposes
-- ---@return Cache self
function cache:new(fn, name)
  -- local self = setmetatable({}, cache)
  self.fn = fn or default_empty
  self.cache = {}
  self.count = 0
  self.name = name or "cache"
end

--- load resource to cache
---@param self Cache
---@param key string assigned_key
---@param ... any
---@return any resource
function cache:load_to(key, ...)
  if (self.cache[key] == nil) then
    self.cache[key] = self.fn(...)
    self.count = self.count + 1
    return self.cache[key]
  else
    return self:load_from(key)
  end
end


--- load resource from cache
---@param self Cache
---@param key string assigned_key
---@return any resource
function cache:load_from(key)
  if (self.cache[key] == nil) then
    print(format("WARNING : key %s not found"), key)
  else
    return self.cache[key]
  end
end

--- release object and set it up to nil
---@param self Cache
function cache:release()
  for k, _ in pairs(self.cache) do
    self:release(k)
  end
  self.cache = nil
  self.count = nil
  self.name = nil
  self.fn = nil
  setmetatable(self, nil)
  self = nil
end

--- release object and set it up to nil
---@param self Cache
---@param key string
function cache:release_from(key)
  if (self.cache[key]) then
    if (self.cache[key].release) then
      self.cache[key]:release()
      self.cache[key] = nil
      self.count = self.count - 1
    else
      print(format("WARNING : during release, %s don't have release function"), key)
    end
  else
    error(format("ERROR : during release, %s not found", key))
  end
end

return cache
