/* {{{ 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.

}}} */

var AnimationController = Controller.extend({

  // {{{ init
  init: function( container, animation ) {
    
    this._super( container, animation );

    this.frameWidget = new FrameWidget( "frame", this.animation, 640, 400 );
    this.timelineWidget = new TimelineWidget( "timeline", this.animation, 560, 40 );

    this.addWidget( this.timelineWidget );
    this.addWidget( this.frameWidget );

    this.pastOnionSkinButton = this.findChild( "pastOnionSkinButton" );
    this.futureOnionSkinButton = this.findChild( "futureOnionSkinButton" );

  },
  // }}}

  // {{{ onSave
  onSave: function() {
    this.animationWriter.writeAnimation( this.animation.pageName, this.animation );
    
    return false;
  },
  // }}}
  
  // {{{ onRevert
  onRevert: function() {
    var animationController = this;
    this.animationReader.readAnimation( this.animation.pageName, this.animation.versionNumber, function( animation ) { animationController.onAnimationLoaded( animation ); } );
  },
  // }}}
  
  // {{{ onAnimationLoaded
  onAnimationLoaded: function( animation ) {
    // We reuse the existing animation object
    this.log.debug( "onAnimationLoaded. this=", this );
    this.animation.revert( animation );
  },
  // }}}
  
  // {{{ onFigureSelected
  onFigureSelected: function( sender ) {
    
  },
  // }}}
  
  // {{{ onTogglePastOnionSkin
  onTogglePastOnionSkin: function() {
    this.frameWidget.onTogglePastOnionSkin();
    this.pastOnionSkinButton.className = this.frameWidget.showPastOnionSkin ? "selected" : "";
    this.log.debug( "pastonion skin status", this.frameWidget.showPastOnionSkin, this.pastOnionSkinButton.className );
  },
  // }}}
  
  // {{{ onToggleFutureOnionSkin
  onToggleFutureOnionSkin: function() {
    this.frameWidget.onToggleFutureOnionSkin();
    this.futureOnionSkinButton.className = this.frameWidget.showFutureOnionSkin ? "selected" : "";
  },
  // }}}

  // {{{ onImportFigure
  onImportFigure: function() {
    var animationController = this;
    openDialog.pick( "stock", ".*\\.stick", function( namespace, filename ) { animationController.onImportChosen( namespace + ":" + filename ); } );
  },
  // }}}
  
  // {{{ onImportChosen
  onImportChosen: function( filename ) {
    this.log.debug( "Chosen filename", filename );
    this.animationReader.readFigure( filename, -1, function( figure ) { animationController.onLoadedFigure( figure ); } );
  }, // }}}
  
  // {{{ onLoadedFigure
  onLoadedFigure: function( figure ) {
    this.frameWidget.getCurrentFrame().addFigure( figure );
    this.animation.notifyFrameChanged();
    this.animation.history.add( new UndoAddFigure( figure ) );
    this.log.debug( "Loaded figure", figure );
  },
  // }}}

  // {{{ toString
  toString: function() {
    return this._super() + "(AnimationController)";
  }
  // }}}

});

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

