/***************************************************************************
 *   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 "efillcmd.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/fillattribute.h"
#include "elements/animatedlengthattr.h"
#include "elements/urireference.h"
#include "elements/animateelement.h"
#include "elements/animatedcolorattr.h"
#include "elements/colorattribute.h"
#include "elements/animatednumber.h"
#include "elements/number.h"
#include "elements/timeattribute.h"
#include "elements/defselement.h"
#include "elements/paintserver.h"

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

EFillCMD::EFillCMD(const FillAttribute& f, PaintServer* s)
    : ECommand( i18n("Change Fill") ),
    m_animFillColor(0),
    m_animFillOpacity(0)
{
  m_selection = Kast::self()->elementDB().selection().clone();
  m_fill = new FillAttribute(f);
  m_oldfill = new FillAttribute(0);
  m_newPaintServer = s;
}

EFillCMD::EFillCMD( ElementNode* element, const FillAttribute& f, PaintServer* s)
    : ECommand( i18n( "Change Fill" ) ),
    m_selection(0),
    m_animFillColor(0),
    m_animFillOpacity(0),
    m_removePS(false)
{
  m_selection = new ElementSelection();
  m_selection->append(*element);
  m_fill = new FillAttribute(f);
  m_oldfill = new FillAttribute(0);
  m_newPaintServer = s;
}

EFillCMD::~EFillCMD()
{
  if (m_newPaintServer && m_removePS)
  {
    Kast::self()->elementDB().deleteEntry( m_newPaintServer );
  }

  delete m_selection;
  delete m_oldfill;
  delete m_fill;
  delete m_animFillColor;
  delete m_animFillOpacity;
}

void EFillCMD::execute()
{
  ECommand::execute();
  m_removePS = false;
  if (m_newPaintServer)
  {
    if (isRedoing())
    {
      Kast::self()->elementDB().reviveEntry( m_newPaintServer );
    }
    else
    {
      DefsElement& defs = Kast::self()->elementDB().getPaintServerRoot();
      defs.appendChild(m_newPaintServer);
      Kast::self()->elementDB().insertEntry(m_newPaintServer);
    }
  }

  m_selection->elements().getFirst()->accept(*this);
}

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

  if (m_newPaintServer)
  {
    Kast::self()->elementDB().removeEntry( m_newPaintServer );
  }
}

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

  if ( getRecordAnimation() )
  {
    if ( isRedoing() )
    {
      if ( m_animFillOpacity )
        Kast::self()->elementDB().reviveEntry( m_animFillOpacity );
      if ( m_animFillColor )
        Kast::self()->elementDB().reviveEntry( m_animFillColor );
    }
    else if ( isExecuting() )
    {
      if ( element.fill().fillOpacity().baseVal() != m_fill->fillOpacity().baseVal() )
      {
        URIReference *ref = new URIReference( &element, true );
        m_animFillOpacity = new AnimateNumberElement(*ref, &element.fill().fillOpacity() );
        m_animFillOpacity->setFill( AnimateElement::FILL_FREEZE );
        m_animFillOpacity->toAttribute() = m_fill->fillOpacity().baseVal();
        m_animFillOpacity->getStartTime().setValue( 0.0 );
        m_animFillOpacity->getSimpleDuration().setValue( Kast::self()->timeServer().getTime() );
        element.appendChild( m_animFillOpacity );
        Kast::self()->elementDB().insertEntry( m_animFillOpacity );
      }
      if ( element.fill().color().baseVal() != m_fill->color().baseVal() )
      {
        URIReference *ref = new URIReference( &element, true );
        m_animFillColor = new AnimateColorElement(*ref, &element.fill().color() );
        m_animFillColor->setFill( AnimateElement::FILL_FREEZE );
        m_animFillColor->toAttribute() = m_fill->color().baseVal();
        m_animFillColor->getStartTime().setValue( 0.0 );
        m_animFillColor->getSimpleDuration().setValue( Kast::self()->timeServer().getTime() );
        element.appendChild( m_animFillColor );
        Kast::self()->elementDB().insertEntry( m_animFillColor );
      }
    }
    else if ( isUndoing() )
    {
      if ( m_animFillOpacity )
        Kast::self()->elementDB().removeEntry( m_animFillOpacity );
      if ( m_animFillColor )
        Kast::self()->elementDB().removeEntry( m_animFillColor );
    }
  }
  else
  {
    if ( isUndoing() )
    {
      element.fill() = *m_oldfill;
    }
    else if ( isExecuting() || isRedoing() )
    {
      if ( !isRedoing() )
        *m_oldfill = element.fill();
      element.fill() = *m_fill;
    }
  }

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


