
local objects = require 'objects'

water_data = serialize.define({
    {"object_idx", "u14"},
    {"water_level", "u16"}
})

function on_exec(room, method, data)
    if method == "update_water_level" then
        local data = water_data.decode(data)
        room.objects[data.object_idx].properties.water_level = data.water_level
    end
end

function tick_plants(room)
    -- TODO: Do the syncing on request instead of all the time
    --       for performance
    local sync_state = room.sync_state
    sync_state.plants = {}
    for idx, obj in ipairs(room.objects) do
        if objects.get_properties(obj.key).plant then
            obj.properties['water_level'] = (obj.properties['water_level'] or 1200) - 1
            sync_state.plants[idx] = obj.properties['water_level']
        end
    end
    room.sync_state = sync_state
end

function tick_plants_client(room)
    if not room.properties.has_synced_plants then
        room.properties.has_synced_plants = true
        local sync_state = room.sync_state
        if sync_state.plants ~= nil then
            for idx, obj in ipairs(room.objects) do
                if objects.get_properties(obj.key).plant then
                    if sync_state.plants[idx] ~= nil then
                        obj.properties['water_level'] = sync_state.plants[idx]
                    end
                end
            end
        end
    end
    for idx, obj in ipairs(room.objects) do
        local props = objects.get_properties(obj.key)
        if props.plant then
            obj.properties['water_level'] = (obj.properties['water_level'] or 1200) - 1
            if props.plant_textures ~= nil then
                local lvl = obj.properties['water_level']
                if lvl < 20 * 30 then
                    -- Dying
                    obj.entities[1].model_texture = props.plant_textures[3]
                elseif lvl < 20 * 60 * 2 then
                    -- Drying out
                    obj.entities[1].model_texture = props.plant_textures[2]
                else
                    -- Fresh
                    obj.entities[1].model_texture = props.plant_textures[1]
                end
            end
        end
    end
end

function save_plants(room)
    local idx = 1
    local data = {}
    for _, obj in ipairs(room.objects) do
        if objects.get_properties(obj.key).plant then
            data[idx] = obj.properties['water_level'] or 1200
            idx = idx + 1
        end
    end
    return data
end

function load_plants(room, data)
    local idx = 1
    for _, obj in ipairs(room.objects) do
        if objects.get_properties(obj.key).plant then
            obj.properties['water_level'] = data[idx]
            idx = idx + 1
        end
    end
end


objects.assign_properties("base:plants/simple", {
    plant = true,
    plant_textures = {
        "base:models/plants/simple",
        "base:models/plants/simple_dry",
        "base:models/plants/simple_dying"
    },
    happiness = 0.05,
    happiness_dropoff = 0.2,
})
objects.assign_properties("base:plants/flowers", {
    plant = true,
    plant_textures = {
        "base:models/plants/simple",
        "base:models/plants/simple_dry",
        "base:models/plants/simple_dying"
    },
    happiness = 0.05,
    happiness_dropoff = 0.2,
})
objects.assign_properties("base:plants/small_tree", {
    plant = true,
    plant_textures = {
        "base:models/plants/simple",
        "base:models/plants/simple_dry",
        "base:models/plants/simple_dying"
    },
    happiness = 0.05,
    happiness_dropoff = 0.2,
})
objects.assign_properties("base:plants/brickpot", {
    plant = true,
    plant_textures = {
        "base:models/plants/brickpot",
        "base:models/plants/brickpot_dry",
        "base:models/plants/brickpot_dying"
    },
    happiness = 0.05,
    happiness_dropoff = 0.2,
})
objects.assign_properties("base:plants/woodpot", {
    plant = true,
    plant_textures = {
        "base:models/plants/woodpot",
        "base:models/plants/woodpot_dry",
        "base:models/plants/woodpot_dying"
    },
    happiness = 0.05,
    happiness_dropoff = 0.2,
})
objects.assign_properties("base:plants/bush", {
    plant = true,
    plant_textures = {
        "base:models/garden/bush",
        "base:models/garden/bush_dry",
        "base:models/garden/bush_dying"
    },
    happiness = 0.05,
    happiness_dropoff = 0.2,
})
objects.assign_properties("base:plants/bush_corner", {
    plant = true,
    plant_textures = {
        "base:models/garden/bush",
        "base:models/garden/bush_dry",
        "base:models/garden/bush_dying"
    },
    happiness = 0.05,
    happiness_dropoff = 0.2,
})