/*
 * Copyright (C) 2006, 2010 Apple Inc. All rights reserved.
 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
 * Copyright (C) 2009 Google, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#pragma once

#include "BackForwardList.h"
#include <wtf/HashSet.h>
#include <wtf/Ref.h>
#include <wtf/Vector.h>

namespace WebCore {

class Page;

typedef Vector<RefPtr<HistoryItem>> HistoryItemVector;
typedef HashSet<RefPtr<HistoryItem>> HistoryItemHashSet;

// FIXME: After renaming BackForwardList to BackForwardClient,
// rename this to BackForwardList.
class BackForwardListImpl FINAL : public BackForwardList {
public: 
    static Ref<BackForwardListImpl> create(Page* page) { return adoptRef(*new BackForwardListImpl(page)); }
    virtual ~BackForwardListImpl();

    virtual void copy(const BackForwardList* other) FINAL;
    
    Page* page() { return m_page; }
    
    virtual void addItem(PassRefPtr<HistoryItem>) FINAL;
    void goBack();
    void goForward();
    virtual void goToItem(HistoryItem*) FINAL;
        
    HistoryItem* backItem();
    HistoryItem* currentItem();
    HistoryItem* forwardItem();
    virtual RefPtr<HistoryItem> itemAtIndex(int);

    void backListWithLimit(int, HistoryItemVector&);
    void forwardListWithLimit(int, HistoryItemVector&);

    int capacity() const;
    void setCapacity(int);
    bool enabled() const;
    void setEnabled(bool);
    virtual int backListCount() FINAL;
    virtual int forwardListCount() FINAL;
    virtual bool containsItem(const HistoryItem&) const FINAL;

    virtual void close() FINAL;
    bool closed() const;

    void removeItem(HistoryItem*);
    HistoryItemVector& entries();

	virtual void copyTo(BackForwardListImpl* to) const FINAL;

    virtual bool isActive() const FINAL { return enabled() && capacity(); }

private:
    explicit BackForwardListImpl(Page*);

    Page* m_page;
    HistoryItemVector m_entries;
    HistoryItemHashSet m_entryHash;
    unsigned m_current;
    unsigned m_capacity;
    bool m_closed;
    bool m_enabled;

    static const unsigned DefaultCapacity = 100;
    static const unsigned NoCurrentItemIndex = UINT_MAX;
};

inline int BackForwardListImpl::capacity() const
{
    return m_capacity;
}

inline bool BackForwardListImpl::enabled() const
{
    return m_enabled;
}

inline bool BackForwardListImpl::closed() const
{
    return m_closed;
}

inline int BackForwardListImpl::backListCount()
{
    return m_current == NoCurrentItemIndex ? 0 : m_current;
}

inline int BackForwardListImpl::forwardListCount()
{
    return m_current == NoCurrentItemIndex ? 0 : (int)m_entries.size() - (m_current + 1);
}

inline HistoryItemVector& BackForwardListImpl::entries()
{
    return m_entries;
}

} // namespace WebCore
