/*  This file is part of the KDE project
    Copyright (C) 2009 Norbert Frese <nf2.email@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    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
    along 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.

*/

#ifndef PLACESLIST_H_
#define PLACESLIST_H_

#include "placesitem.h"

class PlacesList
{
public:

    const PlacesItem::Ptr operator[] ( int index ) const
    {
        if (index < 0)
        {
            return PlacesItem::Ptr(0);
        }

        int c=0;
        int cNext = standardItems.count();
        if (index < cNext)
            return standardItems[index-c];

        c=cNext;
        cNext += deviceItems.count();
        if (index < cNext)
            return deviceItems[index-c];

        c=cNext;
        cNext += standardItems2.count();
        if (index < cNext)
            return standardItems2[index-c];

        c=cNext;
        cNext += bookmarkItems.count();
        if (index < cNext)
            return bookmarkItems[index-c];

        return PlacesItem::Ptr(0);
    }

    void removeAt(int index)
    {
        int c=0;
        int cNext = standardItems.count();
        if (index < cNext)
            standardItems.removeAt(index-c);

        c=cNext;
        cNext += deviceItems.count();
        if (index < cNext)
            deviceItems.removeAt(index-c);

        c=cNext;
        cNext += standardItems2.count();
        if (index < cNext)
            standardItems2.removeAt(index-c);

        c=cNext;
        cNext += bookmarkItems.count();
        if (index < cNext)
            bookmarkItems.removeAt(index-c);
    }

    PlacesItem::Ptr findById(const QString &id)
    {
        for (int i=0;i<size();i++)
        {
            const PlacesItem::Ptr item = (*this)[i];
            if (item->id() == id)
                return item;
        }
        return PlacesItem::Ptr(0);
    }

    int findIndexById(const QString &id)
    {
        for (int i=0;i<size();i++)
        {
            const PlacesItem::Ptr item = (*this)[i];
            if (item->id() == id)
                return i;
        }
        return -1;
    }

    int size() const
    {
        return standardItems.count()
            + deviceItems.count()
            + standardItems2.count()
            + bookmarkItems.count();
    }

    int globalIndexFromBookmarkIndex(int bi)
    {
        return standardItems.count()
            + deviceItems.count()
            + standardItems2.count()
            + bi;
    }

    int bookMarkIndexFromGlobalIndex(int i)
    {
        int bstart = standardItems.count()
            + deviceItems.count()
            + standardItems2.count();

        return i-bstart;
    }

    QList<PlacesItem::Ptr> standardItems;
    QList<PlacesItem::Ptr> deviceItems;
    QList<PlacesItem::Ptr> standardItems2;
    QList<PlacesItem::Ptr> bookmarkItems;
};


#endif /* PLACESLIST_H_ */
