/*

    SPDX-FileCopyrightText: 2003-2009 Sebastian Trueg <trueg@k3b.org>
    SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "k3bexternalencoderconfigwidget.h"
#include "k3bcore.h"
#include "k3bplugin_i18n.h"

#include <KConfig>
#include <KMessageBox>

#include <QDebug>
#include <QList>
#include <QCheckBox>
#include <QTreeWidget>

K3B_EXPORT_PLUGIN_CONFIG_WIDGET( kcm_k3bexternalencoder, K3bExternalEncoderSettingsWidget )


K3bExternalEncoderEditDialog::K3bExternalEncoderEditDialog( QWidget* parent )
    : QDialog( parent )
{
    setModal( true );
    setWindowTitle( i18n("Editing external audio encoder") );
    setupUi( this );
}


K3bExternalEncoderEditDialog::~K3bExternalEncoderEditDialog()
{
}


K3bExternalEncoderCommand K3bExternalEncoderEditDialog::currentCommand() const
{
    K3bExternalEncoderCommand cmd;
    cmd.name = m_editName->text();
    cmd.extension = m_editExtension->text();
    cmd.command = m_editCommand->text();
    cmd.swapByteOrder = m_checkSwapByteOrder->isChecked();
    cmd.writeWaveHeader = m_checkWriteWaveHeader->isChecked();
    return cmd;
}


void K3bExternalEncoderEditDialog::setCommand( const K3bExternalEncoderCommand& cmd )
{
    m_editName->setText( cmd.name );
    m_editExtension->setText( cmd.extension );
    m_editCommand->setText( cmd.command );
    m_checkSwapByteOrder->setChecked( cmd.swapByteOrder );
    m_checkWriteWaveHeader->setChecked( cmd.writeWaveHeader );
}


void K3bExternalEncoderEditDialog::accept()
{
    if( m_editName->text().isEmpty() ) {
        KMessageBox::error( this,
                            i18n("Please specify a name for the command."),
                            i18n("No name specified") );
    }
    else if( m_editExtension->text().isEmpty() ) {
        KMessageBox::error( this,
                            i18n("Please specify an extension for the command."),
                            i18n("No extension specified") );
    }
    else if( m_editCommand->text().isEmpty() ) {
        KMessageBox::error( this,
                            i18n("Please specify the command line."),
                            i18n("No command line specified") );
    }
    else if( !m_editCommand->text().contains( "%f" ) ) {
        KMessageBox::error( this,
                            // xgettext: no-c-format
                            i18n("Please add the output filename (%f) to the command line."),
                            i18n("No filename specified") );
    }
    // FIXME: check for name and extension uniqueness
    else {
        QDialog::accept();
    }
}



K3bExternalEncoderSettingsWidget::K3bExternalEncoderSettingsWidget(QObject *parent, const KPluginMetaData& metaData, const QVariantList& args )
    : K3b::PluginConfigWidget( parent, metaData, args )
{
    setupUi( widget() );

    connect( m_viewEncoders, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
             this, SLOT(slotSelectionChanged(QTreeWidgetItem*)) );
    connect( m_viewEncoders, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
             this, SLOT(slotEditCommand()) );
    connect( m_buttonAdd, SIGNAL(clicked()),
             this, SLOT(slotNewCommand()) );
    connect( m_buttonEdit, SIGNAL(clicked()),
             this, SLOT(slotEditCommand()) );
    connect( m_buttonRemove, SIGNAL(clicked()),
             this, SLOT(slotRemoveCommand()) );

    m_editDlg = new K3bExternalEncoderEditDialog( widget() );
    slotSelectionChanged( nullptr );
}


K3bExternalEncoderSettingsWidget::~K3bExternalEncoderSettingsWidget()
{
}


void K3bExternalEncoderSettingsWidget::slotNewCommand()
{
    // clear the dialog
    m_editDlg->setCommand( K3bExternalEncoderCommand() );

    if( m_editDlg->exec() == QDialog::Accepted ) {
        K3bExternalEncoderCommand cmd = m_editDlg->currentCommand();
        createItem( cmd );
        setNeedsSave( true );
    }
}


void K3bExternalEncoderSettingsWidget::slotSelectionChanged( QTreeWidgetItem* current )
{
    m_buttonRemove->setEnabled( current != nullptr );
    m_buttonEdit->setEnabled( current != nullptr );
}


void K3bExternalEncoderSettingsWidget::slotEditCommand()
{
    if( QTreeWidgetItem* item = m_viewEncoders->currentItem() ) {
        m_editDlg->setCommand( m_commands[item] );
        if( m_editDlg->exec() == QDialog::Accepted ) {
            m_commands[item] = m_editDlg->currentCommand();
            fillItem( item, m_editDlg->currentCommand() );
            setNeedsSave( true );
        }
    }
}


void K3bExternalEncoderSettingsWidget::slotRemoveCommand()
{
    if( QTreeWidgetItem* item = m_viewEncoders->currentItem() ) {
        m_commands.remove( item );
        delete item;
        setNeedsSave( true );
    }
}


void K3bExternalEncoderSettingsWidget::load()
{
    qDebug();
    fillEncoderView( K3bExternalEncoderCommand::readCommands() );
}


void K3bExternalEncoderSettingsWidget::save()
{
    qDebug();
    K3bExternalEncoderCommand::saveCommands( m_commands.values() );
    setNeedsSave( false );
}


void K3bExternalEncoderSettingsWidget::defaults()
{
    qDebug();
    fillEncoderView( K3bExternalEncoderCommand::defaultCommands() );
    setNeedsSave( true );
}


QTreeWidgetItem* K3bExternalEncoderSettingsWidget::createItem( const K3bExternalEncoderCommand& cmd )
{
    QTreeWidgetItem* item = new QTreeWidgetItem( m_viewEncoders );
    fillItem( item, cmd );
    m_commands.insert( item, cmd );
    return item;
}


void K3bExternalEncoderSettingsWidget::fillItem( QTreeWidgetItem* item, const K3bExternalEncoderCommand& cmd )
{
    item->setText( 0, cmd.name );
    item->setText( 1, cmd.extension );
    item->setText( 2, cmd.command );
}


void K3bExternalEncoderSettingsWidget::fillEncoderView( const QList<K3bExternalEncoderCommand>& commands )
{
    m_commands.clear();
    m_viewEncoders->clear();

    QList<K3bExternalEncoderCommand> cmds( commands );
    for( QList<K3bExternalEncoderCommand>::iterator it = cmds.begin();
         it != cmds.end(); ++it ) {
        K3bExternalEncoderCommand& cmd = *it;
        createItem( cmd );
    }
}

#include "k3bexternalencoderconfigwidget.moc"

#include "moc_k3bexternalencoderconfigwidget.cpp"
