//
//  LineBuffer.hpp
//  TermKit
//
//  Created by Colin Caufield on 2016-03-30.
//  Copyright © 2016 Secret Geometry. All rights reserved.
//

#ifndef TERMKIT_LINE_BUFFER_HPP
#define TERMKIT_LINE_BUFFER_HPP

#include "StyledChar.hpp"
#include <boost/circular_buffer.hpp>

namespace term {

//
// LineBuffer
//

class LineBuffer
{
  public:
    
    LineBuffer(size_t maxLines)
    :
        _lines(maxLines)
    {}
    
    void setCapacity(size_t newCapacity)
    {
        _lines.set_capacity(newCapacity);
    }
    
    size_t count() const
    {
        return _lines.size();
    }
    
    bool empty() const
    {
        return _lines.empty();
    }
    
    void clear()
    {
        _lines.clear();
    }
    
    StyledString& back()
    {
        TERMKIT_ASSERT(!empty());
        return _lines.back();
    }
    
    const StyledString& back() const
    {
        TERMKIT_ASSERT(!empty());
        return _lines.back();
    }
    
    StyledString& operator[](int i)
    {
        TERMKIT_ASSERT(i < count());
        return _lines[i];
    }
    
    const StyledString& operator[](int i) const
    {
        TERMKIT_ASSERT(i < count());
        return _lines[i];
    }
    
    void insert(int index, int count, const StyledString& line)
    {
        TERMKIT_ASSERT(index <= _lines.size());
        _lines.insert(_lines.begin() + index, count, line);
    }
    
    void erase(int beginIndex, int endIndex)
    {
        TERMKIT_ASSERT(beginIndex < count());
        TERMKIT_ASSERT(endIndex <= count());
        _lines.erase(_lines.begin() + beginIndex, _lines.begin() + endIndex);
    }
    
  protected:
    
    boost::circular_buffer<StyledString> _lines;
};

}

#endif
