/* Generated file (by generator) */

/*
 * SPDX-FileCopyrightText: 2015 William Yu <williamyu@gnome.org>
 *
 * SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "i-cal-recurrence.h"
#include "libical-glib-private.h"
#include "i-cal-array.h"
#include "i-cal-time.h"

G_DEFINE_TYPE (ICalRecurrence, i_cal_recurrence, I_CAL_TYPE_OBJECT)

static void i_cal_recurrence_class_init (G_GNUC_UNUSED ICalRecurrenceClass *klass)
{
}

static void i_cal_recurrence_init (G_GNUC_UNUSED ICalRecurrence *self)
{
}

/**
 * i_cal_recurrence_new_full: (skip)
 * @native: The native libical object.
 * @owner: The parent.
 *
 * Create a new libical-glib object from the native libical object and the owner.
 *
 * Returns: (transfer full): The newly create libical-glib object.
 *
 * Since: 1.0
 **/
ICalRecurrence *
i_cal_recurrence_new_full (struct icalrecurrencetype *native, 
                           GObject *owner)
{
    ICalRecurrence *object;
    if (native == NULL) {
        return NULL;
    }
    if (owner) { native = icalrecurrencetype_clone (native); owner = NULL; }
    
    
    object = i_cal_object_construct (I_CAL_TYPE_RECURRENCE,
                            (gpointer) native,
                            (GDestroyNotify) icalrecurrencetype_unref,
                            FALSE,
                            owner);

    return object;
}


/**
 * i_cal_recurrence_rscale_supported_calendars:
 *
 * Gets an array of calendars supporting rscale (currently always return NULL).
 *
 * Returns: (transfer full): Array of calendars. Currently always NULL.
 *
 * Since: 2.0
 *
 **/
ICalArray *
i_cal_recurrence_rscale_supported_calendars (void)
{
	return i_cal_array_new_full (icalrecurrencetype_rscale_supported_calendars (), NULL, FALSE);
}

/**
 * i_cal_recurrence_new_default: (skip)
 *
 * Returns: The default value.
 *
 * Since: 1.0
 *
 **/
struct icalrecurrencetype *
i_cal_recurrence_new_default (void)
{
    return icalrecurrencetype_new();
}

/**
 * i_cal_recurrence_new:
 *
 * Creates a new #ICalRecurrence.
 *
 * Returns: (transfer full): The newly created #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
ICalRecurrence *
i_cal_recurrence_new (void)
{
    return i_cal_recurrence_new_full(i_cal_recurrence_new_default(), NULL);
}

/**
 * i_cal_recurrence_clone:
 * @src: The #ICalRecurrence to be cloned
 *
 * Creates a copy of the @src.
 *
 * Returns: (transfer full): The newly created #ICalRecurrence.
 *
 * Since: 4.0
 *
 **/
ICalRecurrence *
i_cal_recurrence_clone (ICalRecurrence *src)
{
    g_return_val_if_fail (I_CAL_IS_RECURRENCE ((ICalRecurrence*)src), NULL);

	return i_cal_recurrence_new_full (icalrecurrencetype_clone ((struct icalrecurrencetype *)i_cal_object_get_native (I_CAL_OBJECT ((ICalObject *)src))), NULL);
}

/**
 * i_cal_recurrence_ref: (skip)
 * @self: The #ICalRecurrence to be referenced
 *
 * Increases the reference counter by 1 for the @self.
 *
 * Since: 4.0
 *
 **/
void
i_cal_recurrence_ref (ICalRecurrence *self)
{
    g_return_if_fail (I_CAL_IS_RECURRENCE ((ICalRecurrence*)self));

	icalrecurrencetype_ref ((struct icalrecurrencetype *)i_cal_object_get_native (I_CAL_OBJECT ((ICalObject *)self)));
}

/**
 * i_cal_recurrence_unref: (skip)
 * @self: The #ICalRecurrence to be unreferenced
 *
 * Decreases the reference counter by 1 for the @self.
 *
 * Since: 4.0
 *
 **/
void
i_cal_recurrence_unref (ICalRecurrence *self)
{
    g_return_if_fail (I_CAL_IS_RECURRENCE ((ICalRecurrence*)self));

	icalrecurrencetype_unref ((struct icalrecurrencetype *)i_cal_object_get_native (I_CAL_OBJECT ((ICalObject *)self)));
}

/**
 * i_cal_recurrence_day_day_of_week:
 * @day: The encoded day which represents the day of the week and Nth day of the week
 *
 * Decodes a day to a weekday in a week.
 *
 * Returns: The decoded weekday with Monday to be 1.
 *
 * Since: 1.0
 *
 **/
ICalRecurrenceWeekday
i_cal_recurrence_day_day_of_week (gshort day)
{
	return (ICalRecurrenceWeekday) (icalrecurrencetype_day_day_of_week (day));
}

/**
 * i_cal_recurrence_day_position:
 * @day: The encoded day which represents the day of the week and Nth day of the week
 *
 * Decodes a day to a position of the weekday.
 *
 * Returns: The decoded day of the week. 0 == any of day of week. 1 == first, 2 = second, -2 == second to last, etc
 *
 * Since: 1.0
 *
 **/
gint
i_cal_recurrence_day_position (gshort day)
{
	return icalrecurrencetype_day_position (day);
}

/**
 * i_cal_recurrence_encode_day:
 * @weekday: The #ICalRecurrenceWeekday to use.
 * @position: The position to use. 0 == any of day of week. 1 == first, 2 = second, -2 == second to last, etc
 *
 * Encodes the @weekday and @position into a single value, which can by split by i_cal_recurrence_day_day_of_week()
 * and i_cal_recurrence_day_position().
 *
 * Returns: Encoded @weekday and @position into a single value
 *
 * Since: 3.0.5
 *
 **/
gshort
i_cal_recurrence_encode_day (ICalRecurrenceWeekday weekday, 
                             gint position)
{
	return icalrecurrencetype_encode_day ((icalrecurrencetype_weekday) (weekday), position);
}

/**
 * i_cal_recurrence_month_is_leap:
 * @month: The month to be decoded
 *
 * Decodes a month and check whether it is a leap month.
 *
 * Returns: Whether this month is a leap month.
 *
 * Since: 2.0
 *
 **/
gboolean
i_cal_recurrence_month_is_leap (gshort month)
{
	return icalrecurrencetype_month_is_leap (month);
}

/**
 * i_cal_recurrence_month_month:
 * @month: The month to be decoded
 *
 * Decode a month from an encoded value by i_cal_recurrence_encode_month().
 *
 * Returns: The actual month number regardless of the leap flag.
 *
 * Since: 2.0
 *
 **/
gint
i_cal_recurrence_month_month (gshort month)
{
	return icalrecurrencetype_month_month (month);
}

/**
 * i_cal_recurrence_encode_month:
 * @month: The month to be encoded
 * @is_leap: %TRUE, when the month is leap, %FALSE otherwise
 *
 * Encodes the @month and the @is_leap flag into a single value, which can be split by i_cal_recurrence_month_is_leap()
 * and i_cal_recurrence_month_month().
 *
 * Returns: Encoded @month and @is_leap
 *
 * Since: 3.0.5
 *
 **/
gshort
i_cal_recurrence_encode_month (gint month, 
                               gboolean is_leap)
{
	return icalrecurrencetype_encode_month (month, is_leap);
}

/**
 * i_cal_recurrence_new_from_string:
 * @str: The string representation of the #ICalRecurrence
 *
 * Converts a string to a #ICalRecurrence.
 *
 * Returns: (transfer full): The #ICalRecurrence converted from @str.
 *
 * Since: 1.0
 *
 **/
ICalRecurrence *
i_cal_recurrence_new_from_string (const gchar *str)
{
    g_return_val_if_fail (str != NULL, NULL);

	return i_cal_recurrence_new_full (icalrecurrencetype_new_from_string (str), NULL);
}

/**
 * i_cal_recurrence_to_string:
 * @recur: The #ICalRecurrence to be converted
 *
 * Converts a #ICalRecurrence to a string.
 *
 * Returns: (transfer full): The string representation of @recur.
 *
 * Since: 1.0
 *
 **/
gchar *
i_cal_recurrence_to_string (ICalRecurrence *recur)
{
    g_return_val_if_fail (I_CAL_IS_RECURRENCE ((ICalRecurrence*)recur), NULL);

	return i_cal_memory_str_to_glib (icalrecurrencetype_as_string_r ((struct icalrecurrencetype *)i_cal_object_get_native (I_CAL_OBJECT ((ICalObject *)recur))));
}

/**
 * i_cal_recurrence_get_until:
 * @recur: The #ICalRecurrence
 *
 * Gets the until from #ICalRecurrence.
 *
 * Returns: (transfer full): The until of #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
ICalTime *
i_cal_recurrence_get_until (ICalRecurrence *recur)
{
    g_return_val_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur), NULL);
    return i_cal_time_new_full (((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->until);
}

/**
 * i_cal_recurrence_set_until:
 * @recur: The #ICalRecurrence
 * @until: The until of #ICalRecurrence
 *
 * Sets the until from #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
void
i_cal_recurrence_set_until (ICalRecurrence *recur, 
                            ICalTime *until)
{
    g_return_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur));
    g_return_if_fail (until != NULL && I_CAL_IS_TIME(until));
    ((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->until = *(icaltimetype *)i_cal_object_get_native ((ICalObject *)until);
}

/**
 * i_cal_recurrence_get_freq:
 * @recur: The #ICalRecurrence
 *
 * Gets the freq from #ICalRecurrence.
 *
 * Returns: The freq of #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
ICalRecurrenceFrequency
i_cal_recurrence_get_freq (ICalRecurrence *recur)
{
    g_return_val_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur), I_CAL_NO_RECURRENCE);
    return (ICalRecurrenceFrequency) (((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->freq);
}

/**
 * i_cal_recurrence_set_freq:
 * @recur: The #ICalRecurrence
 * @freq: The freq of #ICalRecurrence
 *
 * Sets the freq from #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
void
i_cal_recurrence_set_freq (ICalRecurrence *recur, 
                           ICalRecurrenceFrequency freq)
{
    g_return_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur));
    ((struct icalrecurrencetype *) i_cal_object_get_native ((ICalObject *)recur))->freq = (icalrecurrencetype_frequency) freq;
}

/**
 * i_cal_recurrence_get_count:
 * @recur: The #ICalRecurrence
 *
 * Gets the count from #ICalRecurrence.
 *
 * Returns: The count of #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
gint
i_cal_recurrence_get_count (ICalRecurrence *recur)
{
    g_return_val_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur), 0);
    return ((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->count;
}

/**
 * i_cal_recurrence_set_count:
 * @recur: The #ICalRecurrence
 * @count: The count of #ICalRecurrence
 *
 * Sets the count from #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
void
i_cal_recurrence_set_count (ICalRecurrence *recur, 
                            gint count)
{
    g_return_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur));
    ((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->count = count;
}

/**
 * i_cal_recurrence_get_interval:
 * @recur: The #ICalRecurrence
 *
 * Gets the interval from #ICalRecurrence.
 *
 * Returns: The interval of #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
gshort
i_cal_recurrence_get_interval (ICalRecurrence *recur)
{
    g_return_val_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur), 0);
    return ((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->interval;
}

/**
 * i_cal_recurrence_set_interval:
 * @recur: The #ICalRecurrence
 * @interval: The interval of #ICalRecurrence
 *
 * Sets the interval from #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
void
i_cal_recurrence_set_interval (ICalRecurrence *recur, 
                               gshort interval)
{
    g_return_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur));
    ((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->interval = interval;
}

/**
 * i_cal_recurrence_get_week_start:
 * @recur: The #ICalRecurrence
 *
 * Gets the week_start from #ICalRecurrence.
 *
 * Returns: The week_start of #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
ICalRecurrenceWeekday
i_cal_recurrence_get_week_start (ICalRecurrence *recur)
{
    g_return_val_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur), I_CAL_NO_WEEKDAY);
    return (ICalRecurrenceWeekday) (((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->week_start);
}

/**
 * i_cal_recurrence_set_week_start:
 * @recur: The #ICalRecurrence
 * @week_start: The week_start of #ICalRecurrence
 *
 * Sets the week_start from #ICalRecurrence.
 *
 * Since: 1.0
 *
 **/
void
i_cal_recurrence_set_week_start (ICalRecurrence *recur, 
                                 ICalRecurrenceWeekday week_start)
{
    g_return_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur));
    ((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->week_start = (icalrecurrencetype_weekday) week_start;
}

/**
 * i_cal_recurrence_get_by_array:
 * @recur: The #ICalRecurrence
 * @byrule: The 'by' part to use.
 *
 * Gets the by[byrule] array from #ICalRecurrence.
 *
 * Returns: (transfer full) (element-type gshort): The by[byrule] of #ICalRecurrence.
 *
 * Since: 4.0
 *
 **/
GArray *
i_cal_recurrence_get_by_array (ICalRecurrence *recur, 
                               ICalRecurrenceByRule byrule)
{
    g_return_val_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur), NULL);
    g_return_val_if_fail ((byrule >= 0) && ((int)byrule < (int)ICAL_BY_NUM_PARTS), NULL);
    icalrecurrence_by_data *by = &((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->by[byrule];
    GArray *array = g_array_sized_new(FALSE, TRUE, sizeof (gshort), by->size);
    if (by->size) {
        g_array_append_vals (array, by->data, by->size);
    }
    return array;
}

/**
 * i_cal_recurrence_get_by_array_size:
 * @recur: The #ICalRecurrence
 * @byrule: The 'by' part to use.
 *
 * Returns the size of given 'by' array from #ICalRecurrence.
 *
 * Returns: The current size of the given 'by' array.
 *
 * Since: 4.0
 *
 **/
guint
i_cal_recurrence_get_by_array_size (ICalRecurrence *recur, 
                                    ICalRecurrenceByRule byrule)
{
    g_return_val_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur), 0);
    g_return_val_if_fail ((byrule >= 0) && ((int)byrule < (int)ICAL_BY_NUM_PARTS), 0);
    icalrecurrence_by_data *by = &((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->by[byrule];
    return by->size;
}

/**
 * i_cal_recurrence_set_by_array:
 * @recur: The #ICalRecurrence
 * @byrule: The 'by' part to use.
 * @values: (element-type gshort): The array of values
 *
 * Sets the given 'by' array.
 *
 * Since: 4.0
 *
 **/
void
i_cal_recurrence_set_by_array (ICalRecurrence *recur, 
                               ICalRecurrenceByRule byrule, 
                               GArray *values)
{
    g_return_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur));
    g_return_if_fail ((byrule >= 0) && ((int)byrule < (int)ICAL_BY_NUM_PARTS));
    g_return_if_fail(values != NULL);
    icalrecurrence_by_data *by = &((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->by[byrule];
    g_return_if_fail(icalrecur_resize_by(by, values->len));
    memcpy(by->data, values->data, values->len * sizeof(by->data[0]));
}

/**
 * i_cal_recurrence_get_by:
 * @recur: The #ICalRecurrence
 * @byrule: The 'by' part to use.
 * @index: The index in the by[byrule] array of #ICalRecurrence.
 *
 * Returns the element at the specified index of the 'by' array if it exists, 0 otherwise.
 *
 * Returns: The 'by' part at the given position.
 *
 * Since: 4.0
 *
 **/
gshort
i_cal_recurrence_get_by (ICalRecurrence *recur, 
                         ICalRecurrenceByRule byrule, 
                         guint index)
{
    g_return_val_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur), 0);
    g_return_val_if_fail ((byrule >= 0) && ((int)byrule < (int)ICAL_BY_NUM_PARTS), 0);
    icalrecurrence_by_data *by = &((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->by[byrule];
    g_return_val_if_fail (index < (guint)by->size, 0);
    return by->data[index];
}

/**
 * i_cal_recurrence_set_by:
 * @recur: The #ICalRecurrence
 * @byrule: The 'by' part to use.
 * @index: The index in the 'by' array
 * @value: The value to be set
 *
 * Sets the by array at the given index. Resizes the array to have a size of at least index+1 elements if
 * necessary.
 *
 * Since: 4.0
 *
 **/
void
i_cal_recurrence_set_by (ICalRecurrence *recur, 
                         ICalRecurrenceByRule byrule, 
                         guint index, 
                         gshort value)
{
    g_return_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur));
    g_return_if_fail ((byrule >= 0) && ((int)byrule < (int)ICAL_BY_NUM_PARTS));
    icalrecurrence_by_data *by = &((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->by[byrule];
    if (((guint)by->size) < (index + 1)) {
        g_return_if_fail (icalrecur_resize_by(by, index + 1));
    }
    by->data[index] = value;
}

/**
 * i_cal_recurrence_resize_by_array:
 * @recur: The #ICalRecurrence
 * @byrule: The 'by' part to use.
 * @size: The new size of the 'by' array
 *
 * Resizes the 'by' array to the given size.
 *
 * Since: 4.0
 *
 **/
void
i_cal_recurrence_resize_by_array (ICalRecurrence *recur, 
                                  ICalRecurrenceByRule byrule, 
                                  guint size)
{
    g_return_if_fail (recur != NULL && I_CAL_IS_RECURRENCE (recur));
    g_return_if_fail ((byrule >= 0) && ((int)byrule < (int)ICAL_BY_NUM_PARTS));
    icalrecurrence_by_data *by = &((struct icalrecurrencetype *)i_cal_object_get_native ((ICalObject *)recur))->by[byrule];
    g_return_if_fail (icalrecur_resize_by(by, size));
}
