////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011-2025 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave 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.
//
// Octave 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 Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <QApplication>
#include <QClipboard>
#include <QCompleter>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QInputDialog>
#include <QLabel>
#include <QLineEdit>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
#include <QSignalMapper>
#include <QTimer>
#include <QVBoxLayout>

#include "gui-preferences-ws.h"
#include "gui-settings.h"
#include "gui-utils.h"
#include "workspace-view.h"

OCTAVE_BEGIN_NAMESPACE(octave)

workspace_view::workspace_view (QWidget *p)
  : octave_dock_widget ("WorkspaceView", p),
    m_view (new QTableView (this)),
    m_filter_checkbox (new QCheckBox ()),
    m_filter (new QComboBox (this)),
    m_filter_widget (new QWidget (this)),
    m_first (true)
{
  set_title (tr ("Workspace"));
  setStatusTip (tr ("View the variables in the active workspace."));

  m_filter->setToolTip (tr ("Enter text to filter the workspace"));
  m_filter->setEditable (true);
  m_filter->setMaxCount (ws_max_filter_history.def ().toInt ());
  m_filter->setInsertPolicy (QComboBox::NoInsert);
  m_filter->setSizeAdjustPolicy (QComboBox::AdjustToMinimumContentsLengthWithIcon);
  QSizePolicy sizePol (QSizePolicy::Expanding, QSizePolicy::Preferred);
  m_filter->setSizePolicy (sizePol);
  m_filter->completer ()->setCaseSensitivity (Qt::CaseSensitive);

  QLabel *filter_label = new QLabel (tr ("Filter"));

  m_view->setWordWrap (false);
  m_view->setContextMenuPolicy (Qt::CustomContextMenu);
  m_view->setShowGrid (false);
  (m_view->verticalHeader) ()->hide ();
  m_view->setAlternatingRowColors (true);
  m_view_previous_row_count = 0;

  // Set an empty widget, so we can assign a layout to it.
  setWidget (new QWidget (this));

  // Create the layouts
  QHBoxLayout *filter_layout = new QHBoxLayout ();

  filter_layout->addWidget (filter_label);
  filter_layout->addWidget (m_filter_checkbox);
  filter_layout->addWidget (m_filter);
  filter_layout->setContentsMargins (0, 0, 0, 0);

  m_filter_widget->setLayout (filter_layout);

  QVBoxLayout *ws_layout = new QVBoxLayout ();
  ws_layout->addWidget (m_filter_widget);
  ws_layout->addWidget (m_view);
  ws_layout->setSpacing (0);

  gui_settings settings;

  m_filter_shown = settings.bool_value (ws_filter_shown);
  m_filter_widget->setVisible (m_filter_shown);

  ws_layout->setContentsMargins (2, 2, 2, 2);

  // Set the empty widget to have our layout.
  widget ()->setLayout (ws_layout);

  // Initialize collapse/expand state of the workspace subcategories.

  //enable sorting (setting column and order after model was set)
  m_view->setSortingEnabled (true);

  // Set header properties for sorting
  m_view->horizontalHeader ()->setSectionsClickable (true);
  m_view->horizontalHeader ()->setSectionsMovable (true);
  m_view->horizontalHeader ()->setSortIndicator
    (settings.int_value (ws_sort_column),
     static_cast<Qt::SortOrder> (settings.uint_value (ws_sort_order)));
  // FIXME: use value<Qt::SortOrder> instead of static cast after
  //        dropping support of Qt 5.4

  m_view->horizontalHeader ()->setSortIndicatorShown (true);

  m_view->horizontalHeader ()->setContextMenuPolicy (Qt::CustomContextMenu);
  connect (m_view->horizontalHeader (),
           &QTableView::customContextMenuRequested,
           this, &workspace_view::header_contextmenu_requested);

  // Init state of the filter
  m_filter->addItems (settings.value (ws_mru_list.settings_key ()).toStringList ());

  bool filter_state = settings.bool_value (ws_filter_active);
  m_filter_checkbox->setChecked (filter_state);
  filter_activate (filter_state);

  // Connect signals and slots.

  connect (m_filter, &QComboBox::editTextChanged,
           this, &workspace_view::filter_update);
  connect (m_filter_checkbox, &QCheckBox::toggled,
           this, &workspace_view::filter_activate);
  connect (m_filter->lineEdit (), &QLineEdit::editingFinished,
           this, &workspace_view::update_filter_history);

  connect (m_view, &QTableView::customContextMenuRequested,
           this, &workspace_view::contextmenu_requested);

  connect (m_view, &QTableView::activated,
           this, &workspace_view::handle_contextmenu_edit);

  if (! p)
    make_window ();
}

void
workspace_view::restore_header_state ()
{
  gui_settings settings;

  if (settings.contains (ws_column_state.settings_key ()))
    m_view->horizontalHeader ()->restoreState
      (settings.value (ws_column_state.settings_key ()).toByteArray ());
}

void
workspace_view::setModel (workspace_model *model)
{
  m_filter_model.setSourceModel (model);
  m_filter_model.setFilterKeyColumn (0);

  m_view->setModel (&m_filter_model);

  // set the sorting after the model is set, it would be ignored otherwise

  gui_settings settings;

  m_view->sortByColumn
    (settings.int_value (ws_sort_column),
     // FIXME: use value<Qt::SortOrder> instead of static cast after
     //        dropping support of Qt 5.4
     static_cast<Qt::SortOrder> (settings.uint_value (ws_sort_order)));

  m_model = model;
}

void
workspace_view::notice_settings ()
{
  gui_settings settings;

  if (m_first)
    m_first = false;
  else
    {
      // Save current state in case some settings are messing up the state
      settings.setValue (ws_column_state.settings_key (),
                         m_view->horizontalHeader ()->saveState ());
      settings.sync ();
    }

  m_model->notice_settings (); // update colors of model first

  QString tool_tip;

  if (settings.bool_value (ws_enable_colors)
      && ! settings.bool_value (ws_hide_tool_tips))
    {
      tool_tip  = QString (tr ("View the variables in the active workspace.<br>"));
      tool_tip += QString (tr ("Colors for variable attributes:"));

      for (int i = 0; i < ws_colors_count; i++)
        {
          tool_tip +=
            QString (R"(<div style="background-color:%1;color:%2">%3</div>)")
            .arg (m_model->storage_class_color (i).name ())
            .arg (m_model->storage_class_color (i + ws_colors_count).name ())
            .arg (QCoreApplication::translate ("octave::settings_dialog",
                                               ws_color_names.at (i).toStdString ().data ()));
        }
    }

  setToolTip (tool_tip);

  // Initialize column order, visibility and width of the file browser. From this post,
  // https://www.qtcentre.org/threads/26675-QTableView-saving-restoring-columns-widths
  // this might fail if done directly after other actions. This effect shows
  // up in the GUI since Qt 6.6.x. As a solution, the following timer ensures
  // that the header is restored when the event loop is idle.

  QTimer::singleShot (0, this, SLOT(restore_header_state ()));
}

void
workspace_view::save_settings ()
{
  gui_settings settings;

  settings.setValue (ws_column_state.settings_key (),
                     m_view->horizontalHeader ()->saveState ());

  int sort_column = m_view->horizontalHeader ()->sortIndicatorSection ();
  Qt::SortOrder sort_order = m_view->horizontalHeader ()->sortIndicatorOrder ();
  settings.setValue (ws_sort_column.settings_key (), sort_column);
  settings.setValue (ws_sort_order.settings_key (), sort_order);

  settings.setValue (ws_filter_active.settings_key (), m_filter_checkbox->isChecked ());
  settings.setValue (ws_filter_shown.settings_key (), m_filter_shown);

  QStringList mru;
  for (int i = 0; i < m_filter->count (); i++)
    mru.append (m_filter->itemText (i));
  settings.setValue (ws_mru_list.settings_key (), mru);

  settings.sync ();

  octave_dock_widget::save_settings ();
}

void
workspace_view::set_filter_focus (bool focus)
{
  if (focus)
    {
      m_filter->setFocus ();
      setFocusProxy (m_filter);
    }
  else
    {
      m_view->setFocus ();
      setFocusProxy (m_view);
    }
}

void
workspace_view::filter_update (const QString& expression)
{
  m_filter_model.setFilterWildcard (expression);
  handle_model_changed ();
}

void
workspace_view::filter_activate (bool state)
{
  m_filter->setEnabled (state);
  m_filter_model.setDynamicSortFilter (state);

  if (state)
    filter_update (m_filter->currentText ());
  else
    filter_update (QString ());

  set_filter_focus (state);
}

void
workspace_view::update_filter_history ()
{
  combobox_insert_current_item (m_filter, QString ());
}

void
workspace_view::header_contextmenu_requested (const QPoint& mpos)
{
  QMenu menu (this);
  QSignalMapper sig_mapper (this);

  gui_settings settings;

  for (int i = 0; i < ws_columns_shown.length (); i++)
    {
      QAction *action
        = menu.addAction (tr (ws_columns_shown.at (i).toStdString ().data ()),
                          &sig_mapper, SLOT (map ()));
      sig_mapper.setMapping (action, i);
      action->setCheckable (true);
      action->setChecked (! m_view->isColumnHidden (i+1));
    }

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  connect (&sig_mapper, &QSignalMapper::mappedInt,
           this, &workspace_view::toggle_header);
#else
  connect (&sig_mapper, SIGNAL (mapped (int)),
           this, SLOT (toggle_header (int)));
#endif

  menu.exec (m_view->mapToGlobal (mpos));
}

void
workspace_view::toggle_header (int col)
{
  m_view->setColumnHidden (col + 1, ! m_view->isColumnHidden (col + 1));
}

void
workspace_view::contextmenu_requested (const QPoint& qpos)
{
  QMenu menu (this);

  QModelIndex index = m_view->indexAt (qpos);

  // if it isn't Local, Global etc, allow the ctx menu
  if (index.isValid () && index.column () == 0)
    {
      QString var_name = get_var_name (index);

      menu.addAction (tr ("Open in Variable Editor"), this,
                      &workspace_view::handle_contextmenu_edit);

      menu.addAction (tr ("Copy name"), this,
                      &workspace_view::handle_contextmenu_copy);

      menu.addAction (tr ("Copy value"), this,
                      &workspace_view::handle_contextmenu_copy_value);

      QAction *rename
        = menu.addAction (tr ("Rename"), this,
                          &workspace_view::handle_contextmenu_rename);

      // Use m_model here instead of using "m_view->model ()" because
      // that points to the proxy model.
      if (! m_model->is_top_level ())
        {
          rename->setDisabled (true);
          rename->setToolTip (tr ("Only top-level symbols may be renamed"));
        }

      menu.addAction ("Clear " + var_name, this,
                      &workspace_view::handle_contextmenu_clear);

      menu.addSeparator ();

      menu.addAction ("disp (" + var_name + ')', this,
                      &workspace_view::handle_contextmenu_disp);

      menu.addAction ("plot (" + var_name + ')', this,
                      &workspace_view::handle_contextmenu_plot);

      menu.addAction ("stem (" + var_name + ')', this,
                      &workspace_view::handle_contextmenu_stem);

      menu.addSeparator ();

    }

  if (m_filter_shown)
    menu.addAction (tr ("Hide filter"), this,
                    &workspace_view::handle_contextmenu_filter);
  else
    menu.addAction (tr ("Show filter"), this,
                    &workspace_view::handle_contextmenu_filter);

  menu.exec (m_view->mapToGlobal (qpos));
}

void
workspace_view::handle_contextmenu_copy ()
{
  QModelIndex index = m_view->currentIndex ();

  if (index.isValid ())
    {
      QString var_name = get_var_name (index);

      QClipboard *clipboard = QApplication::clipboard ();

      clipboard->setText (var_name);
    }
}

void
workspace_view::handle_contextmenu_copy_value ()
{
  QModelIndex index = m_view->currentIndex ();

  if (index.isValid ())
    Q_EMIT copy_variable_value_to_clipboard (get_var_name (index));
}

void
workspace_view::handle_contextmenu_rename ()
{
  QModelIndex index = m_view->currentIndex ();

  if (index.isValid ())
    {
      QString var_name = get_var_name (index);

      QInputDialog *inputDialog = new QInputDialog ();

      inputDialog->setOptions (QInputDialog::NoButtons);

      bool ok = false;

      QString new_name
        = inputDialog->getText (nullptr, "Rename Variable", "New name:",
                                QLineEdit::Normal, var_name, &ok);

      if (ok && ! new_name.isEmpty ())
        Q_EMIT rename_variable_signal (var_name, new_name);
    }
}

void
workspace_view::handle_contextmenu_edit ()
{
  QModelIndex index = m_view->currentIndex ();

  if (index.isValid ())
    Q_EMIT edit_variable_signal (get_var_name (index));
}

void
workspace_view::handle_contextmenu_clear ()
{
  relay_contextmenu_command ("clear", true);
}

void
workspace_view::handle_contextmenu_disp ()
{
  relay_contextmenu_command ("disp");
}

void
workspace_view::handle_contextmenu_plot ()
{
  relay_contextmenu_command ("figure (); plot");
}

void
workspace_view::handle_contextmenu_stem ()
{
  relay_contextmenu_command ("figure (); stem");
}

void
workspace_view::handle_contextmenu_filter ()
{
  m_filter_shown = ! m_filter_shown;
  m_filter_widget->setVisible (m_filter_shown);

  set_filter_focus (m_filter_shown && m_filter_checkbox->isChecked ());
}

void
workspace_view::handle_model_changed ()
{
  // m_view->resizeRowsToContents ();
  // Just modify those rows that have been added rather than go through
  // the whole list.  For-loop test will handle when number of rows reduced.
  QFontMetrics fm = m_view->fontMetrics ();
  int row_height = fm.height ();
  int new_row_count = m_filter_model.rowCount ();
  for (int i = m_view_previous_row_count; i < new_row_count; i++)
    m_view->setRowHeight (i, row_height);
  m_view_previous_row_count = new_row_count;
}

void
workspace_view::copyClipboard ()
{
  if (m_view->hasFocus ())
    handle_contextmenu_copy ();
}

void
workspace_view::selectAll ()
{
  if (m_view->hasFocus ())
    m_view->selectAll ();
}

void
workspace_view::relay_contextmenu_command (const QString& cmdname, bool str)
{
  QModelIndex index = m_view->currentIndex ();

  if (index.isValid ())
    {
      QString var_name;

      if (str)
        var_name = "\'" + get_var_name (index) + "\'";
      else
        var_name = get_var_name (index);

      Q_EMIT command_requested (cmdname + " (" + var_name + ");");
    }
}

QString
workspace_view::get_var_name (const QModelIndex& index)
{
  // We are using a sort model proxy so m_model won't provide the
  // correct ordering.

  QAbstractItemModel *m = m_view->model ();

  QMap<int, QVariant> item_data
    = m->itemData (index.sibling (index.row (), 0));

  return item_data[0].toString ();
}

OCTAVE_END_NAMESPACE(octave)
