local sit = require 'room.sit'
local plants = require 'objects.plants'

local common = require 'room.common'
encode_tooltip = common.encode_tooltip_def
decode_tooltip = common.decode_tooltip_def

function update(room)
    local width, height = room:get_size()
    local objects = room.objects
    local num_chairs = sit.seat_capacity(objects)

    local stand_areas = {}
    room.properties['stand_areas'] = stand_areas

    local ox, oy = room:get_position()
    local num_idle = 0
    for y = 0, height - 1 do
        for x = 0, width - 1 do
            local tile = level.get_tile(ox + x, oy + y)
            if tile.has_property('idle_area') then
                num_idle = num_idle + 1
                table.insert(stand_areas, {x = x, y = y})
            end
        end
    end

    room.capacity = num_idle * 0.5
    room.properties.can_sit = num_chairs > 0
end

function save(room)
    return {
        plants = plants.save_plants(room),
    }
end

function load(room, data)
    plants.load_plants(room, data.plants)
end

function force_entity_release(room, e)
end

function server(room)
    local objects = room.objects

    local stand_areas = room.properties['stand_areas']

    plants.tick_plants(room)
    for _, e in ipairs(room.owned) do
        e:release()
    end
    for _, e in ipairs(room.visitors) do
        e:release()
    end
end

function client(room, entities)
    plants.tick_plants_client(room)
end

function on_exec(room, method, data)
    plants.on_exec(room, method, data)
end