#include "alsadevicespage.h"
#include "../../model/alsamodel.h"
#include "../../model/alsadevice.h"
#include "../kledbutton.h"

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqscrollview.h>
#include <tqframe.h>
#include <tdelocale.h>

// ── AlsaSwitchRow ─────────────────────────────────────────────────────────

AlsaSwitchRow::AlsaSwitchRow( AlsaDevice *dev, TQWidget *parent )
    : TQWidget(parent), m_dev(dev)
{
    TQColor ledColor = ( dev->category() == AlsaDevice::Input ) ? TQt::red : TQt::yellow;
    m_led = new KLedButton( ledColor,
        dev->muted() ? KLed::Off : KLed::On,
        KLed::Sunken, KLed::Circular, this );
    m_led->setFixedSize(14, 14);

    TQHBoxLayout *hbox = new TQHBoxLayout(this, 2, 4);
    hbox->addWidget(m_led);
    hbox->addWidget( new TQLabel(dev->name(), this) );
    hbox->addStretch(1);

    connect(dev,   TQ_SIGNAL(muteChanged(bool)),   this, TQ_SLOT(onMuteChanged(bool)));
    connect(m_led, TQ_SIGNAL(stateChanged(bool)),  this, TQ_SLOT(onLedToggled(bool)));
}

void AlsaSwitchRow::onMuteChanged( bool m )
{
    m_led->blockSignals(true);
    m_led->setState( m ? KLed::Off : KLed::On );
    m_led->blockSignals(false);
}

void AlsaSwitchRow::onLedToggled( bool on )
{
    m_dev->setMuted( !on );
}

// ── AlsaEnumRow ──────────────────────────────────────────────────────────────

AlsaEnumRow::AlsaEnumRow( AlsaDevice *dev, TQWidget *parent )
    : TQWidget(parent), m_dev(dev)
{
    m_combo = new TQComboBox(false, this);
    TQStringList vals = dev->enumValues();
    for ( TQStringList::ConstIterator it = vals.begin(); it != vals.end(); ++it )
        m_combo->insertItem(*it);
    m_combo->setCurrentItem( dev->enumId() );

    TQHBoxLayout *hbox = new TQHBoxLayout(this, 2, 6);
    hbox->addWidget( new TQLabel(dev->name() + ":", this) );
    hbox->addWidget(m_combo, 1);

    connect(dev,     TQ_SIGNAL(enumChanged(int)), this, TQ_SLOT(onEnumChanged(int)));
    connect(m_combo, TQ_SIGNAL(activated(int)),   this, TQ_SLOT(onComboActivated(int)));
}

void AlsaEnumRow::onEnumChanged( int id )
{
    m_combo->blockSignals(true);
    m_combo->setCurrentItem(id);
    m_combo->blockSignals(false);
}

void AlsaEnumRow::onComboActivated( int idx )
{
    m_dev->setEnumId(idx);
}

// ── AlsaDevicesPage ──────────────────────────────────────────────────────────

AlsaDevicesPage::AlsaDevicesPage( AlsaModel *model, TQWidget *parent )
    : TQWidget(parent), m_model(model), m_cardIndex(-1), m_content(0)
{
    TQVBoxLayout *outer = new TQVBoxLayout(this, 8, 4);

    m_scroll = new TQScrollView(this);
    m_scroll->setResizePolicy( TQScrollView::AutoOneFit );
    m_scroll->setFrameStyle( TQFrame::NoFrame );
    outer->addWidget(m_scroll, 1);

    m_content = new TQWidget( m_scroll->viewport() );
    m_scroll->addChild(m_content);

    connect(model, TQ_SIGNAL(deviceAdded(AlsaDevice*)), this, TQ_SLOT(onDeviceAdded(AlsaDevice*)));
}

void AlsaDevicesPage::setCard( int cardIndex )
{
    m_cardIndex = cardIndex;
    rebuild();
}

void AlsaDevicesPage::rebuild()
{
    delete m_content;
    m_content = new TQWidget( m_scroll->viewport() );
    m_scroll->addChild(m_content);

    TQVBoxLayout *vbox = new TQVBoxLayout(m_content, 6, 4);

    if ( m_cardIndex < 0 ) {
        vbox->addWidget( new TQLabel( i18n("No card selected."), m_content ) );
        vbox->addStretch(1);
        m_content->show();
        return;
    }

    TQPtrList<AlsaDevice> devs = m_model->devicesForCard(m_cardIndex);
    bool hasSwitches = false, hasEnums = false;

    for ( TQPtrListIterator<AlsaDevice> it(devs); *it; ++it ) {
        if ( (*it)->type() == AlsaDevice::Switch ) hasSwitches = true;
        if ( (*it)->type() == AlsaDevice::Enum   ) hasEnums    = true;
    }

    if ( hasSwitches ) {
        TQLabel *hdr = new TQLabel( TQString("<b>%1</b>").arg(i18n("Switches")), m_content );
        vbox->addWidget(hdr);
        for ( TQPtrListIterator<AlsaDevice> it(devs); *it; ++it ) {
            if ( (*it)->type() == AlsaDevice::Switch ) {
                AlsaSwitchRow *row = new AlsaSwitchRow( *it, m_content );
                vbox->addWidget(row);
            }
        }
        if ( hasEnums )
            vbox->addSpacing(8);
    }

    if ( hasEnums ) {
        TQLabel *hdr = new TQLabel( TQString("<b>%1</b>").arg(i18n("Routing")), m_content );
        vbox->addWidget(hdr);
        for ( TQPtrListIterator<AlsaDevice> it(devs); *it; ++it ) {
            if ( (*it)->type() == AlsaDevice::Enum ) {
                AlsaEnumRow *row = new AlsaEnumRow( *it, m_content );
                vbox->addWidget(row);
            }
        }
    }

    if ( !hasSwitches && !hasEnums ) {
        vbox->addWidget( new TQLabel( i18n("No device settings available for this card."), m_content ) );
    }

    vbox->addStretch(1);
    m_content->show();
    m_content->adjustSize();
}

void AlsaDevicesPage::onDeviceAdded( AlsaDevice * )
{
    rebuild();
}

#include "alsadevicespage.moc"
