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

    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_WIDGETENTRYPICKER_HEADER
#define LIFEOGRAPH_WIDGETENTRYPICKER_HEADER


#include <gtkmm.h>

#include "../helpers_gtk.hpp"
#include "../diaryelements/diary.hpp"
#include "po_completion_proto.hpp"


namespace LoG
{

// TREESTORE FOR DIARY ELEMENT LISTS (NOT USED ANY MORE) ===========================================
/*class TreeStoreDiaryElem : public Gtk::TreeStore
{
    protected:
        TreeStoreDiaryElem();

    public:
        class Colrec : public Gtk::TreeModel::ColumnRecord
        {
            public:
                Colrec()
                {
                    add( icon );
                    add( name );
                    add( ptr );
                }
                Gtk::TreeModelColumn< R2Pixbuf >        icon;
                Gtk::TreeModelColumn< Ustring >         name;
                Gtk::TreeModelColumn< DiaryElement* >   ptr;
        };

        Colrec colrec;

        static Glib::RefPtr< TreeStoreDiaryElem > create();
};*/

class WidgetTagPicker; // forward declaration

// COMPLETION POPOVER ==============================================================================
class TagPickerCompletion : public PoCompletionProto
{
    public:
                                    TagPickerCompletion( Gtk::Widget& );

        void                        set_diary( Diary* diary );
        Diary*                      get_diary() const
        { return m_p2diary; }

        void                        add_item( DiaryElemTag* );
        void                        add_new_item();
        void                        clear();

        void                        popup( const Ustring& );
        void                        popup( const Gdk::Rectangle&, const Ustring& );
        bool                        update_LB( const Ustring& );

        bool                        handle_key( guint, bool, bool ) override;

        SignalVoidTag               signal_elem_activated()
        { return m_Sg_elem_activated; }

        DiaryElemTag*               get_selected() const;

        inline static bool          s_F_offer_new;
        bool                        m_F_entry_only  { false };

    protected:
        void                       create_new_entry();

        Gtk::Box*                  m_Bx_new_under  { nullptr };
        WidgetTagPicker*           m_WTP_new_under { nullptr };

        VecTags                    m_items;
        Ustring                    m_filter;

        SignalVoidTag              m_Sg_elem_activated;

        Diary*                     m_p2diary       { nullptr };
        bool                       m_F_offer_new;
};

// WIDGET FOR ENTERING TAG NAMES WITH AUTOCOMPLETION ===============================================
class WidgetTagPicker : public EntryClear
{
    public:
        WidgetTagPicker( BaseObjectType*, const Glib::RefPtr< Gtk::Builder >& );
        WidgetTagPicker( bool, const std::string& = "" );

        void                        set_diary( Diary* diary )
        { m_completion->set_diary( diary ); }
        void                        set_entry_only( bool F_entry_only )
        { m_completion->m_F_entry_only = F_entry_only; }

        void                        set_text( const Ustring& text ) = delete;

        void                        set_elem( DiaryElemTag* );
        LoG::Entry*                 get_entry()
        { return( ( m_p2tag && m_p2tag->is_entry() ) ? dynamic_cast< LoG::Entry* >( m_p2tag )
                                                     : nullptr ); }
        LoGID                       get_tag_id()
        { return( m_p2tag ? m_p2tag->get_id() : DEID::UNSET ); }
        LoG::Entry*                 get_entry_create( Diary* );

        SignalVoidTag               m_Sg_updated;
        SignalVoidEntry             m_Sg_updated_entry;

    protected:
        void                        initialize();

        void                        on_changed() override;
        bool                        on_key_press_event( guint, guint, Gdk::ModifierType );
        bool                        handle_drop( const Glib::ValueBase&, double, double );

        void                        handle_elem_selected( DiaryElemTag* );

        DiaryElemTag*               m_p2tag         { nullptr };

        TagPickerCompletion*        m_completion    { nullptr };

        Glib::RefPtr< Gtk::DropTarget >
                                    m_drop_target;
};

// SIMPLE POPOVER FOR SELECTING AN EENTRY OR A PARAGRAPH FROM A GROUP
class PoElemChooser
{
    public:
        static void
        show( const SetTagsByNameID&,
              std::function< void( LoG::DiaryElement* ) >&&,
              Gtk::Widget&,
              const Gdk::Rectangle& );

    private:
        static Glib::RefPtr< Gio::ListModel >
        create_model( const Glib::RefPtr< Glib::ObjectBase >& = {} );

        static VecDiaryElems m_elems;
        static bool m_F_para_mode;
        static Gtk::Popover* m_Po;
        static FuncVoidElem handle_activate;
};


} // end of namespace LoG

#endif
