 
#include "lintvmixer.h"
#include "lintvprocess.h"

#include <kdebug.h>

#include <QTextStream>
#include <QRegExp>
#define GET_SIMPLE_CONTROLS 0

LinTVMixer::LinTVMixer(QObject *parent)
    : QObject(parent)
{
    m_audioInput = "Line";
    m_volume = 80;
    m_mute = false;

    loadCommon();
}

LinTVMixer::LinTVMixer(QString audioInput, int volume, QObject *parent)
    : QObject(parent),
      m_audioInput(audioInput),
      m_volume(volume)
{
    loadCommon();
}

LinTVMixer::~LinTVMixer()
{
    m_amixerProcess->terminate();

    delete m_amixerProcess;
}

void LinTVMixer::loadCommon()
{
    m_amixerProcess = new LinTVProcess(this);
    connect(m_amixerProcess, SIGNAL(receivedStdout(KProcess *, char *, int )),
            this, SLOT(receivedStdout(KProcess *, char *, int )));

    setVolume(m_volume, false);
}

void LinTVMixer::setAudioInput(QString audioInput)
{
    if (m_mute) {
        return;
    }

    m_audioInput = audioInput;

    m_amixerProcess->clearProgram();
    *m_amixerProcess << "amixer";
    *m_amixerProcess << "sset";
    *m_amixerProcess << QString("%1").arg(m_audioInput);
    *m_amixerProcess << QString("%2%%").arg(m_volume);
    *m_amixerProcess << "unmute";
    *m_amixerProcess << "cap";
    m_amixerProcess->start();
    m_amixerProcess->waitForFinished();
}

void LinTVMixer::setVolume(int volume, bool relative)
{
    if (relative) {
        m_volume += volume;
    } else {
        m_volume = volume;
    }

    if (m_volume < 0) {
        m_volume = 0;
    }

    //sendCmd(QString("sset %1 %2%% unmute").arg(m_mixer).arg(m_volume);
    m_amixerProcess->clearProgram();
    *m_amixerProcess << "amixer";
    *m_amixerProcess << "sset";
    *m_amixerProcess << QString("%1").arg(m_audioInput);
    *m_amixerProcess << QString("%2%%").arg(m_volume);
    if (m_mute) {
        *m_amixerProcess << "mute";
    } else {
        *m_amixerProcess << "unmute";
    }
    m_amixerProcess->start();
    m_amixerProcess->waitForFinished();
}

void LinTVMixer::mute()
{
    //sendCmd(QString("sset %1 mute").arg(m_mixer));
    m_amixerProcess->clearProgram();
    *m_amixerProcess << "amixer";
    *m_amixerProcess << "sset";
    *m_amixerProcess << QString("%1").arg(m_audioInput);
    *m_amixerProcess << "mute";
    m_amixerProcess->start();
    m_amixerProcess->waitForFinished();

    m_mute = true;
}

void LinTVMixer::unmute()
{
    //sendCmd(QString("sset %1 unmute").arg(m_mixer));
    m_amixerProcess->clearProgram();
    *m_amixerProcess << "amixer";
    *m_amixerProcess << "sset";
    *m_amixerProcess << QString("%1").arg(m_audioInput);
    *m_amixerProcess << "unmute";
    m_amixerProcess->start();
    m_amixerProcess->waitForFinished();

    m_mute = false;
}

void LinTVMixer::setMuted(bool muted)
{
    if (muted) {
        mute();
    } else {
        unmute();
    }
}

/*QStringList LinTVMixer::simpleMixers()
{
    m_amixerProcess->clearProgram();
    //proc.setOutputChannelMode(KProcess::ForwardedChannels);
    *m_amixerProcess << "amixer";
    *m_amixerProcess << "scontrols";


    m_amixerProcess->start();
    m_amixerProcess->waitForFinished();
    
    return m_mixers;
}*/

void LinTVMixer::receivedStdout(KProcess *, char *buf, int buflen)
{
    QString buffer = QString::fromLatin1(buf, buflen);
    /*if (proc->program()[0] == "amixer" && proc->program()[1] == "scontrols") {
        
        QString *scontrols = new QString(buffer);
        QTextStream stream(scontrols);
        QRegExp rx("[A-Za-z\\s_]+[']+([A-Za-z]*)+[',]+([0-9_])");

        kDebug() << buffer;

        while (!stream.atEnd()) {
            QString line = stream.readLine();

            kDebug() << line;

            if (rx.indexIn(line) == -1) {
                continue;
            }
            QString mixer = rx.cap(1).trimmed();

            kDebug() << mixer;

            m_mixers << mixer;
        }
    }*/
}