local function draw_map_tag(surface, force, position, name)
    local t = force.add_chart_tag(surface, {icon = {type = 'item', name = 'heavy-armor'}, position = position, text = '   '})
    if t and name then
        t.last_user = name
    end
end

local function is_tag_valid(tag)
    if not tag.icon then
        return
    end
    if tag.icon.type ~= 'item' then
        return
    end
    if tag.icon.name ~= 'heavy-armor' then
        return
    end
    if tag.text ~= '   ' then
        return
    end
    return true
end

local function get_corpse_force(corpse)
    if corpse.character_corpse_player_index then
        if game.players[corpse.character_corpse_player_index] then
            return game.players[corpse.character_corpse_player_index].force
        end
    end
    return game.forces.neutral
end

local function destroy_all_tags()
    for _, force in pairs(game.forces) do
        for _, surface in pairs(game.surfaces) do
            for _, tag in pairs(force.find_chart_tags(surface)) do
                if is_tag_valid(tag) then
                    tag.destroy()
                end
            end
        end
    end
end

local function redraw_all_tags()
    for _, surface in pairs(game.surfaces) do
        for _, corpse in pairs(surface.find_entities_filtered({name = 'character-corpse'})) do
            if corpse.character_corpse_player_index then
                local player = game.get_player(corpse.character_corpse_player_index)
                if player and player.valid then
                    draw_map_tag(corpse.surface, get_corpse_force(corpse), corpse.position, player.name)
                end
            else
                draw_map_tag(corpse.surface, get_corpse_force(corpse), corpse.position)
            end
        end
    end
end

local function find_and_destroy_tag(corpse)
    local force = get_corpse_force(corpse)
    for _, tag in pairs(force.find_chart_tags(corpse.surface, {{corpse.position.x - 0.1, corpse.position.y - 0.1}, {corpse.position.x + 0.1, corpse.position.y + 0.1}})) do
        if is_tag_valid(tag) then
            tag.destroy()
            return true
        end
    end
    return false
end

local function on_player_died(event)
    local player = game.players[event.player_index]
    draw_map_tag(player.surface, player.force, player.position, player.name)
end

local function on_character_corpse_expired(event)
    if find_and_destroy_tag(event.corpse) then
        return
    end
    destroy_all_tags()
    redraw_all_tags()
end

local function on_pre_player_mined_item(event)
    if event.entity.name ~= 'character-corpse' then
        return
    end
    if find_and_destroy_tag(event.entity) then
        return
    end
    destroy_all_tags()
    redraw_all_tags()
end

local event = require 'utils.event'
event.add(defines.events.on_player_died, on_player_died)
event.add(defines.events.on_character_corpse_expired, on_character_corpse_expired)
event.add(defines.events.on_pre_player_mined_item, on_pre_player_mined_item)
