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

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

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

#include "../lifeograph.hpp"
#include "dialog_import.hpp"
#include "src/python/py_bindings.hpp"

using namespace LoG;

// DIALOG IMPORT ===================================================================================
DialogImport::DialogImport( BaseObjectType* cobject, const Glib::RefPtr< Gtk::Builder >& builder )
:   Gtk::Window( cobject ) //, m_p2W_options( nullptr )
{
    Gtk::Button* B_cancel;

    m_FCB_import    = Gtk::Builder::get_widget_derived< FileChooserButton >(
                            builder, "FCB_import" );
    m_DD_type       = builder->get_widget< Gtk::DropDown >( "DD_import_type" );
    m_Bx_contents   = builder->get_widget< Gtk::Box >( "Bx_contents" );
    B_cancel        = builder->get_widget< Gtk::Button >( "B_cancel" );
    m_B_import      = builder->get_widget< Gtk::Button >( "B_go" );

    update_importers();

    m_FCB_import->m_signal_file_set.connect(
            sigc::mem_fun( *this, &DialogImport::handle_import_path_changed ) );
    // m_DD_type->property_selected().signal_changed().connect(
    //         [ this ](){ handle_importer_changed(); } );
    m_B_import->signal_clicked().connect( [ this ](){ handle_go(); } );
    B_cancel->signal_clicked().connect( [ this ](){ hide(); } );

    m_DD_type->set_selected( 0 );    // defaults to lifeograph diary
}

void
DialogImport::update_importers()
{
    Lifeograph::START_INTERNAL_OPERATIONS();

    // VecUstrings vec_importer_name;
    // auto        p2scripts { PyBindings::get_script_vault() };
    // unsigned    i_selected      { 0 };

    m_importers.clear();
    PyBindings::update_scripts_in_entries( true ); // update dirty only
    PyBindings::get_scripts( ScriptCategory::IMPORTER, m_importers );

    // COLUMN VALUES
    // for( Script* p2script : *p2scripts )
    // {
    //     if( p2script->get_category() != Script::Category::IMPORTER ) continue;
    //
    //     vec_importer_name.push_back( p2script->get_name() );
    //     m_importers.push_back( p2script );
    // }

    m_DD_type->set_model( Gtk::StringList::create( m_importers ) );
    // m_DD_type->set_selected( i_selected );

    Lifeograph::FINISH_INTERNAL_OPERATIONS();
}

// void
// DialogImport::handle_importer_changed()
// {
//     m_2importer_cur = m_importers[ m_DD_type->get_selected() ];
//
//     if( m_p2W_options )
//     {
//         m_Bx_contents->remove( *m_p2W_options );
//     }
//
//     m_p2W_options = m_p2importer_cur->draw_options();
//
//     if( m_p2W_options )
//         m_Bx_contents->append( *m_p2W_options );
//
//     handle_readiness_changed();
// }

// void
// DialogImport::handle_readiness_changed()
// {
//     m_F_importer_ready = m_p2importer_cur->is_ready();
//
//     m_B_import->set_sensitive( m_F_importer_ready );
// }

void
DialogImport::handle_go()
{
    PyBindings::run_script_name( m_importers[ m_DD_type->get_selected() ],
                                 const_cast< const String& >( m_import_path ) );
    hide();
}

