/*
 * Copyright © 2014 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Alexander Larsson <alexl@redhat.com>
 */

#include "config.h"

#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <glib/gi18n.h>

#include "libglnx/libglnx.h"

#include "flatpak-builtins.h"
#include "flatpak-builtins-utils.h"
#include "flatpak-utils-private.h"
#include "flatpak-table-printer.h"

static gboolean opt_show_details;
static gboolean opt_runtime;
static gboolean opt_app;
static gboolean opt_all;
static gboolean opt_only_updates;
static gboolean opt_cached;
static char *opt_arch;
static char *opt_app_runtime;
static const char **opt_cols;

static GOptionEntry options[] = {
  { "show-details", 'd', 0, G_OPTION_ARG_NONE, &opt_show_details, N_("Show arches and branches"), NULL },
  { "runtime", 0, 0, G_OPTION_ARG_NONE, &opt_runtime, N_("Show only runtimes"), NULL },
  { "app", 0, 0, G_OPTION_ARG_NONE, &opt_app, N_("Show only apps"), NULL },
  { "updates", 0, 0, G_OPTION_ARG_NONE, &opt_only_updates, N_("Show only those where updates are available"), NULL },
  { "arch", 0, 0, G_OPTION_ARG_STRING, &opt_arch, N_("Limit to this arch (* for all)"), N_("ARCH") },
  { "all", 'a', 0, G_OPTION_ARG_NONE, &opt_all, N_("List all refs (including locale/debug)"), NULL },
  { "app-runtime", 0, 0, G_OPTION_ARG_STRING, &opt_app_runtime, N_("List all applications using RUNTIME"), N_("RUNTIME") },
  { "columns", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_cols, N_("What information to show"), N_("FIELD,…") },
  { "cached", 0, 0, G_OPTION_ARG_NONE, &opt_cached, N_("Use local caches even if they are stale"), NULL },
  { NULL }
};

static Column all_columns[] = {
  { "name",           N_("Name"),           N_("Show the name"),           1, FLATPAK_ELLIPSIZE_MODE_END, 1, 1 },
  { "description",    N_("Description"),    N_("Show the description"),    1, FLATPAK_ELLIPSIZE_MODE_END, 1, 0 },
  { "application",    N_("Application ID"),    N_("Show the application ID"), 1, FLATPAK_ELLIPSIZE_MODE_START, 0, 1 },
  { "version",        N_("Version"),        N_("Show the version"),        1, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 1 },
  { "branch",         N_("Branch"),         N_("Show the branch"),         1, FLATPAK_ELLIPSIZE_MODE_NONE, 0, 1 },
  { "arch",           N_("Arch"),           N_("Show the architecture"),   1, FLATPAK_ELLIPSIZE_MODE_NONE, 0, 0 },
  { "origin",         N_("Origin"),         N_("Show the origin remote"),  1, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 1, 1 },
  { "ref",            N_("Ref"),            N_("Show the ref"),            1, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
  { "commit",         N_("Commit"),         N_("Show the active commit"),  1, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
  { "runtime",        N_("Runtime"),        N_("Show the runtime"),        1, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
  { "installed-size", N_("Installed size"), N_("Show the installed size"), 1, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
  { "download-size",  N_("Download size"),  N_("Show the download size"),  1, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
  { "options",        N_("Options"),        N_("Show options"),            1, FLATPAK_ELLIPSIZE_MODE_END, 1, 0 },
  { NULL }
};


typedef struct RemoteStateDirPair
{
  FlatpakRemoteState *state;
  FlatpakDir         *dir;
} RemoteStateDirPair;

static void
remote_state_dir_pair_free (RemoteStateDirPair *pair)
{
  flatpak_remote_state_unref (pair->state);
  g_object_unref (pair->dir);
  g_free (pair);
}

static RemoteStateDirPair *
remote_state_dir_pair_new (FlatpakDir *dir, FlatpakRemoteState *state)
{
  RemoteStateDirPair *pair = g_new (RemoteStateDirPair, 1);

  pair->state = state;
  pair->dir = g_object_ref (dir);
  return pair;
}

static gboolean
ls_remote (GHashTable *refs_hash, const char **arches, const char *app_runtime, Column *columns, GCancellable *cancellable, GError **error)
{
  FlatpakTablePrinter *printer;
  GHashTableIter refs_iter;
  GHashTableIter iter;
  gpointer refs_key;
  gpointer refs_value;
  gpointer key;
  gpointer value;
  guint n_keys;
  g_autofree const char **keys = NULL;
  int i, j;
  g_autoptr(GHashTable) pref_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
  FlatpakKinds match_kinds;
  g_autofree char *match_id = NULL;
  g_autofree char *match_arch = NULL;
  g_autofree char *match_branch = NULL;
  gboolean need_cache_data = FALSE;
  gboolean need_appstream_data = FALSE;
  int rows, cols;

  printer = flatpak_table_printer_new ();

  flatpak_table_printer_set_columns (printer, columns,
                                     opt_cols == NULL && !opt_show_details);

  if (app_runtime)
    {
      need_cache_data = TRUE;
      if (!flatpak_split_partial_ref_arg (app_runtime, FLATPAK_KINDS_RUNTIME, NULL, NULL,
                                          &match_kinds, &match_id, &match_arch, &match_branch, error))
        return FALSE;
    }

  for (j = 0; columns[j].name; j++)
    {
      if (strcmp (columns[j].name, "download-size") == 0 ||
          strcmp (columns[j].name, "installed-size") == 0 ||
          strcmp (columns[j].name, "runtime") == 0)
        need_cache_data = TRUE;
      if (strcmp (columns[j].name, "name") == 0 ||
          strcmp (columns[j].name, "description") == 0 ||
          strcmp (columns[j].name, "version") == 0)
        need_appstream_data = TRUE;
    }

  g_hash_table_iter_init (&refs_iter, refs_hash);
  while (g_hash_table_iter_next (&refs_iter, &refs_key, &refs_value))
    {
      GHashTable *refs = refs_key;
      RemoteStateDirPair *remote_state_dir_pair = refs_value;
      FlatpakDir *dir = remote_state_dir_pair->dir;
      FlatpakRemoteState *state = remote_state_dir_pair->state;
      const char *remote = state->remote_name;
      g_autoptr(AsStore) store = NULL;
      g_autoptr(GHashTable) names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

      g_hash_table_iter_init (&iter, refs);
      while (g_hash_table_iter_next (&iter, &key, &value))
        {
          FlatpakCollectionRef *coll_ref = key;
          char *ref = coll_ref->ref_name;
          char *partial_ref;
          const char *slash = strchr (ref, '/');

          if (slash == NULL)
            {
              g_debug ("Invalid remote ref %s", ref);
              continue;
            }

          partial_ref = flatpak_make_valid_id_prefix (slash + 1);
          g_hash_table_insert (pref_hash, partial_ref, ref);
        }

      g_hash_table_iter_init (&iter, refs);
      while (g_hash_table_iter_next (&iter, &key, &value))
        {
          FlatpakCollectionRef *coll_ref = key;
          const char *ref = coll_ref->ref_name;
          const char *checksum = value;
          g_auto(GStrv) parts = NULL;

          parts = flatpak_decompose_ref (ref, NULL);
          if (parts == NULL)
            {
              g_debug ("Invalid remote ref %s", ref);
              continue;
            }

          if (opt_only_updates)
            {
              g_autoptr(GVariant) deploy_data = flatpak_dir_get_deploy_data (dir, ref, FLATPAK_DEPLOY_VERSION_ANY, cancellable, NULL);

              if (deploy_data == NULL)
                continue;

              if (g_strcmp0 (flatpak_deploy_data_get_origin (deploy_data), remote) != 0)
                continue;

              if (g_strcmp0 (flatpak_deploy_data_get_commit (deploy_data), checksum) == 0)
                continue;
            }

          if (arches != NULL && !g_strv_contains (arches, parts[2]))
            continue;

          if (strcmp (parts[0], "runtime") == 0 && !opt_runtime)
            continue;

          if (strcmp (parts[0], "app") == 0 && !opt_app)
            continue;

          if (!opt_all &&
              strcmp (parts[0], "runtime") == 0 &&
              flatpak_id_has_subref_suffix (parts[1]))
            {
              g_autofree char *prefix_partial_ref = NULL;
              char *last_dot = strrchr (parts[1], '.');

              *last_dot = 0;
              prefix_partial_ref = g_strconcat (parts[1], "/", parts[2], "/", parts[3], NULL);
              *last_dot = '.';

              if (g_hash_table_lookup (pref_hash, prefix_partial_ref))
                continue;
            }

          if (!opt_all && opt_arch == NULL &&
              /* Hide non-primary arches if the primary arch exists */
              strcmp (arches[0], parts[2]) != 0)
            {
              g_autofree char *alt_arch_ref = g_strconcat (parts[0], "/", parts[1], "/", arches[0], "/", parts[3], NULL);
              g_autoptr(FlatpakCollectionRef) alt_arch_coll_ref = flatpak_collection_ref_new (coll_ref->collection_id, alt_arch_ref);
              if (g_hash_table_lookup (refs, alt_arch_coll_ref))
                continue;
            }

          if (g_hash_table_lookup (names, ref) == NULL)
            g_hash_table_insert (names, g_strdup (ref), g_strdup (checksum));
        }

      if (need_appstream_data)
        {
          store = as_store_new ();

#if AS_CHECK_VERSION (0, 6, 1)
          as_store_set_add_flags (store, as_store_get_add_flags (store) | AS_STORE_ADD_FLAG_USE_UNIQUE_ID);
#endif

          flatpak_dir_load_appstream_store (dir, remote, NULL, store, NULL, NULL);
        }

      keys = (const char **) g_hash_table_get_keys_as_array (names, &n_keys);
      g_qsort_with_data (keys, n_keys, sizeof (char *), (GCompareDataFunc) flatpak_strcmp0_ptr, NULL);

      for (i = 0; i < n_keys; i++)
        {
          const char *ref = keys[i];
          guint64 installed_size;
          guint64 download_size;
          g_autofree char *runtime = NULL;
          AsApp *app = NULL;
          g_auto(GStrv) parts = NULL;
          g_autoptr(GVariant) sparse = NULL;

          sparse = flatpak_remote_state_lookup_sparse_cache (state, ref, NULL);

          /* The sparse cache is optional */
          if (sparse)
            {
              const char *eol;
              if (!opt_all && (g_variant_lookup (sparse, "eol", "&s", &eol) || g_variant_lookup (sparse, "eolr", "&s", &eol)))
                continue;
            }

          parts = flatpak_decompose_ref (ref, NULL);

          if (need_cache_data)
            {
              const char *metadata = NULL;
              g_autoptr(GKeyFile) metakey = NULL;

              if (!flatpak_remote_state_lookup_cache (state, ref,
                                                      &download_size, &installed_size, &metadata,
                                                      error))
                return FALSE;

              metakey = g_key_file_new ();
              if (g_key_file_load_from_data (metakey, metadata, -1, 0, NULL))
                runtime = g_key_file_get_string (metakey, "Application", "runtime", NULL);
            }

          if (need_appstream_data)
            app = as_store_find_app (store, ref);

          if (app_runtime && runtime)
            {
              g_auto(GStrv) pref = g_strsplit (runtime, "/", 3);
              if ((match_id && pref[0] && strcmp (pref[0], match_id) != 0) ||
                  (match_arch && pref[1] && strcmp (pref[1], match_arch) != 0) ||
                  (match_branch && pref[2] && strcmp (pref[2], match_branch) != 0))
                continue;
            }

          for (j = 0; columns[j].name; j++)
            {
              if (strcmp (columns[j].name, "name") == 0)
                {
                  const char *name = NULL;

                  if (app)
                    name = as_app_get_localized_name (app);

                  flatpak_table_printer_add_column (printer, name ? name : strrchr (parts[1], '.') + 1);
                }
              else if (strcmp (columns[j].name, "description") == 0)
                {
                  const char *comment = NULL;
                  if (app)
                      comment = as_app_get_localized_comment (app);

                  flatpak_table_printer_add_column (printer, comment);
                }
              else if (strcmp (columns[j].name, "version") == 0)
                flatpak_table_printer_add_column (printer, app ? as_app_get_version (app) : "");
              else if (strcmp (columns[j].name, "ref") == 0)
                flatpak_table_printer_add_column (printer, ref);
              else if (strcmp (columns[j].name, "application") == 0)
                flatpak_table_printer_add_column (printer, parts[1]);
              else if (strcmp (columns[j].name, "arch") == 0)
                flatpak_table_printer_add_column (printer, parts[2]);
              else if (strcmp (columns[j].name, "branch") == 0)
                flatpak_table_printer_add_column (printer, parts[3]);
              else if (strcmp (columns[j].name, "origin") == 0)
                flatpak_table_printer_add_column (printer, remote);
              else if (strcmp (columns[j].name, "commit") == 0)
                {
                  g_autofree char *value = NULL;

                  value = g_strdup ((char *) g_hash_table_lookup (names, keys[i]));
                  value[MIN (strlen (value), 12)] = 0;
                  flatpak_table_printer_add_column (printer, value);
                }
              else if (strcmp (columns[j].name, "installed-size") == 0)
                {
                  g_autofree char *installed = g_format_size (installed_size);
                  flatpak_table_printer_add_decimal_column (printer, installed);
                }
              else if (strcmp (columns[j].name, "download-size") == 0)
                {
                  g_autofree char *download = g_format_size (download_size);
                  flatpak_table_printer_add_decimal_column (printer, download);
                }
              else if (strcmp (columns[j].name, "runtime") == 0)
                {
                  flatpak_table_printer_add_column (printer, runtime);
                }
              else if (strcmp (columns[j].name, "options") == 0)
                {
                  flatpak_table_printer_add_column (printer, ""); /* Extra */
                  if (sparse)
                    {
                      const char *eol;

                      if (g_variant_lookup (sparse, "eol", "&s", &eol))
                        flatpak_table_printer_append_with_comma_printf (printer, "eol=%s", eol);
                      if (g_variant_lookup (sparse, "eolr", "&s", &eol))
                        flatpak_table_printer_append_with_comma_printf (printer, "eol-rebase=%s", eol);
                    }
                }
            }

          flatpak_table_printer_finish_row (printer);
        }
    }

  if (flatpak_table_printer_get_current_row (printer) > 0)
    {
      flatpak_get_window_size (&rows, &cols);
      flatpak_table_printer_print_full (printer, 0, cols, NULL, NULL);
      g_print ("\n");
    }

  flatpak_table_printer_free (printer);

  return TRUE;
}

gboolean
flatpak_builtin_remote_ls (int argc, char **argv, GCancellable *cancellable, GError **error)
{
  g_autoptr(GOptionContext) context = NULL;
  g_autoptr(GPtrArray) dirs = NULL;
  const char **arches = flatpak_get_arches ();
  const char *opt_arches[] = {NULL, NULL};
  gboolean has_remote;
  g_autoptr(GHashTable) refs_hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, (GDestroyNotify) g_hash_table_unref, (GDestroyNotify) remote_state_dir_pair_free);
  g_autofree char *col_help = NULL;
  g_autofree Column *columns = NULL;

  context = g_option_context_new (_(" [REMOTE or URI] - Show available runtimes and applications"));
  g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
  col_help = column_help (all_columns);
  g_option_context_set_description (context, col_help);

  if (!flatpak_option_context_parse (context, options, &argc, &argv,
                                     FLATPAK_BUILTIN_FLAG_STANDARD_DIRS, &dirs, cancellable, error))
    return FALSE;

  if (!opt_app && !opt_runtime)
    opt_app = opt_runtime = TRUE;

  if (argc > 2)
    return usage_error (context, _("Too many arguments"), error);

  has_remote = (argc == 2);

  if (has_remote)
    {
      g_autoptr(FlatpakDir) preferred_dir = NULL;
      g_autoptr(GHashTable) refs = NULL;
      RemoteStateDirPair *remote_state_dir_pair = NULL;
      g_autoptr(FlatpakRemoteState) state = NULL;
      gboolean is_local = FALSE;

      is_local = g_str_has_prefix (argv[1], "file:");
      if (is_local)
        preferred_dir = flatpak_dir_get_system_default ();
      else
        {
          if (!flatpak_resolve_duplicate_remotes (dirs, argv[1], &preferred_dir, cancellable, error))
            return FALSE;
        }

      state = get_remote_state (preferred_dir, argv[1], opt_cached, cancellable, error);
      if (state == NULL)
        return FALSE;

      if (!flatpak_dir_list_remote_refs (preferred_dir, state, &refs,
                                         cancellable, error))
        return FALSE;

      remote_state_dir_pair = remote_state_dir_pair_new (preferred_dir, g_steal_pointer (&state));
      g_hash_table_insert (refs_hash, g_steal_pointer (&refs), remote_state_dir_pair);
    }
  else
    {
      int i;

      for (i = 0; i < dirs->len; i++)
        {
          FlatpakDir *dir = g_ptr_array_index (dirs, i);
          g_auto(GStrv) remotes = NULL;
          int j;

          remotes = flatpak_dir_list_remotes (dir, cancellable, error);
          if (remotes == NULL)
            return FALSE;

          for (j = 0; remotes[j] != NULL; j++)
            {
              g_autoptr(GHashTable) refs = NULL;
              RemoteStateDirPair *remote_state_dir_pair = NULL;
              const char *remote_name = remotes[j];
              g_autoptr(FlatpakRemoteState) state = NULL;

              if (flatpak_dir_get_remote_disabled (dir, remote_name))
                continue;

              state = get_remote_state (dir, remote_name, opt_cached,
                                        cancellable, error);
              if (state == NULL)
                return FALSE;

              if (!flatpak_dir_list_remote_refs (dir, state, &refs,
                                                 cancellable, error))
                return FALSE;

              remote_state_dir_pair = remote_state_dir_pair_new (dir, g_steal_pointer (&state));
              g_hash_table_insert (refs_hash, g_steal_pointer (&refs), remote_state_dir_pair);
            }
        }
    }

  if (opt_arch != NULL)
    {
      if (strcmp (opt_arch, "*") == 0)
        arches = NULL;
      else
        {
          opt_arches[0] = opt_arch;
          arches = opt_arches;
        }
    }

  /* show origin by default if listing multiple remotes */
  all_columns[5].def = !has_remote;

  columns = handle_column_args (all_columns, opt_show_details, opt_cols, error);
  if (columns == NULL)
    return FALSE;

  return ls_remote (refs_hash, arches, opt_app_runtime, columns, cancellable, error);
}

gboolean
flatpak_complete_remote_ls (FlatpakCompletion *completion)
{
  g_autoptr(GOptionContext) context = NULL;
  g_autoptr(GPtrArray) dirs = NULL;
  int i;

  context = g_option_context_new ("");

  if (!flatpak_option_context_parse (context, options, &completion->argc, &completion->argv,
                                     FLATPAK_BUILTIN_FLAG_STANDARD_DIRS, &dirs, NULL, NULL))
    return FALSE;

  switch (completion->argc)
    {
    case 0:
    case 1: /* REMOTE */
      flatpak_complete_options (completion, global_entries);
      flatpak_complete_options (completion, options);
      flatpak_complete_options (completion, user_entries);
      flatpak_complete_columns (completion, all_columns);

      for (i = 0; i < dirs->len; i++)
        {
          FlatpakDir *dir = g_ptr_array_index (dirs, i);
          int j;
          g_auto(GStrv) remotes = flatpak_dir_list_remotes (dir, NULL, NULL);
          if (remotes == NULL)
            return FALSE;
          for (j = 0; remotes[j] != NULL; j++)
            flatpak_complete_word (completion, "%s ", remotes[j]);
        }

      break;
    }

  return TRUE;
}
