/* {{{ GPL

 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.

 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, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

}}} */

// ==== Class Undo ====
var Undo = Class.extend({
  
  // {{{ undo
  undo: function() {
    this.animation.setCurrentFrameNumber( this.frameNumber );
  },
  // }}}
  
  // {{{ redo
  redo: function() {
    this.animation.setCurrentFrameNumber( this.frameNumber );
  },
  // }}}

  // {{{ toString
  toString: function() {
    return "Undo";
  }
  // }}}

});

// ==== Class UndoChangeFigure ====
var UndoChangeFigure = Undo.extend({
  
  // {{{ init    
  init: function( oldFigure, newFigure ) {
      
    this.oldFigure = oldFigure.copy();
    this.figureId = oldFigure.id;
    
    this.newFigure = newFigure.copy();
    this.figureId = newFigure.id;
  },
  // }}}

  // {{{ undo
  undo: function() {
    this._super();
    this.animation.getCurrentFrame().replaceFigure( this.figureId, this.oldFigure.copy() );
    this.animation.notifyFrameChanged();
  },
  // }}}
  
  // {{{ redo
  redo: function() {
    this._super();
    this.animation.getCurrentFrame().replaceFigure( this.figureId, this.newFigure.copy() );
    this.animation.notifyFrameChanged();
  },
  // }}}
  
  // {{{ toString
  toString: function() {
    return "UndoChangeFigure";
  }
  // }}}

});

// ==== Class UndoAddFigure ====
var UndoAddFigure = Undo.extend({
  
  // {{{ init    
  init: function( figure ) {
      
    this.addedFigure = figure.copy();
    this.figureId = figure.id;
  },
  // }}}

  // {{{ undo
  undo: function() {
    this._super();
    this.animation.getCurrentFrame().removeFigure( this.figureId );
    this.animation.notifyFrameChanged();    
  },
  // }}}
  
  // {{{ redo
  redo: function() {
    this._super();
    this.animation.getCurrentFrame().addFigure( this.addedFigure.copy() );
    this.animation.notifyFrameChanged();
  },
  // }}}

  // {{{ toString
  toString: function() {
    return "UndoAddFigure";
  }
  // }}}

});


// ==== Class UndoDeleteFigure ====
var UndoDeleteFigure = Undo.extend({
  
  // {{{ init    
  init: function( figure, index ) {
      
    this.figure = figure;
    this.index = index;
  },
  // }}}

  // {{{ undo
  undo: function() {
    this._super();
    this.animation.getCurrentFrame().addFigure( this.figure.copy(), this.index );
    this.animation.notifyFrameChanged();    
  },
  // }}}

  // {{{ redo
  redo: function() {
    this._super();
    this.animation.getCurrentFrame().removeFigure( this.figure.id );
    this.animation.notifyFrameChanged();
  },
  // }}}

  // {{{ toString
  toString: function() {
    return "UndoDeleteFigure";
  }
  // }}}

});

// ==== Class UndoInsertFrame ====
var UndoInsertFrame = Undo.extend({
  
  // {{{ init    
  init: function( insertedFrameNumber ) {
      
    this.insertedFrameNumber = insertedFrameNumber;
  },
  // }}}

  // {{{ undo
  undo: function() {
    this._super();
    this.animation.removeFrame( this.insertedFrameNumber );
    this.animation.setCurrentFrameNumber( this.insertedFrameNumber - 1 );
  },
  // }}}
  
  // {{{ redo
  redo: function() {
    this._super();
    
    var frame = this.animation.frames[ this.insertedFrameNumber - 1 ].copy();
    this.animation.addFrame( frame, this.insertedFrameNumber - 1 );
    this.animation.setCurrentFrameNumber( this.insertedFrameNumber );
  },
  // }}}

  // {{{ toString
  toString: function() {
    return "UndoInsertFrame";
  }
  // }}}

});

// ==== Class UndoDeleteFrame ====
var UndoDeleteFrame = Undo.extend({
  
  // {{{ init    
  init: function( deletedFrame, deletedFrameNumber ) {
      
    this.deletedFrameNumber = deletedFrameNumber;
    this.deletedFrame = deletedFrame;
  },
  // }}}

  // {{{ undo
  undo: function() {
    this._super();
    this.animation.addFrame( this.deletedFrame.copy(), this.deletedFrameNumber );
    this.animation.setCurrentFrameNumber( this.deletedFrameNumber );
  },
  // }}}
  
  // {{{ redo
  redo: function() {
    this._super();
    this.animation.removeFrame( this.deletedFrameNumber );
  },
  // }}}

  // {{{ toString
  toString: function() {
    return "UndoDeleteFrame";
  }
  // }}}

});


// ==== Class UndoZOrderFigure ====
var UndoZOrderFigure = Undo.extend({
  
  // {{{ init    
  init: function( figure, beforeIndex, afterIndex ) {
      
    this.figureId = figure.id;
    this.beforeIndex = beforeIndex;
    this.afterIndex = afterIndex;
  },
  // }}}

  // {{{ undo
  undo: function() {
    this._super();
    var frame = this.animation.getCurrentFrame();
    var figure = frame.findFigure( this.figureId );
    frame.removeFigure( this.figureId );
    frame.addFigure( figure, this.beforeIndex );
    this.animation.notifyFrameChanged();
  },
  // }}}
  
  // {{{ redo
  redo: function() {
    this._super();
    var frame = this.animation.getCurrentFrame();
    var figure = frame.findFigure( this.figureId );
    frame.removeFigure( this.figureId );
    frame.addFigure( figure, this.afterIndex );
    this.animation.notifyFrameChanged();
  },
  // }}}

  // {{{ toString
  toString: function() {
    return "UndoZOrderFigure";
  }
  // }}}

});


Undo.prototype.log = new Log( "Undo" );

