/***************************************************************************

    source::worx xtree
    Copyright © 2024-2025 c.holzheuer
    christoph.holzheuer@gmail.com

    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 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#ifndef XQCOMMAND_H
#define XQCOMMAND_H

#include <QUndoCommand>
#include <xqitem.h>

class XQViewModel;

struct XQNodeBackup
{
  int       itemPos{-1};
  int       nodePos{-1};
  XQNodePtr contentNode;
};

class XQNodeStore : public QVector<XQNodeBackup>
{

public:

  void dumpList( const QString& title="" ) const;
  virtual void saveNodes( const QModelIndexList& list ) = 0;

};

// Das command enthält immer auch die betroffenen items
// ist also auch eine SavedNodeList
class XQCommand : public QUndoCommand, public XQNodeStore
{

public:

  enum CmdType
  {
    cmdInvalid,

    cmdUndo,
    cmdRedo,

    cmdTextEdit,

    cmdCut,
    cmdPaste,
    cmdPasteSelf,
    cmdCopy,
    cmdMove,
    cmdNew,
    cmdDelete,

    cmdToggleSection,

    cmdExtern //??
  };

  XQCommand(CmdType cmdType, XQViewModel* viewModel );
  virtual ~XQCommand();

  CmdType                commandType() const;
  void                   setCommandType(  CmdType cmdType );

  const QModelIndex&     originIndex() const;
  void                   setOriginIndex( const QModelIndex& origin );

  void saveNodes( const QModelIndexList& list ) override;

  void redo() override;
  void undo() override;
  
  QString toString() const;

protected:

  CmdType       _cmdType{cmdInvalid};
  XQViewModel*  _viewModel{}; // needed for redo() / undo()
  QModelIndex   _originIndex;

  /*

  Du hast den item editor vergessen, Du Honk!

  NTCompositeModel* m_pModel;
  QModelIndex m_index;
  QVariant m_value;
  QVariant m_oldValue;
  bool m_updateIndex;
*/

};

Q_DECLARE_METATYPE(XQCommand::CmdType);

#endif // XQCOMMAND_H

