/***********************************************************************************

    Copyright (C) 2007-2026 Ahmet Öztürk (aoz_2@yahoo.com)

    This file is part of Lifeograph.

    Lifeograph 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 3 of the License, or
    (at your option) any later version.

    Lifeograph 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Lifeograph.  If not, see <http://www.gnu.org/licenses/>.

***********************************************************************************/


#ifndef LIFEOGRAPH_UI_ENTRY_HEADER
#define LIFEOGRAPH_UI_ENTRY_HEADER


#include <thread>

#include "widgets/widget_textviewedit.hpp"
#include "widgets/po_entry.hpp"


namespace LoG
{

class UIEntry
{
    public:
                                UIEntry();
                                ~UIEntry();

        void                    handle_login();
        void                    handle_edit_enabled();
        void                    handle_logout();

        void                    show( Entry* );
        void                    show( Paragraph*, const HiddenFormat* = nullptr );
        void                    refresh(); // updates text with changes made elsewhere

        void                    show_popover()
        { m_CPo_entry.show(); }
        void                    hide_popover()
        { m_CPo_entry.hide(); }

        Entry*                  get_entry() const { return m_TvDE->m_p2entry; }
        bool                    is_cur_entry( const Entry* e, bool F_or_descendant = false ) const
        { return( e == get_entry() || ( F_or_descendant && e->is_descendant_of( get_entry() ) ) ); }
        void                    go_back();
        void                    go_forward();
        void                    add_entry_to_history( Entry* );
        void                    remove_entry_from_history( Entry* );
        void                    clear_history();

        void                    refresh_title();
        void                    refresh_extra_info();

        void                    handle_title_edited()
        {
            m_F_title_edited = true;
            refresh_title();
        }

        void                    undo()
        {
            m_TvDE->process_undo( dynamic_cast< UndoEdit* >(
                    get_entry()->get_undo_stack()->get_undo_cur() ), false );
        }
        void                    redo()
        {
            m_TvDE->process_undo( dynamic_cast< UndoEdit* >(
                    get_entry()->get_undo_stack()->get_redo_cur() ), true ); }

        void                    prepare_for_hiding();

        TextviewDiaryEdit*      get_textview() { return m_TvDE; }
        //TextbufferDiaryEdit*    get_buffer() { return m_TvDE->m_buffer_edit; }

        bool                    has_multipara_selection() const
        { return( m_TvDE->m_para_sel_end && m_TvDE->m_para_sel_bgn != m_TvDE->m_para_sel_end ); }

    protected:
        // MULTI-THREADING
        void                    start_bg_jobs();
        void                    handle_bg_jobs_completed();
        void                    destroy_bg_jobs_thread( bool );

        // WIDGETS
        Gtk::Box*               m_Bx_navigation;

        PoEntry                 m_CPo_entry { PoEntry::Mode::ENTRY };

        Gtk::Stack*             m_St_entry;
        TextviewDiaryEdit*      m_TvDE;

        // ACTIONS
        R2Action                m_A_go_back;
        R2Action                m_A_go_forward;

        bool                    m_F_index_locked      { false }; // history index
        bool                    m_F_title_edited      { false };

        std::list< Entry* >     m_browsing_history;
        unsigned int            m_bhist_offset        { 0 };  // browsing history index

        // MULTI-THREADING
        sigc::connection        m_connection_bg_jobs;
        std::thread*            m_thread_bg_jobs      { nullptr };
        Glib::Dispatcher        m_dispatcher;
        bool                    m_F_bg_jobs_queued    { false };
        bool                    m_F_extra_panel_update_queued
                                                      { false };

        ParserString            m_parser_word_count; // updates the word count in the bg

    friend class Lifeograph;
};

} // end of namespace LoG

#endif
