/*
 * 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 "birbattr.h"

#include "birbstr.h"

/******************************************************************************
 * Mentions
 *****************************************************************************/
PangoAttrType BIRB_ATTR_MENTION = 0;

static PangoAttribute *
birb_attr_mention_copy(const PangoAttribute *attr) {
	BirbAttrMention *mention = (BirbAttrMention *)attr;

	return birb_attr_mention_new(mention->type,
	                             mention->id,
	                             mention->color);
}

static void
birb_attr_mention_destroy(PangoAttribute *attr) {
	BirbAttrMention *mention = (BirbAttrMention *)attr;

	g_free(mention->id);
	g_free(mention->color);
	g_free(mention);
}

static gboolean
birb_attr_mention_equal(const PangoAttribute *attr1,
                        const PangoAttribute *attr2)
{
	const BirbAttrMention *mention1 = (const BirbAttrMention *)attr1;
	const BirbAttrMention *mention2 = (const BirbAttrMention *)attr2;

	if(mention1->type != mention2->type) {
		return FALSE;
	}

	if(!birb_str_equal(mention1->id, mention2->id)) {
		return FALSE;
	}

	if(!birb_str_equal(mention1->color, mention2->color)) {
		return FALSE;
	}

	return TRUE;
}

PangoAttribute *
birb_attr_mention_new(BirbMention type, const char *id, const char *color) {
	static PangoAttrClass klass = {
		0,
		birb_attr_mention_copy,
		birb_attr_mention_destroy,
		birb_attr_mention_equal,
	};
	BirbAttrMention *result = NULL;

	if(klass.type == 0) {
		klass.type = pango_attr_type_register("BirbMention");
		BIRB_ATTR_MENTION = klass.type;
	}

	result = g_new(BirbAttrMention, 1);
	pango_attribute_init(&result->attr, &klass);

	result->type = type;
	result->id = g_strdup(id);
	result->color = g_strdup(color);

	return (PangoAttribute *)result;
}
