/***************************************************************************
 *   Copyright (C) 2004 by Hans Oischinger                                 *
 *   hans.oischinger@kdemail.net                                           *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include "timeserver.h"

// Elements
#include "elements/elementnode.h"
#include "elements/timeattribute.h"
#include "elements/animateelement.h"

// KDE
#include <kdebug.h>

TimeServer::TimeServer(QObject *parent, const char *name)
    : QObject(parent, name),
    m_time(0.0)
{}


TimeServer::~TimeServer()
{}

void TimeServer::setTime( double time )
{
  kdDebug(38000) << "Setting time to " << time << endl;
  m_time = time;

  emit(timeChanged());
}

TimeAttribute TimeServer::calcAnimStartTime( ElementNode * targetElement )
{
  if (!targetElement)
  {
    kdWarning() << "TimeServer::calcAnimStartTime() - Null target given... aborting" << endl;
    return TimeAttribute();
  }

  TimeAttribute retVal; // default time 0s
  ElementNode *e = targetElement->getFirstChild();
  while (e)
  {
    AnimateElement* a = ::qt_cast<AnimateElement*>(e);
    if (a)
    {
      double a_endTime = a->getStartTime().value() + a->getSimpleDuration().value();
      if ( a_endTime < m_time )
        retVal.setValue(a_endTime);
    }
    e = e->getNext();
  }

  return retVal;
}

#include "timeserver.moc"
