/*
    Copyright (C) 2007 Murad Tagirov <tmurad@gmail.com>
    Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>

    This file is part of the KDE project

    This library is free software you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    aint with this library see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "GeoDataDocument.h"

#include "GeoDataFolder.h"
#include "GeoDataPlacemark.h"
#include "GeoDataStyle.h"
#include "GeoDataStyleMap.h"

#include <QtCore/QDebug>

namespace Marble
{

class GeoDataDocumentPrivate
{
  public:
    GeoDataDocumentPrivate()
    {
    }

    ~GeoDataDocumentPrivate()
    {
    }

    QHash<QString, GeoDataStyle*> m_styleHash;
    QHash<QString, GeoDataStyleMap*> m_styleMapHash;
};

GeoDataDocument::GeoDataDocument( GeoDataObject *parent )
    : GeoDocument()
    , GeoDataContainer( parent )
    , d( new GeoDataDocumentPrivate() )
{
}

GeoDataDocument::~GeoDataDocument()
{
#ifdef DEBUG_GEODATA
    qDebug() << "delete Document";
#endif
    delete d;
}

void GeoDataDocument::addStyle( GeoDataStyle* style )
{
    Q_ASSERT( style );
    qDebug( "GeoDataDocument: Add new style" );
    d->m_styleHash.insert( style->styleId(), style );
}

void GeoDataDocument::removeStyle( GeoDataStyle* style )
{
    Q_ASSERT( style );
    qDebug( "GeoDataDocument: remove style" );
    d->m_styleHash.remove( style->styleId() );
}

GeoDataStyle* GeoDataDocument::style( const QString& styleId )
{
    /*
     * FIXME: m_styleHash always should contain at least default
     *        GeoDataStyle element
     */
    return d->m_styleHash.value( styleId );
}

QList<GeoDataStyle*> GeoDataDocument::styles() const
{
    return d->m_styleHash.values();
}

void GeoDataDocument::addStyleMap( GeoDataStyleMap* map )
{
    Q_ASSERT( map );
    qDebug( "GeoDataDocument: Add new styleMap" );
    d->m_styleMapHash.insert( map->styleId(), map );
}

void GeoDataDocument::removeStyleMap( GeoDataStyleMap* map )
{
    Q_ASSERT( map );
    qDebug( "GeoDataDocument: remove styleMap" );
    d->m_styleMapHash.remove( map->styleId() );
}

GeoDataStyleMap* GeoDataDocument::styleMap( const QString& styleId )
{
    return d->m_styleMapHash.value( styleId );
}

QList<GeoDataStyleMap*> GeoDataDocument::styleMaps() const
{
    return d->m_styleMapHash.values();
}

void GeoDataDocument::pack( QDataStream& stream ) const
{
    GeoDataContainer::pack( stream );

    stream << d->m_styleHash.size();
    
    
    for( QHash<QString, GeoDataStyle*>::const_iterator iterator 
          = d->m_styleHash.constBegin(); 
        iterator != d->m_styleHash.constEnd(); 
        ++iterator ) {
        iterator.value()->pack( stream );
    }
}


void GeoDataDocument::unpack( QDataStream& stream )
{
    GeoDataContainer::unpack( stream );

    int size = 0;

    stream >> size;
    for( int i = 0; i < size; i++ ) {
        GeoDataStyle* style = new GeoDataStyle();
        style->unpack( stream );
        d->m_styleHash.insert( style->styleId(), style );
    }
}

}
