/*
 * Copyright (C) Birb Developers
 *
 * Birb is the legal property of its developers, whose names are too numerous
 * to list here. Please refer to the AUTHORS file distributed with this source
 * distribution
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>

#include "birbstringlist.h"

#include "birbstr.h"
#include "birbstringobject.h"

struct _BirbStringList {
    GObject parent;

    GPtrArray *items;
};

enum {
    PROP_0,
    PROP_ITEM_TYPE,
    PROP_N_ITEMS,
    N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

/******************************************************************************
 * GListModel Implementation
 *****************************************************************************/
static GType
birb_string_list_get_item_type(G_GNUC_UNUSED GListModel *model) {
    return BIRB_TYPE_STRING_OBJECT;
}

static guint
birb_string_list_get_n_items(GListModel *model) {
    BirbStringList *list = BIRB_STRING_LIST(model);

    return list->items->len;
}

static gpointer
birb_string_list_get_item(GListModel *model, guint position) {
    BirbStringList *list = BIRB_STRING_LIST(model);
    BirbStringObject *obj = NULL;

    if(position < list->items->len) {
        obj = g_ptr_array_index(list->items, position);
        g_object_ref(obj);
    }

    return obj;
}

static void
birb_string_list_list_model_init(GListModelInterface *iface) {
    iface->get_item_type = birb_string_list_get_item_type;
    iface->get_n_items = birb_string_list_get_n_items;
    iface->get_item = birb_string_list_get_item;
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_FINAL_TYPE_WITH_CODE(BirbStringList, birb_string_list,
                              G_TYPE_OBJECT,
                              G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL,
                                                    birb_string_list_list_model_init))

static void
birb_string_list_finalize(GObject *obj) {
    BirbStringList *list = BIRB_STRING_LIST(obj);

    g_clear_pointer(&list->items, g_ptr_array_unref);

    G_OBJECT_CLASS(birb_string_list_parent_class)->finalize(obj);
}

static void
birb_string_list_get_property(GObject *obj,
                              guint param_id,
                              GValue *value,
                              GParamSpec *pspec)
{
    switch(param_id) {
    case PROP_ITEM_TYPE:
        g_value_set_gtype(value,
                          birb_string_list_get_item_type(G_LIST_MODEL(obj)));
        break;
    case PROP_N_ITEMS:
        g_value_set_uint(value,
                         birb_string_list_get_n_items(G_LIST_MODEL(obj)));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
        break;
    }
}

static void
birb_string_list_init(BirbStringList *list) {
    list->items = g_ptr_array_new_full(1, g_object_unref);
}

static void
birb_string_list_class_init(BirbStringListClass *klass) {
    GObjectClass *obj_class = G_OBJECT_CLASS(klass);

    obj_class->finalize = birb_string_list_finalize;
    obj_class->get_property = birb_string_list_get_property;

    /**
     * BirbStringList:item-type:
     *
     * The type of items. See [vfunc@Gio.ListModel.get_item_type].
     *
     * Since: 3.0
     */
    properties[PROP_ITEM_TYPE] = g_param_spec_gtype(
        "item-type", NULL, NULL,
        G_TYPE_OBJECT,
        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

    /**
     * BirbStringList:n-items:
     *
     * The number of items. See [vfunc@Gio.ListModel.get_n_items].
     *
     * Since: 3.0
     */
    properties[PROP_N_ITEMS] = g_param_spec_uint(
        "n-items", NULL, NULL,
        0, G_MAXUINT, 0,
        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

    g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * Public API
 *****************************************************************************/
void
birb_string_list_append(BirbStringList *list, const char *str) {
    g_return_if_fail(BIRB_IS_STRING_LIST(list));

    g_ptr_array_add(list->items, birb_string_object_new(str));

    g_list_model_items_changed(G_LIST_MODEL(list), list->items->len - 1, 0, 1);
    g_object_notify_by_pspec(G_OBJECT(list), properties[PROP_N_ITEMS]);
}

gboolean
birb_string_list_equal(BirbStringList *list1, BirbStringList *list2) {
    if(list1 == NULL && list2 == NULL) {
        return TRUE;
    }

    if(BIRB_IS_STRING_LIST(list1) && BIRB_IS_STRING_LIST(list2)) {
        if(list1->items->len != list2->items->len) {
            return FALSE;
        }

        for(guint i = 0; i < list1->items->len; i++) {
            BirbStringObject *str1 = NULL;
            BirbStringObject *str2 = NULL;

            str1 = g_ptr_array_index(list1->items, i);
            str2 = g_ptr_array_index(list2->items, i);
            if(!birb_string_object_equal(str1, str2)) {
                return FALSE;
            }
        }

        return TRUE;
    }

    return FALSE;
}

BirbStringList *
birb_string_list_new(void) {
    return g_object_new(BIRB_TYPE_STRING_LIST, NULL);
}

gboolean
birb_string_list_remove(BirbStringList *list, const char *str) {
    g_return_val_if_fail(BIRB_IS_STRING_LIST(list), FALSE);

    for(guint i = 0; i < list->items->len; i++) {
        BirbStringObject *obj = NULL;

        obj = g_ptr_array_index(list->items, i);
        if(birb_str_equal(str, birb_string_object_get_string(obj))) {
            g_ptr_array_remove_index(list->items, i);

            g_list_model_items_changed(G_LIST_MODEL(list), i, 1, 0);
            g_object_notify_by_pspec(G_OBJECT(list), properties[PROP_N_ITEMS]);

            return TRUE;
        }
    }

    return FALSE;
}
