#include "AvReceiver.h"
#include "AvReceiverNetworkCtrlInterface.h"

CAvReceiver::CAvReceiver(CAvReceiverGui *ReceiverGui)
{
    mReceiverGui = ReceiverGui;
    ReceiverGui->mProgramHandle = this;
    mNetworkInterface = new CAvReceiverNetworkCtrlInterface(this);

    mReceiverPollTimer = new QTimer();
    mReceiverPollTimer->setSingleShot(false);
    mReceiverPollTimer->setInterval(1000);
    connect(mReceiverPollTimer,SIGNAL(timeout()),this,SLOT(PollTimerExpired()));

    connect(&mVolumeController,&CVolumeController::ExternalVolumeChanged,this,&CAvReceiver::ExternalVolumeChanged);
    mTempReceiverVolume = 0;
}

CAvReceiver::~CAvReceiver()
{
    delete mNetworkInterface;
    delete mReceiverPollTimer;
}

int CAvReceiver::Start()
{
    mNetworkInterface->ConnectToMasterCtrl();
    mReceiverPollTimer->start();
    mVolumeController.Init(this);
    return RET_OK;
}


int CAvReceiver::Zone2ToggleSwitchPressed(bool state)
{

    QByteArray ZoneState;
    ZoneState.clear();

    if(state)
    {
        ZoneState.append((char)0x01);
    }
    else
    {
        ZoneState.append((char)0x00);
    }
    return mNetworkInterface->SendMasterCtrlCommand(AV_RECEIVER_INTERFACE_SET_ZONE2_REQUEST,ZoneState);

}


int CAvReceiver::MainUnitToggleSwitchPressed(bool state)
{

    QByteArray ZoneState;
    ZoneState.clear();

    if(state)
    {
        ZoneState.append((char)0x01);
    }
    else
    {
        ZoneState.append((char)0x00);
    }
    return mNetworkInterface->SendMasterCtrlCommand(AV_RECEIVER_INTERFACE_SET_MAIN_ZONE_REQUEST,ZoneState);

}

void CAvReceiver::PollTimerExpired()
{
    mNetworkInterface->SendMasterCtrlCommand(AV_RECEIVER_INTERFACE_GENERAL_STATUS_REQUEST,QByteArray());
}

int CAvReceiver::ReceiverGeneralStatusReceived(QByteArray StatusData)
{
    QDataStream Strm(&StatusData,QIODevice::ReadOnly | QIODevice::Unbuffered);
    Strm.device()->seek(0);

    Strm >> mReceiverStatus;
    Strm >> mZone2Status;
    mReceiverGui->UpdateReceiverStatus(mReceiverStatus, mZone2Status);
    mTempReceiverVolume = mReceiverStatus.mMainVolume;
    return RET_OK;
}
int CAvReceiver::SelectScenePressed(char Zone, char Scene)
{
    QByteArray SceneData;
    SceneData.append(Zone).append(Scene);

    return mNetworkInterface->SendMasterCtrlCommand(AV_RECEIVER_INTERFACE_SELECT_SCENE_REQUEST,SceneData);
}

int CAvReceiver::MainZoneVolumeChanged(float Value)
{
//    qDebug("MainZoneVolumeChanged : %f",Value);
    QByteArray VolumeData;
    QDataStream Strm(&VolumeData,QIODevice::WriteOnly);
    Strm << Value;
    return mNetworkInterface->SendMasterCtrlCommand(AV_RECEIVER_INTERFACE_SET_MAIN_VOLUME_REQUEST,VolumeData);
}

int CAvReceiver::Zone2VolumeChanged(float Value)
{
    QByteArray VolumeData;
    QDataStream Strm(&VolumeData,QIODevice::WriteOnly);
    Strm << Value;
    return mNetworkInterface->SendMasterCtrlCommand(AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_REQUEST,VolumeData);
}

int CAvReceiver::SyncZonesVolumesChanged(bool Sync)
{
    QByteArray SyncData;
    QDataStream Strm(&SyncData,QIODevice::WriteOnly);
    Strm << Sync;
    return mNetworkInterface->SendMasterCtrlCommand(AV_RECEIVER_INTERFACE_SET_SYNC_Z2_WITH_Z1_REQUEST,SyncData);
}

int CAvReceiver::Zone2InputSrcChanged(QString InputSrc)
{
    QByteArray SourceData;
    QDataStream Strm(&SourceData,QIODevice::WriteOnly);
    Strm << InputSrc;
    return mNetworkInterface->SendMasterCtrlCommand(AV_RECEIVER_INTERFACE_SET_ZONE2_INPUT_REQUEST,SourceData);
}

//We need to implement this in a slot because the Windows API runs in a differen Thread and it crashes the MasterCtrl socket.
void CAvReceiver::ExternalVolumeChanged(float Value)
{
    float ValueInPercent;
    float Ret;
    if(Value > 0)
    {
        mTempReceiverVolume += Value;
        MainZoneVolumeChanged(mTempReceiverVolume);
        Ret = 0.04;
    }
    else
    {
        if(mTempReceiverVolume == -80.5)
        {
            Ret = 0;
        }
        else
        {
            mTempReceiverVolume += Value;
            if(mTempReceiverVolume == -80.5)
            {
                MainZoneVolumeChanged(mTempReceiverVolume);
                Ret = 0;
            }
            else
            {
                MainZoneVolumeChanged(mTempReceiverVolume);
                Ret = 0.04;
            }
        }
    }
    mVolumeController.SetMasterVolume(Ret);

}
