/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef DCFILEBROWSERHISTORY_H
#define DCFILEBROWSERHISTORY_H

#include <QObject>
#include <QStack>

class FileBrowserItem;
class FileBrowserModel;

template <typename T>
class DCFileBrowserHistory
{
public:
    DCFileBrowserHistory();
    DCFileBrowserHistory(int);
    DCFileBrowserHistory(const DCFileBrowserHistory&);

    void operator=(const DCFileBrowserHistory&);

    T next();
    T prev();

    void add(T);

    int forwardSize() { return forward.size(); }
    int backSize() { return back.size(); }

    bool isFlashBack;

private:
    int size;
    QStack<T> forward;
    QStack<T> back;
};

template <typename T>
DCFileBrowserHistory<T>::DCFileBrowserHistory():
        size(10)
{
    isFlashBack = false;
}

template <typename T>
DCFileBrowserHistory<T>::DCFileBrowserHistory(int _size):
        size(_size)
{
    isFlashBack = false;
}

template <typename T>
DCFileBrowserHistory<T>::DCFileBrowserHistory(const DCFileBrowserHistory &fh){
    size = fh.size;
    forward = fh.forward;
    back = fh.back;
    isFlashBack = fh.isFlashBack;
}

template <typename T>
void DCFileBrowserHistory<T>::operator =(const DCFileBrowserHistory &fh){
    size = fh.size;
    forward = fh.forward;
    back = fh.back;
    isFlashBack = fh.isFlashBack;
}

template <typename T>
T DCFileBrowserHistory<T>::next(){
    if (forward.empty())
        return static_cast<T>(NULL);

    T item;

    item = forward.pop();

    back.push(item);

    return item;
}

template <typename T>
T DCFileBrowserHistory<T>::prev(){
    if (back.empty())
        return static_cast<T>(NULL);

    T item;

    item = back.pop();

    forward.push(item);

    return item;
}

template <typename T>
void DCFileBrowserHistory<T>::add(T item){

    if (isFlashBack){
        isFlashBack = false;

        return;
    }

    if (back.size() == size){
        QStack<T> t = back;

        while (back.size() != 1)//erase 1st element
            t.push_front(back.pop());

        back.clear();

        while (!t.empty())
            back.push_front(t.pop());
    }

    back.push(item);
}

#endif // DCFILEBROWSERHISTORY_H
