/* 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 Spencer Kimball and Peter Mattis
 *
 * 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/>.
 */

#include "config.h"
#include "tinyscheme/scheme-private.h"
#include "script-fu-compat.h"

/*
 * Make some PDB procedure names deprecated in ScriptFu.
 * Until such time as we turn deprecation off and make them obsolete.
 *
 * This only makes them deprecated in ScriptFu.
 */


/* private */

static const struct
{
  const gchar *old_name;
  const gchar *new_name;
}
compat_procs[] =
{
  /*
   * deprecations since 2.99
   *
   * With respect to ScriptFu,
   * the old names are *obsolete in the PDB* (as of this writing.)
   * That is, they don't exist in the PDB with the same signature.
   * There is no "compatibility" procedure in the PDB.
   *
   * With respect to Python using GI, some old names are *NOT* obsolete.
   * (Where "some" means those dealing with ID.)
   * I.E. Pika.Image.is_valid() exists but takes a GObject *, not an int ID.
   *
   * Original data was constructed more or less by hand, partially automated.
   */
   { "pika-brightness-contrast"               , "pika-drawable-brightness-contrast"      },
   { "pika-brushes-get-brush"                 , "pika-context-get-brush"                 },
   { "pika-drawable-is-channel"               , "pika-item-id-is-channel"                },
   { "pika-drawable-is-layer"                 , "pika-item-id-is-layer"                  },
   { "pika-drawable-is-layer-mask"            , "pika-item-id-is-layer-mask"             },
   { "pika-drawable-is-text-layer"            , "pika-item-id-is-text-layer"             },
   { "pika-drawable-is-valid"                 , "pika-item-id-is-valid"                  },
   { "pika-drawable-transform-2d"             , "pika-item-transform-2d"                 },
   { "pika-drawable-transform-flip"           , "pika-item-transform-flip"               },
   { "pika-drawable-transform-flip-simple"    , "pika-item-transform-flip-simple"        },
   { "pika-drawable-transform-matrix"         , "pika-item-transform-matrix"             },
   { "pika-drawable-transform-perspective"    , "pika-item-transform-perspective"        },
   { "pika-drawable-transform-rotate"         , "pika-item-transform-rotate"             },
   { "pika-drawable-transform-rotate-simple"  , "pika-item-transform-rotate-simple"      },
   { "pika-drawable-transform-scale"          , "pika-item-transform-scale"              },
   { "pika-drawable-transform-shear"          , "pika-item-transform-shear"              },
   { "pika-display-is-valid"                  , "pika-display-id-is-valid"               },
   { "pika-image-is-valid"                    , "pika-image-id-is-valid"                 },
   { "pika-item-is-channel"                   , "pika-item-id-is-channel"                },
   { "pika-item-is-drawable"                  , "pika-item-id-is-drawable"               },
   { "pika-item-is-layer"                     , "pika-item-id-is-layer"                  },
   { "pika-item-is-layer-mask"                , "pika-item-id-is-layer-mask"             },
   { "pika-item-is-selection"                 , "pika-item-id-is-selection"              },
   { "pika-item-is-text-layer"                , "pika-item-id-is-text-layer"             },
   { "pika-item-is-valid"                     , "pika-item-id-is-valid"                  },
   { "pika-item-is-vectors"                   , "pika-item-id-is-vectors"                },
   { "pika-procedural-db-dump"                , "pika-pdb-dump"                          },
   { "pika-procedural-db-get-data"            , "pika-pdb-get-data"                      },
   { "pika-procedural-db-set-data"            , "pika-pdb-set-data"                      },
   { "pika-procedural-db-get-data-size"       , "pika-pdb-get-data-size"                 },
   { "pika-procedural-db-proc-arg"            , "pika-pdb-get-proc-argument"             },
   { "pika-procedural-db-proc-info"           , "pika-pdb-get-proc-info"                 },
   { "pika-procedural-db-proc-val"            , "pika-pdb-get-proc-return-value"         },
   { "pika-procedural-db-proc-exists"         , "pika-pdb-proc-exists"                   },
   { "pika-procedural-db-query"               , "pika-pdb-query"                         },
   { "pika-procedural-db-temp-name"           , "pika-pdb-temp-name"                     },
   { "pika-image-get-exported-uri"            , "pika-image-get-exported-file"           },
   { "pika-image-get-imported-uri"            , "pika-image-get-imported-file"           },
   { "pika-image-get-xcf-uri"                 , "pika-image-get-xcf-file"                },
   { "pika-image-get-filename"                , "pika-image-get-file"                    },
   { "pika-image-set-filename"                , "pika-image-set-file"                    },
   { "pika-plugin-menu-register"              , "pika-pdb-add-proc-menu-path"            },
   { "pika-plugin-get-pdb-error-handler"      , "pika-plug-in-get-pdb-error-handler"     },
   { "pika-plugin-help-register"              , "pika-plug-in-help-register"             },
   { "pika-plugin-menu-branch-register"       , "pika-plug-in-menu-branch-register"      },
   { "pika-plugin-set-pdb-error-handler"      , "pika-plug-in-set-pdb-error-handler"     },
   { "pika-plugins-query"                     , "pika-plug-ins-query"                    },
   { "file-gtm-save"                          , "file-html-table-save"                   },
   { "python-fu-histogram-export"             , "histogram-export"                       },
   { "python-fu-gradient-save-as-css"         , "gradient-save-as-css"                   }
};

static gchar *empty_string = "";


static void
define_deprecated_scheme_func (const char   *old_name,
                               const char   *new_name,
                               const scheme *sc)
{
  gchar *buff;

  /* Creates a definition in Scheme of a function that calls a PDB procedure.
   *
   * The magic below that makes it deprecated:
   * - the "--pika-proc-db-call"
   * - defining under the old_name but calling the new_name

   * See scheme-wrapper.c, where this was copied from.
   * But here creates scheme definition of old_name
   * that calls a PDB procedure of a different name, new_name.
   *
   * As functional programming is: eval(define(apply f)).
   * load_string is more typically called eval().
   */
  buff = g_strdup_printf (" (define (%s . args)"
                          " (apply --pika-proc-db-call \"%s\" args))",
                          old_name, new_name);

  sc->vptr->load_string ((scheme *) sc, buff);

  g_free (buff);
}


/*  public functions  */

/* Define Scheme functions whose name is old name
 * that call compatible PDB procedures whose name is new name.
 * Define into the lisp machine.

 * Compatible means: signature same, semantics same.
 * The new names are not "compatibility" procedures, they are the new procedures.
 *
 * This can overwrite existing definitions in the lisp machine.
 * If the PDB has the old name already
 * (if a compatibility procedure is defined in the PDB
 * or the old name exists with a different signature)
 * and ScriptFu already defined functions for procedures of the PDB,
 * this will overwrite the ScriptFu definition,
 * but produce the same overall effect.
 * The definition here will not call the old name PDB procedure,
 * but from ScriptFu call the new name PDB procedure.
 */
void
define_compat_procs (scheme *sc)
{
  gint i;

  for (i = 0; i < G_N_ELEMENTS (compat_procs); i++)
    {
      define_deprecated_scheme_func (compat_procs[i].old_name,
                                     compat_procs[i].new_name,
                                     sc);
    }
}

/* Return empty string or old_name */
/* Used for a warning message */
const gchar *
deprecated_name_for (const char *new_name)
{
  gint i;
  const gchar * result = empty_string;

  /* search values of dictionary/map. */
  for (i = 0; i < G_N_ELEMENTS (compat_procs); i++)
    {
      if (strcmp (compat_procs[i].new_name, new_name) == 0)
        {
          result = compat_procs[i].old_name;
          break;
        }
    }
  return result;

}

/* Not used.
 * Keep for future implementation: catch "undefined symbol" from lisp machine.
 */
gboolean
is_deprecated (const char *old_name)
{
  gint i;
  gboolean result = FALSE;

  /* search keys of dictionary/map. */
  for (i = 0; i < G_N_ELEMENTS (compat_procs); i++)
  {
    if (strcmp (compat_procs[i].old_name, old_name) == 0)
      {
        result = TRUE;
        break;
      }
  }
  return result;
}
