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

#include "elements/vectorelement.h"
#include "elements/documentelement.h"
#include "elements/fillattribute.h"
#include "elements/colorattribute.h"
#include "elements/animatedcolorattr.h"

#include <qcolor.h>
#include <qstring.h>

ElementsTest::ElementsTest()
    : Tester()
{
  m_doc = new DocumentElement();
}


void ElementsTest::allTests()
{
  testColorInterpreter("#3243AA", 50, 67, 170);
  testColorInterpreter("red", 255, 0, 0);
  testColorInterpreter("papayawhip", 255, 239, 213);
  testMatrixDecompose();
}

void ElementsTest::testColorInterpreter( const QString& colorname, int r, int g, int b )
{
  VectorElement* vec1 = new VectorElement();
  vec1->fill().color().baseVal() = ColorAttribute( colorname );
  int r1 = 0;
  int g1 = 0;
  int b1 = 0;
  vec1->fill().color().baseVal().qColor().getRgb( &r1, &g1, &b1 );
  QString color1 = QString().append(r1).append(g1).append(b1);
  QString color2 = QString().append(r).append(g).append(b);
  CHECK( color1 ,color2 );
}


#include "elements/transformlist.h"
#include "elements/matrix.h"

void ElementsTest::testMatrixDecompose()
{
  // Create a matrix out of some simple transformations
  Transform *t1_tmp = new Transform();
  Transform *t2_tmp = new Transform();
  Transform *t3_tmp = new Transform();
  Transform *t4_tmp = new Transform();
  Transform *t5_tmp = new Transform();
  Transform *t6_tmp = new Transform();
  TransformList tl_tmp;
  t5_tmp->setScale(1.5, 1.8);
  tl_tmp.append(t5_tmp);
  t3_tmp->setTranslate(40, 10);
  tl_tmp.append(t3_tmp);
  t2_tmp->setRotate(47, 0, 0);
  tl_tmp.append(t6_tmp);
  t4_tmp->setSkewX(123);
  tl_tmp.append(t4_tmp);
  t1_tmp->setScale(87.5, 1.4);
  tl_tmp.append(t1_tmp);

  Transform t_consolidate;
  tl_tmp.consolidate( t_consolidate );

  // Decompose the matrix
  TransformList tl;
  t_consolidate.decompose( tl );

  // Check the result
  for(unsigned int i = 0; i < tl.count(); ++i)
  {
    kdDebug() << "ElementsTest::testMatrixDecompose() [" << i << "] "  << tl.at(i)->getReadableTransformMetaType() << ":" << endl;

    QString transValues;
    if (tl.at(i)->transformMetaType() == Transform::TRANSFORM_SKEWX )
      transValues = QString().setNum( tl.at(i)->getSkewX());
    else if (tl.at(i)->transformMetaType() == Transform::TRANSFORM_SKEWY )
      transValues = QString().setNum( tl.at(i)->getSkewY());
    else if (tl.at(i)->transformMetaType() == Transform::TRANSFORM_ROTATE )
      transValues = QString().setNum( tl.at(i)->getRotateAngle());
    else if (tl.at(i)->transformMetaType() == Transform::TRANSFORM_SCALE )
      transValues = QString().setNum( tl.at(i)->getScaleX()).append(",").append(
                      QString().setNum( tl.at(i)->getScaleY() ) );
    else if (tl.at(i)->transformMetaType() == Transform::TRANSFORM_TRANSLATE )
      transValues = QString().setNum( tl.at(i)->getTranslateX()).append(",").append(
                      QString().setNum( tl.at(i)->getTranslateY() ) );

    kdDebug() << "ElementsTest::testMatrixDecompose() [" << i << "]    "  << transValues << endl;
  }

  Transform t_consolidate_result;
  tl.consolidate( t_consolidate_result );
  bool equal = true;
  if ( QString::number( t_consolidate.matrix()->a(), 'f', 4 ) !=
       QString::number( t_consolidate_result.matrix()->a(), 'f', 4 ) ||
       QString::number( t_consolidate.matrix()->b(), 'f', 4 ) !=
       QString::number( t_consolidate_result.matrix()->b(), 'f', 4 ) ||
       QString::number( t_consolidate.matrix()->c(), 'f', 4 ) !=
       QString::number( t_consolidate_result.matrix()->c(), 'f', 4 ) ||
       QString::number( t_consolidate.matrix()->d(), 'f', 4 ) !=
       QString::number( t_consolidate_result.matrix()->d(), 'f', 4 ) ||
       QString::number( t_consolidate.matrix()->e(), 'f', 4 ) !=
       QString::number( t_consolidate_result.matrix()->e(), 'f', 4 ) ||
       QString::number( t_consolidate.matrix()->f(), 'f', 4 ) !=
       QString::number( t_consolidate_result.matrix()->f(), 'f', 4 )
     )
    equal = false;
  //   if ( t_consolidate.matrix().a() != t_consolidate_result.matrix()->qmatrix() )
  //     equal = false;


  kdDebug() << "ElementsTest::testMatrixDecompose() - orig matrix     : " << endl;
  t_consolidate.matrix()->debugPrint();
  kdDebug() << "ElementsTest::testMatrixDecompose() - resulting matrix: " << endl;
  t_consolidate_result.matrix()->debugPrint();

  CHECK( equal, true );
}
