#ifndef GASPARDOS_CODE_BUFFER_HPP
#define GASPARDOS_CODE_BUFFER_HPP
#include "file_explorer.hpp"

namespace gaspardos
{
    namespace desktop
    {
        class CodeBuffer
        {
            struct Snapshot
            {
                std::string text;
                std::size_t cursor;

                Snapshot(const std::string& s, std::size_t p) : text(s), cursor(p)
                {
                }
            };

            std::string              text_, saved_, original_;
            std::vector<std::size_t> lines_;
            std::vector<Snapshot>    undo_;
            std::size_t              cursor_, undoBytes_;
            bool                     crlf_, bom_;
            void                     index();
            bool replace(std::size_t start, std::size_t count, const std::string& value);

        public:
            CodeBuffer();
            bool load(const std::string& raw, std::string& error);
            void clear();

            bool dirty() const
            {
                return text_ != saved_;
            }

            std::string serialize() const;

            const std::string& original() const
            {
                return original_;
            }

            void saved();

            std::size_t lineCount() const
            {
                return lines_.size();
            }

            std::string line(std::size_t number) const;
            std::size_t row() const;
            std::size_t column() const;
            void        place(std::size_t row, std::size_t column);
            bool        key(uint32_t key);
            bool        undo();
        };
    }  // namespace desktop
}  // namespace gaspardos
#endif
