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

    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 "../python/py_bindings.hpp"
#include "dialog_import.hpp"

using namespace LoG;

// DIALOG IMPORT ===================================================================================
String DialogImport::s_active_importer;
String DialogImport::s_import_uri;

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       = Gtk::Builder::get_widget_derived< DropDownAdvanced< String > >(
                            builder, "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->Sg_selection_changed.connect(
            sigc::mem_fun( *this, &DialogImport::handle_importer_changed ) );
    m_B_import->signal_clicked().connect( [ this ](){ handle_go(); } );
    B_cancel->signal_clicked().connect( [ this ](){ hide(); } );

    handle_readiness_changed();
}

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

    // UPDATE IMPORTER LIST
    VecUstrings importers;
    PyBindings::update_scripts_in_entries( true ); // update dirty only
    PyBindings::get_scripts( ScriptCategory::IMPORTER, importers );

    for( auto importer : importers )
        m_DD_type->add( importer, importer );

    // SET SELECTED
    if( s_active_importer.empty() ||
        !m_DD_type->select_by_id( s_active_importer ) )  // restore last selected
        m_DD_type->set_selected( 0 );

    if( !s_import_uri.empty() )
    {
        m_FCB_import->set_uri( s_import_uri );
        m_FCB_import->clear_uri( true ); // clear the uri but keep the initial folder
    }

    Lifeograph::FINISH_INTERNAL_OPERATIONS();
}

void
DialogImport::handle_importer_changed( String id )
{
    s_active_importer = id;

    // 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( !s_active_importer.empty() && !s_import_uri.empty() );
}

void
DialogImport::handle_go()
{
    PyBindings::run_script_name( s_active_importer, Glib::filename_from_uri( s_import_uri ) );
    hide();
}

