local utils = require "lib.utils.utils"

--- @class Level
--- @field size Vec3
--- @field characters Character[]
--- @field characterGrid CharacterGrid
--- @field selector Selector
--- @field camera Camera
--- @field tileGrid TileGrid
--- @field turnOrder TurnOrder
local level = {}
level.__index = level

local path = nil

--- @param type "procedural"|"handmaded"
--- @param template Procedural|Handmaded
local function new(type, template)
    local size = Vec3 { 30, 30 } -- magic numbers for testing purposes only
    print(type, template, size)
    return setmetatable({
        size = size,
        characters = {},
        characterGrid = (require "lib.level.grid.character_grid").new(),
        tileGrid = (require "lib.level.grid.tile_grid").new(type, template, size),
        selector = (require "lib.level.selector").new(),
        camera = (require "lib.level.camera").new(),
        turnOrder = (require "lib.level.turn_order").new(),
    }, level)
end

function level:update(dt)
    self.characterGrid:reload()
    utils.each(self.characters, function(el)
        el:update(dt)
    end)

    self.camera:update(dt)
    self.selector:update(dt)
end

function level:draw()
    self.tileGrid:draw()
    while not self.characterGrid.yOrderQueue:is_empty() do -- по сути это сортировка кучей за n log n
        self.characterGrid.yOrderQueue:pop():draw()
    end
end

return {
    new = new
}
