/*
 * Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net> with
 * other valuable contributors. See CONTRIBUTORS.txt for the full list.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <Filter.h>

#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Str.h>

#include <string.h>

#include <Schema/Filter.h>


bool
IsRoomFiltered(Filter *filter, char *roomID)
{
    size_t i, count;
    if (!filter || !roomID)
    {
        return false;
    }

    count = ArraySize(filter->room.not_rooms);
    for (i = 0; i < count; i++)
    {
        char *notRoom = ArrayGet(filter->room.not_rooms, i);
        if (StrEquals(roomID, notRoom))
        {
            return true;
        }
    }

    count = ArraySize(filter->room.rooms);
    if (count)
    {
        for (i = 0; i < count; i++)
        {
            char *room = ArrayGet(filter->room.rooms, i);
            if (StrEquals(roomID, room))
            {
                return false;
            }
        }
        return true;
    }
    return false;
}

HashMap *
FilterApply(Filter * filter, HashMap * event)
{
    HashMap *copy;
    char *sender, *room;
    if (!event)
    {
        return NULL;
    }
    if (!filter)
    {
        /* Do NOT filter anything out */
        return JsonDuplicate(event);
    }

    sender = JsonValueAsString(HashMapGet(event, "sender"));
    room = JsonValueAsString(HashMapGet(event, "room_id"));

    if (IsRoomFiltered(filter, room))
    {
        return NULL;
    }
    copy = JsonDuplicate(event);

    (void) sender;
    return copy;
}
Filter *
FilterDecode(Db *db, char *user, char *filterStr)
{
    Filter *ret;
    DbRef *filterRef;
    HashMap *filterObj;
    if (!db || !user || !filterStr)
    {
        return NULL;
    }

    if (*filterStr != '{')
    {
        char *err;  /* TODO: use error status somewhere... */

        filterRef = DbLock(db, 3, "filters", user, filterStr);
        filterObj = DbJson(filterRef);
        ret = Malloc(sizeof(*ret));
        memset(ret, 0, sizeof(*ret));

        if (!FilterFromJson(filterObj, ret, &err))
        {
            DbUnlock(db, filterRef);
            Free(ret);
            return NULL;
        }

        DbUnlock(db, filterRef);
        (void) err;
        return ret;
    }

    /* TODO: Decode JSON here... */
    return NULL;
}

void
FilterDestroy(Filter *filter)
{
    if (!filter)
    {
        return;
    }

    FilterFree(filter);
    Free(filter);
}