/***************************************************************************
 *   Copyright (C) 2004 by Armando Navarro Vázquez                         *
 *   armando@rmntec                                                        *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <qtimer.h>

#include "animation.h"

Animation::Animation(CMolecule& molecule) : m_molecule(molecule)
{

  m_timer = new QTimer(this,"animation timer");
  connect(m_timer,SIGNAL(timeout()),this,SLOT(OnAnimationTimer()));
  m_interval=100;
  m_bfirst=true;
  m_animationmode=0; //first mode in the beginning

}

Animation::~Animation()
{
  m_timer->stop();
}
void Animation::OnAnimationTimer()
{
  if(m_counter>10)
  {
    m_direction*=-1;
    m_counter=0;
  }
  std::vector<CAtom>::iterator at;
  for(at=m_molecule.m_atom.begin();at!=m_molecule.m_atom.end();at++)
  {
    at->c+=at->mode[m_animationmode]*m_direction*0.1;
  }

  emit shot();
  m_counter++;
}

void Animation::SetMode(int mode,int frame)
{
  bool brestart=false;
  if( m_timer->isActive() )
  {
    Stop();
    brestart=true;
  }
  m_animationmode=mode;
  if(brestart) Start();
}


void Animation::SetFrame(int frame)
{
  
  std::cout << "frame is " << frame << "previous frame is " << m_previousframe[m_animationmode] << std::endl;
  int nframes=frame-m_previousframe[m_animationmode];
  std::cout << "animation mode is " << std::endl;
  m_previousframe[m_animationmode]=frame;
  const double framescaling=0.10;
  std::vector<CAtom>::iterator at;
  for(at=m_molecule.m_atom.begin();at!=m_molecule.m_atom.end();at++)
  {
    at->c+=at->mode[m_animationmode]*nframes*framescaling;
  }
}

void Animation::Start()
{
  if( m_animationmode<0)
    return;
  if(!m_bfirst) Stop();
  
  m_counter=5;
  m_direction=1;
  m_timer->start(m_interval);
  m_bfirst=false;
}

void Animation::Stop()
{

  m_timer->stop();

  //Fill again the molecule with backup
  RestoreMolecule();
}

void Animation::BackupMolecule()
{
  m_backup.resize(m_molecule.m_atom.size());
  std::vector<CAtom>::iterator at;
  std::vector<coordinate>::iterator ct;
  for(at=m_molecule.m_atom.begin(),ct=m_backup.begin();at!=m_molecule.m_atom.end();at++,ct++)
  {
    (*ct)=at->c;
  }
  
  m_previousframe.resize(m_molecule.m_atom.size());
  std::vector<int>::iterator it;
  for(it=m_previousframe.begin();it!=m_previousframe.end();it++) (*it)=0;
  
}

void Animation::Break()
{
  m_timer->stop();
  m_bfirst=true;
}

void Animation::RestoreMolecule()
{
  std::vector<CAtom>::iterator at;
  std::vector<coordinate>::iterator ct=m_backup.begin();
  if (ct==m_backup.end())
    return;
  for(at=m_molecule.m_atom.begin();at!=m_molecule.m_atom.end();at++,ct++)
  {
    at->c=(*ct);
  }
  std::vector<int>::iterator it;
  for(it=m_previousframe.begin();it!=m_previousframe.end();it++) (*it)=0;
}

bool Animation::IsActive() const
 { 
 return m_timer->isActive(); 
 }

