/* src/bazaar.p/bz-finished-search-query.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-finished-search-query.h"



struct _BzFinishedSearchQuery
{
  GObject parent_instance;

  char *interpreted_query;
  GPtrArray *results;
  guint n_results;
  double elapsed;
};

G_DEFINE_FINAL_TYPE (BzFinishedSearchQuery, bz_finished_search_query, G_TYPE_OBJECT);

enum
{
  PROP_0,

  PROP_INTERPRETED_QUERY,
  PROP_RESULTS,
  PROP_N_RESULTS,
  PROP_ELAPSED,

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

static void
bz_finished_search_query_dispose (GObject *object)
{
  BzFinishedSearchQuery *self = BZ_FINISHED_SEARCH_QUERY (object);

  g_clear_pointer (&self->interpreted_query, g_free);
  g_clear_pointer (&self->results, g_ptr_array_unref);

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

static void
bz_finished_search_query_get_property (GObject    *object,
                       guint       prop_id,
                       GValue     *value,
                       GParamSpec *pspec)
{
  BzFinishedSearchQuery *self = BZ_FINISHED_SEARCH_QUERY (object);

  switch (prop_id)
    {
    case PROP_INTERPRETED_QUERY:
      g_value_set_string (value, bz_finished_search_query_get_interpreted_query (self));
      break;
    case PROP_RESULTS:
      g_value_set_boxed (value, bz_finished_search_query_get_results (self));
      break;
    case PROP_N_RESULTS:
      g_value_set_uint (value, bz_finished_search_query_get_n_results (self));
      break;
    case PROP_ELAPSED:
      g_value_set_double (value, bz_finished_search_query_get_elapsed (self));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bz_finished_search_query_set_property (GObject      *object,
                       guint         prop_id,
                       const GValue *value,
                       GParamSpec   *pspec)
{
  BzFinishedSearchQuery *self = BZ_FINISHED_SEARCH_QUERY (object);

  switch (prop_id)
    {
    case PROP_INTERPRETED_QUERY:
      bz_finished_search_query_set_interpreted_query (self, g_value_get_string (value));
      break;
    case PROP_RESULTS:
      bz_finished_search_query_set_results (self, g_value_get_boxed (value));
      break;
    case PROP_N_RESULTS:
      bz_finished_search_query_set_n_results (self, g_value_get_uint (value));
      break;
    case PROP_ELAPSED:
      bz_finished_search_query_set_elapsed (self, g_value_get_double (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
bz_finished_search_query_class_init (BzFinishedSearchQueryClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);



  object_class->set_property = bz_finished_search_query_set_property;
  object_class->get_property = bz_finished_search_query_get_property;
  object_class->dispose      = bz_finished_search_query_dispose;

  props[PROP_INTERPRETED_QUERY] =
      g_param_spec_string (
          "interpreted-query",
          NULL, NULL, NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_RESULTS] =
      g_param_spec_boxed (
          "results",
          NULL, NULL,
          G_TYPE_PTR_ARRAY,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_N_RESULTS] =
      g_param_spec_uint (
          "n-results",
          NULL, NULL,
          0, G_MAXUINT, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_ELAPSED] =
      g_param_spec_double (
          "elapsed",
          NULL, NULL,
          G_MININT, G_MAXINT, 0.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_finished_search_query_init (BzFinishedSearchQuery *self)
{
}

BzFinishedSearchQuery *
bz_finished_search_query_new (void){
  return g_object_new (BZ_TYPE_FINISHED_SEARCH_QUERY, NULL);
}

const char *
bz_finished_search_query_get_interpreted_query (BzFinishedSearchQuery *self){
  g_return_val_if_fail (BZ_IS_FINISHED_SEARCH_QUERY (self), NULL);
  return self->interpreted_query;
}

GPtrArray *
bz_finished_search_query_get_results (BzFinishedSearchQuery *self){
  g_return_val_if_fail (BZ_IS_FINISHED_SEARCH_QUERY (self), NULL);
  return self->results;
}

guint
bz_finished_search_query_get_n_results (BzFinishedSearchQuery *self){
  g_return_val_if_fail (BZ_IS_FINISHED_SEARCH_QUERY (self), 0);
  return self->n_results;
}

double
bz_finished_search_query_get_elapsed (BzFinishedSearchQuery *self){
  g_return_val_if_fail (BZ_IS_FINISHED_SEARCH_QUERY (self), 0.0);
  return self->elapsed;
}

void
bz_finished_search_query_set_interpreted_query (BzFinishedSearchQuery *self,
    const char *interpreted_query){
  g_return_if_fail (BZ_IS_FINISHED_SEARCH_QUERY (self));

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

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

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

void
bz_finished_search_query_set_results (BzFinishedSearchQuery *self,
    GPtrArray *results){
  g_return_if_fail (BZ_IS_FINISHED_SEARCH_QUERY (self));

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

  g_clear_pointer (&self->results, g_ptr_array_unref);
  if (results != NULL)
    self->results = g_ptr_array_ref (results);

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

void
bz_finished_search_query_set_n_results (BzFinishedSearchQuery *self,
    guint n_results){
  g_return_if_fail (BZ_IS_FINISHED_SEARCH_QUERY (self));

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

  self->n_results = n_results;

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

void
bz_finished_search_query_set_elapsed (BzFinishedSearchQuery *self,
    double elapsed){
  g_return_if_fail (BZ_IS_FINISHED_SEARCH_QUERY (self));

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

  self->elapsed = elapsed;

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

void
bz_finished_search_query_set_interpreted_query_take (BzFinishedSearchQuery *self,
    char *interpreted_query){
  g_return_if_fail (BZ_IS_FINISHED_SEARCH_QUERY (self));

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

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

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

/* End of src/bazaar.p/bz-finished-search-query.c */
