/* src/bazaar.p/bz-curated-section.c
 *
 * Copyright 2026 AUTOGEN
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "src/bazaar.p/bz-curated-section.h"



struct _BzCuratedSection
{
  GObject parent_instance;

  char *title;
  BzCuratedMarkdownInfo *subtitle;
  BzCuratedAppidsInfo *appids;
  char *list_type;
  gboolean enable_bulk_install;
  int overflow_count;
};

G_DEFINE_FINAL_TYPE (BzCuratedSection, bz_curated_section, G_TYPE_OBJECT);

enum
{
  PROP_0,

  PROP_TITLE,
  PROP_SUBTITLE,
  PROP_APPIDS,
  PROP_LIST_TYPE,
  PROP_ENABLE_BULK_INSTALL,
  PROP_OVERFLOW_COUNT,

  LAST_PROP
};
static GParamSpec *props[LAST_PROP] = { 0 };

static void
bz_curated_section_dispose (GObject *object)
{
  BzCuratedSection *self = BZ_CURATED_SECTION (object);

  g_clear_pointer (&self->title, g_free);
  g_clear_pointer (&self->subtitle, g_object_unref);
  g_clear_pointer (&self->appids, g_object_unref);
  g_clear_pointer (&self->list_type, g_free);

  G_OBJECT_CLASS (bz_curated_section_parent_class)->dispose (object);
}

static void
bz_curated_section_get_property (GObject    *object,
                       guint       prop_id,
                       GValue     *value,
                       GParamSpec *pspec)
{
  BzCuratedSection *self = BZ_CURATED_SECTION (object);

  switch (prop_id)
    {
    case PROP_TITLE:
      g_value_set_string (value, bz_curated_section_get_title (self));
      break;
    case PROP_SUBTITLE:
      g_value_set_object (value, bz_curated_section_get_subtitle (self));
      break;
    case PROP_APPIDS:
      g_value_set_object (value, bz_curated_section_get_appids (self));
      break;
    case PROP_LIST_TYPE:
      g_value_set_string (value, bz_curated_section_get_list_type (self));
      break;
    case PROP_ENABLE_BULK_INSTALL:
      g_value_set_boolean (value, bz_curated_section_get_enable_bulk_install (self));
      break;
    case PROP_OVERFLOW_COUNT:
      g_value_set_int (value, bz_curated_section_get_overflow_count (self));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bz_curated_section_set_property (GObject      *object,
                       guint         prop_id,
                       const GValue *value,
                       GParamSpec   *pspec)
{
  BzCuratedSection *self = BZ_CURATED_SECTION (object);

  switch (prop_id)
    {
    case PROP_TITLE:
      bz_curated_section_set_title (self, g_value_get_string (value));
      break;
    case PROP_SUBTITLE:
      bz_curated_section_set_subtitle (self, g_value_get_object (value));
      break;
    case PROP_APPIDS:
      bz_curated_section_set_appids (self, g_value_get_object (value));
      break;
    case PROP_LIST_TYPE:
      bz_curated_section_set_list_type (self, g_value_get_string (value));
      break;
    case PROP_ENABLE_BULK_INSTALL:
      bz_curated_section_set_enable_bulk_install (self, g_value_get_boolean (value));
      break;
    case PROP_OVERFLOW_COUNT:
      bz_curated_section_set_overflow_count (self, g_value_get_int (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bz_curated_section_class_init (BzCuratedSectionClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);



  object_class->set_property = bz_curated_section_set_property;
  object_class->get_property = bz_curated_section_get_property;
  object_class->dispose      = bz_curated_section_dispose;

  props[PROP_TITLE] =
      g_param_spec_string (
          "title",
          NULL, NULL, NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_SUBTITLE] =
      g_param_spec_object (
          "subtitle",
          NULL, NULL,
          BZ_TYPE_CURATED_MARKDOWN_INFO,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_APPIDS] =
      g_param_spec_object (
          "appids",
          NULL, NULL,
          BZ_TYPE_CURATED_APPIDS_INFO,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_LIST_TYPE] =
      g_param_spec_string (
          "list-type",
          NULL, NULL, NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_ENABLE_BULK_INSTALL] =
      g_param_spec_boolean (
          "enable-bulk-install",
          NULL, NULL, FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_OVERFLOW_COUNT] =
      g_param_spec_int (
          "overflow-count",
          NULL, NULL,
          G_MININT, G_MAXINT, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, props);
}

static void
bz_curated_section_init (BzCuratedSection *self)
{
}

BzCuratedSection *
bz_curated_section_new (void){
  return g_object_new (BZ_TYPE_CURATED_SECTION, NULL);
}

const char *
bz_curated_section_get_title (BzCuratedSection *self){
  g_return_val_if_fail (BZ_IS_CURATED_SECTION (self), NULL);
  return self->title;
}

BzCuratedMarkdownInfo *
bz_curated_section_get_subtitle (BzCuratedSection *self){
  g_return_val_if_fail (BZ_IS_CURATED_SECTION (self), NULL);
  return self->subtitle;
}

BzCuratedAppidsInfo *
bz_curated_section_get_appids (BzCuratedSection *self){
  g_return_val_if_fail (BZ_IS_CURATED_SECTION (self), NULL);
  return self->appids;
}

const char *
bz_curated_section_get_list_type (BzCuratedSection *self){
  g_return_val_if_fail (BZ_IS_CURATED_SECTION (self), NULL);
  return self->list_type;
}

gboolean
bz_curated_section_get_enable_bulk_install (BzCuratedSection *self){
  g_return_val_if_fail (BZ_IS_CURATED_SECTION (self), FALSE);
  return self->enable_bulk_install;
}

int
bz_curated_section_get_overflow_count (BzCuratedSection *self){
  g_return_val_if_fail (BZ_IS_CURATED_SECTION (self), 0);
  return self->overflow_count;
}

void
bz_curated_section_set_title (BzCuratedSection *self,
    const char *title){
  g_return_if_fail (BZ_IS_CURATED_SECTION (self));

  if (title == self->title || (title != NULL && self->title != NULL && g_strcmp0(title, self->title) == 0))
    return;

  g_clear_pointer (&self->title, g_free);
  if (title != NULL)
    self->title = g_strdup (title);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_TITLE]);
}

void
bz_curated_section_set_subtitle (BzCuratedSection *self,
    BzCuratedMarkdownInfo *subtitle){
  g_return_if_fail (BZ_IS_CURATED_SECTION (self));

  if (subtitle == self->subtitle)
    return;

  g_clear_pointer (&self->subtitle, g_object_unref);
  if (subtitle != NULL)
    self->subtitle = g_object_ref (subtitle);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_SUBTITLE]);
}

void
bz_curated_section_set_appids (BzCuratedSection *self,
    BzCuratedAppidsInfo *appids){
  g_return_if_fail (BZ_IS_CURATED_SECTION (self));

  if (appids == self->appids)
    return;

  g_clear_pointer (&self->appids, g_object_unref);
  if (appids != NULL)
    self->appids = g_object_ref (appids);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_APPIDS]);
}

void
bz_curated_section_set_list_type (BzCuratedSection *self,
    const char *list_type){
  g_return_if_fail (BZ_IS_CURATED_SECTION (self));

  if (list_type == self->list_type || (list_type != NULL && self->list_type != NULL && g_strcmp0(list_type, self->list_type) == 0))
    return;

  g_clear_pointer (&self->list_type, g_free);
  if (list_type != NULL)
    self->list_type = g_strdup (list_type);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_LIST_TYPE]);
}

void
bz_curated_section_set_enable_bulk_install (BzCuratedSection *self,
    gboolean enable_bulk_install){
  g_return_if_fail (BZ_IS_CURATED_SECTION (self));

  if (!!enable_bulk_install == !!self->enable_bulk_install)
    return;

  self->enable_bulk_install = enable_bulk_install;

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ENABLE_BULK_INSTALL]);
}

void
bz_curated_section_set_overflow_count (BzCuratedSection *self,
    int overflow_count){
  g_return_if_fail (BZ_IS_CURATED_SECTION (self));

  if (overflow_count == self->overflow_count)
    return;

  self->overflow_count = overflow_count;

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_OVERFLOW_COUNT]);
}

void
bz_curated_section_set_title_take (BzCuratedSection *self,
    char *title){
  g_return_if_fail (BZ_IS_CURATED_SECTION (self));

  if (title != NULL && self->title != NULL && g_strcmp0(title, self->title) == 0){
    g_free (title);
    return;
  }

  g_clear_pointer (&self->title, g_free);
  if (title != NULL)
    self->title = title;

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_TITLE]);
}

void
bz_curated_section_set_list_type_take (BzCuratedSection *self,
    char *list_type){
  g_return_if_fail (BZ_IS_CURATED_SECTION (self));

  if (list_type != NULL && self->list_type != NULL && g_strcmp0(list_type, self->list_type) == 0){
    g_free (list_type);
    return;
  }

  g_clear_pointer (&self->list_type, g_free);
  if (list_type != NULL)
    self->list_type = list_type;

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_LIST_TYPE]);
}

/* End of src/bazaar.p/bz-curated-section.c */
