/***

  Olive - Non-Linear Video Editor
  Copyright (C) 2020 Olive Team

  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 <http://www.gnu.org/licenses/>.

***/

#include "nodeviewundo.h"

#include "project/item/sequence/sequence.h"

namespace olive {

NodeEdgeAddCommand::NodeEdgeAddCommand(Node *output, NodeInput *input, int element, QUndoCommand *parent) :
  UndoCommand(parent),
  output_(output),
  input_(input),
  element_(element)
{
}

void NodeEdgeAddCommand::redo_internal()
{
  Node::ConnectEdge(output_, input_, element_);
}

void NodeEdgeAddCommand::undo_internal()
{
  Node::DisconnectEdge(output_, input_, element_);
}

Project *NodeEdgeAddCommand::GetRelevantProject() const
{
  return output_->parent()->project();
}

NodeEdgeRemoveCommand::NodeEdgeRemoveCommand(Node *output, NodeInput *input, int element, QUndoCommand *parent) :
  UndoCommand(parent),
  output_(output),
  input_(input),
  element_(element)
{
}

void NodeEdgeRemoveCommand::redo_internal()
{
  Node::DisconnectEdge(output_, input_, element_);
}

void NodeEdgeRemoveCommand::undo_internal()
{
  Node::ConnectEdge(output_, input_, element_);
}

Project *NodeEdgeRemoveCommand::GetRelevantProject() const
{
  return output_->parent()->project();
}

NodeAddCommand::NodeAddCommand(NodeGraph *graph, Node *node, QUndoCommand *parent) :
  UndoCommand(parent),
  graph_(graph),
  node_(node)
{
  // Ensures that when this command is destroyed, if redo() is never called again, the node will be destroyed too
  node_->setParent(&memory_manager_);
}

void NodeAddCommand::redo_internal()
{
  node_->setParent(graph_);
}

void NodeAddCommand::undo_internal()
{
  node_->setParent(&memory_manager_);
}

Project *NodeAddCommand::GetRelevantProject() const
{
  return graph_->project();
}

NodeRemoveCommand::NodeRemoveCommand(Node *node, QUndoCommand *parent) :
  UndoCommand(parent),
  graph_(node->parent()),
  node_(node)
{
}

void NodeRemoveCommand::redo_internal()
{
  node_->setParent(&memory_manager_);
}

void NodeRemoveCommand::undo_internal()
{
  node_->setParent(graph_);
}

Project *NodeRemoveCommand::GetRelevantProject() const
{
  return static_cast<Sequence*>(graph_)->project();
}

NodeCopyInputsCommand::NodeCopyInputsCommand(Node *src, Node *dest, bool include_connections, QUndoCommand *parent) :
  QUndoCommand(parent),
  src_(src),
  dest_(dest),
  include_connections_(include_connections)
{
}

void NodeCopyInputsCommand::redo()
{
  Node::CopyInputs(src_, dest_, include_connections_);
}

}
