local mod_name = minetest.get_current_modname()
local mod_path = minetest.get_modpath(mod_name)
local S = minetest.get_translator(mod_name)

local pl = {}

local function check_player(player)
    if not pl[player] then
        pl[player] = {
            zoom = 0.9,
        }
    end
end

local function remove_player(player)
    if pl[player] then
        aom_util.unset_fov(player, "aom_admin_tools:camera")
    end
    pl[player] = nil
end

local function zoom_player(player, relative)
    check_player(player)
    pl[player].zoom = pl[player].zoom * relative
    pl[player].zoom = math.min(math.max(pl[player].zoom, 0.05), 1.5)
    aom_util.set_fov(player, {
        tag = "aom_admin_tools:camera",
        fov = pl[player].zoom,
        is_multiplier = true,
        transition_time = 0.1
    })
end

minetest.register_tool("aom_admin_tools:camera", {
    description = S("Camera"),
    _tt_color = "#f0f",
    _tt_long_desc = S("Can change zoom freely."),
    _tt_how_to_use = S("Zoom in: [place]") .. "\n" .. S("Zoom out: [dig]"),
    inventory_image = "aom_admin_tools_camera.png",
    wield_scale = {
        x=0.5,y=0.5,z=0.5
    },
    wield_image = "aom_admin_tools_camera.png",
    groups = {},
    on_use = function(itemstack, user, pointed_thing)
        if not core.is_player(user) then return end
        zoom_player(user, 0.7)
    end,
    on_secondary_use = function(itemstack, user, pointed_thing)
        if not core.is_player(user) then return end
        zoom_player(user, 1.3)
    end,
    on_place = function(itemstack, user, pointed_thing)
        if not core.is_player(user) then return end
        zoom_player(user, 1.3)
    end,
    _on_select = function(itemstack, player)
        zoom_player(player, 1)
    end,
    _on_deselect = function(itemstack, player)
        remove_player(player)
    end,
})
