/*
 * SPDX-FileCopyrightText: 2021-2026 Sébastien Helleu <flashcode@flashtux.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

/* Bar items for typing plugin */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../weechat-plugin.h"
#include "typing.h"
#include "typing-bar-item.h"
#include "typing-config.h"
#include "typing-status.h"


/*
 * Callback used to build a string with the list of nicks typing on the buffer.
 */

void
typing_bar_item_nicks_map_cb (void *data,
                              struct t_hashtable *hashtable,
                              const void *key, const void *value)
{
    char **str_nicks_typing;
    const char *ptr_nick;
    struct t_typing_status *ptr_typing_status;

    /* make C compiler happy */
    (void) hashtable;

    str_nicks_typing = (char **)data;

    ptr_nick = (const char *)key;
    ptr_typing_status = (struct t_typing_status *)value;

    if ((ptr_typing_status->state == TYPING_STATUS_STATE_TYPING)
        || (ptr_typing_status->state == TYPING_STATUS_STATE_PAUSED))
    {
        if ((*str_nicks_typing)[0])
            weechat_string_dyn_concat (str_nicks_typing, ", ", -1);
        if (ptr_typing_status->state == TYPING_STATUS_STATE_PAUSED)
            weechat_string_dyn_concat (str_nicks_typing, "(", -1);
        weechat_string_dyn_concat (str_nicks_typing, ptr_nick, -1);
        if (ptr_typing_status->state == TYPING_STATUS_STATE_PAUSED)
            weechat_string_dyn_concat (str_nicks_typing, ")", -1);
    }
}

/*
 * Return content of bar item "typing": users currently typing on the buffer.
 */

char *
typing_bar_item_typing (const void *pointer, void *data,
                        struct t_gui_bar_item *item,
                        struct t_gui_window *window,
                        struct t_gui_buffer *buffer,
                        struct t_hashtable *extra_info)
{
    struct t_hashtable *ptr_nicks;
    char **str_nicks_typing, **str_typing, *str_typing_cut;
    const char *ptr_text;
    int max_length;

    /* make C compiler happy */
    (void) pointer;
    (void) data;
    (void) item;
    (void) window;
    (void) extra_info;

    if (!weechat_config_boolean (typing_config_look_enabled_nicks))
        return NULL;

    ptr_nicks = weechat_hashtable_get (typing_status_nicks, buffer);
    if (!ptr_nicks)
        return NULL;

    if (weechat_hashtable_get_integer (ptr_nicks, "items_count") == 0)
        return NULL;

    str_nicks_typing = weechat_string_dyn_alloc (128);
    weechat_hashtable_map (ptr_nicks,
                           &typing_bar_item_nicks_map_cb, str_nicks_typing);

    str_typing = weechat_string_dyn_alloc (256);
    ptr_text = weechat_config_string (typing_config_look_item_text);
    if (!ptr_text || !ptr_text[0])
    {
        /* TRANSLATORS: this text is displayed before the list of nicks typing in the bar item "typing", it must be as short as possible */
        ptr_text = _("Typing: ");
    }
    weechat_string_dyn_concat (str_typing, ptr_text, -1);
    weechat_string_dyn_concat (str_typing, *str_nicks_typing, -1);

    weechat_string_dyn_free (str_nicks_typing, 1);

    max_length = weechat_config_integer (typing_config_look_item_max_length);
    if (max_length == 0)
        return weechat_string_dyn_free (str_typing, 0);

    str_typing_cut = weechat_string_cut (*str_typing, max_length, 1, 1, "…");

    weechat_string_dyn_free (str_typing, 1);

    return str_typing_cut;
}

/*
 * Initialize typing bar items.
 */

void
typing_bar_item_init (void)
{
    weechat_bar_item_new (TYPING_BAR_ITEM_NAME,
                          &typing_bar_item_typing, NULL, NULL);
}
