/***************************************************************************
 *   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 "estrokecmd.h"

// Core
#include "core/direktorinstance.h"
#include "core/elementdatabase.h"
#include "core/timeserver.h"

// Elements
#include "elements/elementnode.h"
#include "elements/elementselection.h"
#include "elements/vectorelement.h"
#include "elements/strokeattribute.h"
#include "elements/animatedlengthattr.h"
#include "elements/urireference.h"
#include "elements/animateelement.h"
#include "elements/animatednumber.h"
#include "elements/number.h"
#include "elements/timeattribute.h"

// KDE
#include <klocale.h>
#include <kdebug.h>

EStrokeCMD::EStrokeCMD( const StrokeAttribute& s)
    : ECommand( i18n( "Change Stroke" ) )
{
  m_selection = Kast::self()->elementDB().selection().clone();
  m_stroke = new StrokeAttribute(s);
  m_oldstroke = new StrokeAttribute(0);
}

EStrokeCMD::EStrokeCMD( ElementNode* element, const StrokeAttribute& s)
    : ECommand( i18n( "Change Stroke" ) ),
    m_selection(0)
{
  m_selection = new ElementSelection();
  m_selection->append(*element);
  m_stroke = new StrokeAttribute(s);
  m_oldstroke = new StrokeAttribute(0);
}

EStrokeCMD::~EStrokeCMD()
{
  delete m_selection;
  delete m_stroke;
  delete m_oldstroke;
}

void EStrokeCMD::execute()
{
  ECommand::execute();
  m_selection->elements().getFirst()->accept(*this);
}

void EStrokeCMD::unexecute()
{
  ECommand::unexecute();
  Kast::self()->elementDB().selection() = *m_selection;
  m_selection->elements().getFirst()->accept(*this);
}

bool EStrokeCMD::visit( VectorElement & element )
{
  if ( isExecuting() )
    kdDebug() << "EStrokeCMD::visit() (exec) - " << endl;
  else
    kdDebug() << "EStrokeCMD::visit() (exec) - " << endl;

  if ( getRecordAnimation() )
  {
    if ( isRedoing() )
    {
      Kast::self()->elementDB().reviveEntry( m_animNumElement );
    }
    else if ( isExecuting() )
    {
      if ( element.stroke().strokeWidth().baseVal() != m_stroke->strokeWidth().baseVal() )
      {
        URIReference *ref = new URIReference( &element, true );
        m_animNumElement = new AnimateNumberElement(*ref, &element.stroke().strokeWidth() );
        m_animNumElement->setFill( AnimateElement::FILL_FREEZE );
        //animTransElement->fromAttribute() = ;
        //animTransElement->byAttribute() = byAttr;
        m_animNumElement->toAttribute() = m_stroke->strokeWidth().baseVal();
        m_animNumElement->getStartTime().setValue( 0.0 );
        m_animNumElement->getSimpleDuration().setValue( Kast::self()->timeServer().getTime() );
        element.appendChild(m_animNumElement);
        Kast::self()->elementDB().insertEntry(m_animNumElement);
      }
    }
    else
    {
      if ( m_animNumElement )
      {
        Kast::self()->elementDB().removeEntry(m_animNumElement);
      }
    }
  }
  else
  {
    if ( isUndoing() )
    {
      element.stroke() = *m_oldstroke;
    }
    else
    {
      if ( isExecuting() )
        *m_oldstroke = element.stroke();
      element.stroke() = *m_stroke;
    }
  }

  element.setDirty();
  Kast::self()->elementDB().entryModified(&element);
  return true;
}



