#include "AvReceiverData.h"
#include <QDataStream>

CAvReceiverMainStatus::CAvReceiverMainStatus()
{
   mDataValid = false;
   mReceiverOnline = false;
   mSyncZonesVolumes = true;
}

QByteArray CAvReceiverMainStatus::ToByteArray()
{

    QByteArray Output;
    Output.clear();
//    QDataStream Strm(&Output,QIODevice::WriteOnly);

////    Strm << mMainPwrStatus;
////    Strm <<  mMainSleepStatus;
////    Strm <<  mMainVolume;
////    Strm <<  mIsMute;
////    Strm <<  mInput;
////    Strm <<  mProgram;

////    Strm <<  mDataValid;
////    Strm <<  mReceiverOnline;
////    Strm << mSyncZonesVolumes;



}

QByteArray CAvReceiverMainStatus::ToConsoleStatusByteArray()
{
     QByteArray Output;

    if(mMainPwrStatus == true)  //0
        Output.append(0x01);
    else
        Output.append((char)0x00);

    if(mMainSleepStatus == true)        //1
        Output.append(0x01);
    else
        Output.append((char)0x00);

//    int Volume = ((mMainVolume + 80.5) * 2);
//    char VolArray[4];
//    memcpy(VolArray,&Volume,4);    //2-5
//    Output.append(VolArray,4);

    char Volume = ((mMainVolume + 80.5) * 2);   //2
    Output.append(Volume);

    if(mIsMute == true)         //3
        Output.append(0x01);
    else
        Output.append((char)0x00);


    if(mDataValid == true)      //4
        Output.append(0x01);
    else
        Output.append((char)0x00);

    if(mReceiverOnline == true) //5
        Output.append(0x01);
    else
        Output.append((char)0x00);

    if(mSyncZonesVolumes == true)   //6
        Output.append(0x01);
    else
        Output.append((char)0x00);

   return Output;
}

int CAvReceiverMainStatus::FromByteArray(QByteArray Data)
{
    QDataStream Strm(Data);
    Strm.device()->seek(0);

    Strm >> mMainPwrStatus;
    Strm >>  mMainSleepStatus;
    Strm >>  mMainVolume;
    Strm >>  mIsMute;
    Strm >>  mInput;
    Strm >>  mProgram;

    Strm >>  mDataValid;
    Strm >>  mReceiverOnline;
    Strm >> mSyncZonesVolumes;


    return RET_OK;

}

int CAvReceiverMainStatus::FromConsoleStatusByteArray(QByteArray Data)
{
    if(Data.at(0) == 1)
        mMainPwrStatus = true;
    else
        mMainPwrStatus = false;

    if(Data.at(1) == 1)
        mMainSleepStatus = true;
    else
        mMainSleepStatus = false;

     char VolArray[4];
     VolArray[0] = Data[2];
     VolArray[1] = Data[3];
     VolArray[2] = Data[4];
     VolArray[3] = Data[5];
     memcpy(&mMainVolume,VolArray,4);

    if(Data.at(6) == 1)
        mIsMute = true;
    else
        mIsMute = false;

//    char String[50];
//    for(int i= 0; i < 50; i++)
//    {
//        String[i] = Data[i+7];
//    }
//    mInput.clear();
//    mInput.append(String);

//    for(int i= 0; i < 50; i++)
//    {
//        String[i] = Data[i+57];
//    }
//    mProgram.clear();
//    mProgram.append(String);

    if(Data.at(7) == 1)
        mDataValid = true;
    else
        mDataValid = false;

    if(Data.at(8) == 1)
        mReceiverOnline = true;
    else
        mReceiverOnline = false;

    if(Data.at(9) == 1)
        mSyncZonesVolumes = true;
    else
        mSyncZonesVolumes = false;

    return RET_OK;
}


QDataStream &operator<<(QDataStream &out, const CAvReceiverMainStatus &source)
{
    out << source.mMainPwrStatus
        << source.mMainSleepStatus
        << source.mMainVolume
        << source.mIsMute
        << source.mInput
        << source.mProgram
        << source.mDataValid
        << source.mReceiverOnline
        << source.mSyncZonesVolumes;

    return out;
}

QDataStream &operator>>(QDataStream &in, CAvReceiverMainStatus &dest)
{
    in >> dest.mMainPwrStatus
       >> dest.mMainSleepStatus
       >> dest.mMainVolume
       >> dest.mIsMute
       >> dest.mInput
       >> dest.mProgram
       >> dest.mDataValid
       >> dest.mReceiverOnline
       >> dest.mSyncZonesVolumes;
    return in;
}
