/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
 *
 * pikaunit.c
 * Copyright (C) 1999-2000 Michael Natterer <mitch@gimp.org>
 *
 * 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/>.
 */

/* This file contains the definition of the size unit objects. The
 * factor of the units is relative to inches (which have a factor of 1).
 */

#include "config.h"

#include <gio/gio.h>

#include "libpikabase/pikabase.h"

#include "core-types.h"

#include "pika.h"
#include "pikaunit.h"

#include "pika-intl.h"


/* internal structures */

typedef struct
{
  gboolean  delete_on_exit;
  gdouble   factor;
  gint      digits;
  gchar    *identifier;
  gchar    *symbol;
  gchar    *abbreviation;
  gchar    *singular;
  gchar    *plural;
} PikaUnitDef;


/*  these are the built-in units
 */
static const PikaUnitDef pika_unit_defs[PIKA_UNIT_END] =
{
  /* pseudo unit */
  { FALSE,  0.0, 0, "pixels",      "px", "px",
    NC_("unit-singular", "pixel"),      NC_("unit-plural", "pixels")      },

  /* standard units */
  { FALSE,  1.0, 2, "inches",      "''", "in",
    NC_("unit-singular", "inch"),       NC_("unit-plural", "inches")      },

  { FALSE, 25.4, 1, "millimeters", "mm", "mm",
    NC_("unit-singular", "millimeter"), NC_("unit-plural", "millimeters") },

  /* professional units */
  { FALSE, 72.0, 0, "points",      "pt", "pt",
    NC_("unit-singular", "point"),      NC_("unit-plural", "points")      },

  { FALSE,  6.0, 1, "picas",       "pc", "pc",
    NC_("unit-singular", "pica"),       NC_("unit-plural", "picas")       }
};

/*  not a unit at all but kept here to have the strings in one place
 */
static const PikaUnitDef pika_unit_percent =
{
    FALSE,  0.0, 0, "percent",     "%",  "%",
    NC_("singular", "percent"),    NC_("plural", "percent")
};


/* private functions */

static PikaUnitDef *
_pika_unit_get_user_unit (Pika     *pika,
                          PikaUnit  unit)
{
  return g_list_nth_data (pika->user_units, unit - PIKA_UNIT_END);
}


/* public functions */

gint
_pika_unit_get_number_of_units (Pika *pika)
{
  return PIKA_UNIT_END + pika->n_user_units;
}

gint
_pika_unit_get_number_of_built_in_units (Pika *pika)
{
  return PIKA_UNIT_END;
}

PikaUnit
_pika_unit_new (Pika        *pika,
                const gchar *identifier,
                gdouble      factor,
                gint         digits,
                const gchar *symbol,
                const gchar *abbreviation,
                const gchar *singular,
                const gchar *plural)
{
  PikaUnitDef *user_unit = g_slice_new0 (PikaUnitDef);

  user_unit->delete_on_exit = TRUE;
  user_unit->factor         = factor;
  user_unit->digits         = digits;
  user_unit->identifier     = g_strdup (identifier);
  user_unit->symbol         = g_strdup (symbol);
  user_unit->abbreviation   = g_strdup (abbreviation);
  user_unit->singular       = g_strdup (singular);
  user_unit->plural         = g_strdup (plural);

  pika->user_units = g_list_append (pika->user_units, user_unit);
  pika->n_user_units++;

  return PIKA_UNIT_END + pika->n_user_units - 1;
}

gboolean
_pika_unit_get_deletion_flag (Pika     *pika,
                              PikaUnit  unit)
{
  g_return_val_if_fail (unit < (PIKA_UNIT_END + pika->n_user_units), FALSE);

  if (unit < PIKA_UNIT_END)
    return FALSE;

  return _pika_unit_get_user_unit (pika, unit)->delete_on_exit;
}

void
_pika_unit_set_deletion_flag (Pika     *pika,
                              PikaUnit  unit,
                              gboolean  deletion_flag)
{
  g_return_if_fail ((unit >= PIKA_UNIT_END) &&
                    (unit < (PIKA_UNIT_END + pika->n_user_units)));

  _pika_unit_get_user_unit (pika, unit)->delete_on_exit =
    deletion_flag ? TRUE : FALSE;
}

gdouble
_pika_unit_get_factor (Pika     *pika,
                       PikaUnit  unit)
{
  g_return_val_if_fail (unit < (PIKA_UNIT_END + pika->n_user_units) ||
                        (unit == PIKA_UNIT_PERCENT),
                        pika_unit_defs[PIKA_UNIT_INCH].factor);

  if (unit < PIKA_UNIT_END)
    return pika_unit_defs[unit].factor;

  if (unit == PIKA_UNIT_PERCENT)
    return pika_unit_percent.factor;

  return _pika_unit_get_user_unit (pika, unit)->factor;
}

gint
_pika_unit_get_digits (Pika     *pika,
                       PikaUnit  unit)
{
  g_return_val_if_fail (unit < (PIKA_UNIT_END + pika->n_user_units) ||
                        (unit == PIKA_UNIT_PERCENT),
                        pika_unit_defs[PIKA_UNIT_INCH].digits);

  if (unit < PIKA_UNIT_END)
    return pika_unit_defs[unit].digits;

  if (unit == PIKA_UNIT_PERCENT)
    return pika_unit_percent.digits;

  return _pika_unit_get_user_unit (pika, unit)->digits;
}

const gchar *
_pika_unit_get_identifier (Pika     *pika,
                           PikaUnit  unit)
{
  g_return_val_if_fail ((unit < (PIKA_UNIT_END + pika->n_user_units)) ||
                        (unit == PIKA_UNIT_PERCENT),
                        pika_unit_defs[PIKA_UNIT_INCH].identifier);

  if (unit < PIKA_UNIT_END)
    return pika_unit_defs[unit].identifier;

  if (unit == PIKA_UNIT_PERCENT)
    return pika_unit_percent.identifier;

  return _pika_unit_get_user_unit (pika, unit)->identifier;
}

const gchar *
_pika_unit_get_symbol (Pika     *pika,
                       PikaUnit  unit)
{
  g_return_val_if_fail ((unit < (PIKA_UNIT_END + pika->n_user_units)) ||
                        (unit == PIKA_UNIT_PERCENT),
                        pika_unit_defs[PIKA_UNIT_INCH].symbol);

  if (unit < PIKA_UNIT_END)
    return pika_unit_defs[unit].symbol;

  if (unit == PIKA_UNIT_PERCENT)
    return pika_unit_percent.symbol;

  return _pika_unit_get_user_unit (pika, unit)->symbol;
}

const gchar *
_pika_unit_get_abbreviation (Pika     *pika,
                             PikaUnit  unit)
{
  g_return_val_if_fail ((unit < (PIKA_UNIT_END + pika->n_user_units)) ||
                        (unit == PIKA_UNIT_PERCENT),
                        pika_unit_defs[PIKA_UNIT_INCH].abbreviation);

  if (unit < PIKA_UNIT_END)
    return pika_unit_defs[unit].abbreviation;

  if (unit == PIKA_UNIT_PERCENT)
    return pika_unit_percent.abbreviation;

  return _pika_unit_get_user_unit (pika, unit)->abbreviation;
}

const gchar *
_pika_unit_get_singular (Pika     *pika,
                         PikaUnit  unit)
{
  g_return_val_if_fail ((unit < (PIKA_UNIT_END + pika->n_user_units)) ||
                        (unit == PIKA_UNIT_PERCENT),
                        pika_unit_defs[PIKA_UNIT_INCH].singular);

  if (unit < PIKA_UNIT_END)
    return g_dpgettext2 (NULL, "unit-singular", pika_unit_defs[unit].singular);

  if (unit == PIKA_UNIT_PERCENT)
    return g_dpgettext2 (NULL, "unit-singular", pika_unit_percent.singular);

  return _pika_unit_get_user_unit (pika, unit)->singular;
}

const gchar *
_pika_unit_get_plural (Pika     *pika,
                       PikaUnit  unit)
{
  g_return_val_if_fail ((unit < (PIKA_UNIT_END + pika->n_user_units)) ||
                        (unit == PIKA_UNIT_PERCENT),
                        pika_unit_defs[PIKA_UNIT_INCH].plural);

  if (unit < PIKA_UNIT_END)
    return g_dpgettext2 (NULL, "unit-plural", pika_unit_defs[unit].plural);

  if (unit == PIKA_UNIT_PERCENT)
    return g_dpgettext2 (NULL, "unit-plural", pika_unit_percent.plural);

  return _pika_unit_get_user_unit (pika, unit)->plural;
}


/* The sole purpose of this function is to release the allocated
 * memory. It must only be used from pika_units_exit().
 */
void
pika_user_units_free (Pika *pika)
{
  GList *list;

  for (list = pika->user_units; list; list = g_list_next (list))
    {
      PikaUnitDef *user_unit = list->data;

      g_free (user_unit->identifier);
      g_free (user_unit->symbol);
      g_free (user_unit->abbreviation);
      g_free (user_unit->singular);
      g_free (user_unit->plural);

      g_slice_free (PikaUnitDef, user_unit);
    }

  g_list_free (pika->user_units);
  pika->user_units   = NULL;
  pika->n_user_units = 0;
}
