#include "AvReceiverGui.h"
#include "ui_AvReceiverGui.h"
#include "TrayVolumeCtrl.h"

#include "AvReceiver.h"

CAvReceiverGui::CAvReceiverGui(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CAvReceiverGui)
{
    ui->setupUi(this);
    mVolumeBarMovementTimer = new QTimer();
    mVolumeBarMovementTimer->setInterval(500);
    mVolumeBarMovementTimer->setSingleShot(true);
    mLockZone2VolumeWithZone1 = false;

    mTrayVolumeCtrlGuiHandle = 0;


    ui->mZone2InputComboBx->addItems(QStringList() << "AUDIO1" << "AUDIO2" << "AUDIO3" << "AUDIO4" << "AUDIO5" << "PHONO"  << "TUNER" << "SERVER" << "Main Zone Sync");
    ui->mZone2InputComboBx->setEditable(false);

    connect(mVolumeBarMovementTimer,SIGNAL(timeout()),this,SLOT(VolumeBarMovementTimerExpired()));
    connect(ui->mSpkBCheckBox,SIGNAL(clicked(bool)),this,SLOT(SpeakerBRadioClicked(bool)));
    connect(ui->mSpkACheckBox,SIGNAL(clicked(bool)),this,SLOT(SpeakerARadioClicked(bool)));
    connect(ui->MainZoneScene1Btn,SIGNAL(clicked(bool)),this,SLOT(MainZoneScene1BtnClicked(bool)));
    connect(ui->MainZoneScene2Btn,SIGNAL(clicked(bool)),this,SLOT(MainZoneScene2BtnClicked(bool)));
    connect(ui->MainZoneScene3Btn,SIGNAL(clicked(bool)),this,SLOT(MainZoneScene3BtnClicked(bool)));
    connect(ui->MainZoneScene4Btn,SIGNAL(clicked(bool)),this,SLOT(MainZoneScene4BtnClicked(bool)));
    connect(ui->mMainZoneVolumeSldBar,SIGNAL(valueChanged(int)),this,SLOT(MainZoneVolumeSetChanged(int)));
    connect(ui->mZone2VolumeSldBar,SIGNAL(valueChanged(int)),this,SLOT(Zone2VolumeSetChanged(int)));
    connect(ui->mZone2InputComboBx,&QComboBox::currentTextChanged,this,&CAvReceiverGui::Zone2InputSelectionChanged);
    connect(ui->mZone2SyncVolumeChkBx,&QCheckBox::clicked,this,&CAvReceiverGui::Zone2LockVolumeChanged);
}

CAvReceiverGui::~CAvReceiverGui()
{
    delete ui;
    delete mVolumeBarMovementTimer;
}

void CAvReceiverGui::SpeakerBRadioClicked(bool checked)
{
    mProgramHandle->Zone2ToggleSwitchPressed(checked);
}

void CAvReceiverGui::SpeakerARadioClicked(bool checked)
{
    mProgramHandle->MainUnitToggleSwitchPressed(checked);
}

int CAvReceiverGui::UpdateReceiverStatus(CAvReceiverMainStatus Status, CAvReceiverMainStatus Zone2Status)
{
    QString StatusText;

    StatusText.clear();
    StatusText += "Main Receiver Status:\n\n";

    StatusText += "Power: ";
    if(Status.mMainPwrStatus == true)
    {
        StatusText += "ON\n";
        ui->mSpkACheckBox->setChecked(true);
    }
    else
    {
        StatusText += "OFF\n";
        ui->mSpkACheckBox->setChecked(false);
    }

    StatusText += "Mute: ";
    if(Status.mIsMute == true)
    {
        StatusText += "ON\n";
    }
    else
    {
        StatusText += "OFF\n";
    }

    StatusText += "Main sleep: ";
    if(Status.mMainSleepStatus == true)
    {
        StatusText += "ON\n";
    }
    else
    {
        StatusText += "OFF\n";
    }

    StatusText += "Volume: ";
    StatusText += QString("%1").arg(Status.mMainVolume);
    StatusText +="\n";
    StatusText += "Input: ";
    StatusText +=Status.mInput;
    StatusText += "\n";
    StatusText += "Program: ";
    StatusText +=Status.mProgram;
    StatusText += "\n";

    ui->mRcvrStatusLabel->setText(StatusText);

    if(/*ui->mMainZoneVolumeSldBar->isSliderDown() == false  && */mVolumeBarMovementTimer->isActive() == false)
    {
        int SliderValue = ConvertVolumeToBarPosition(Status.mMainVolume);
        disconnect(ui->mMainZoneVolumeSldBar,SIGNAL(valueChanged(int)),this,SLOT(MainZoneVolumeSetChanged(int)));
        ui->mMainZoneVolumeSldBar->setValue(SliderValue);
        connect(ui->mMainZoneVolumeSldBar,SIGNAL(valueChanged(int)),this,SLOT(MainZoneVolumeSetChanged(int)));
        ui->mMainZoneSliderValueLbl->setText("");
        mTrayVolumeCtrlGuiHandle->SetMainZoneVolume(SliderValue,Status.mMainVolume);
    }





    StatusText = "Zone2 Status\n\n";

    StatusText += "Power: ";
    if(Zone2Status.mMainPwrStatus == true)
    {
        StatusText += "ON\n";
        ui->mSpkBCheckBox->setChecked(true);
    }
    else
    {
        StatusText += "OFF\n";
        ui->mSpkBCheckBox->setChecked(false);
    }

    StatusText += "Mute: ";
    if(Zone2Status.mIsMute == true)
    {
        StatusText += "ON\n";
    }
    else
    {
        StatusText += "OFF\n";
    }

    StatusText += "Main sleep: ";
    if(Zone2Status.mMainSleepStatus == true)
    {
        StatusText += "ON\n";

    }
    else
    {
        StatusText += "OFF\n";
    }

    StatusText += "Volume: ";
    StatusText += QString("%1").arg(Zone2Status.mMainVolume);
    StatusText +="\n";
    StatusText += "Input: ";
    StatusText +=Zone2Status.mInput;
    StatusText += "\n";
    StatusText += "Program: ";
    StatusText +=Zone2Status.mProgram;
    StatusText += "\n";

    ui->mZone2StatusLabel->setText(StatusText);
    ui->mZone2InputComboBx->setCurrentText(Zone2Status.mInput);

    if(/*ui->mZone2VolumeSldBar->isSliderDown() == false  && */mVolumeBarMovementTimer->isActive() == false)
    {
        int SliderValue = ConvertVolumeToBarPosition(Zone2Status.mMainVolume);
        disconnect(ui->mZone2VolumeSldBar,SIGNAL(valueChanged(int)),this,SLOT(Zone2VolumeSetChanged(int)));
        ui->mZone2VolumeSldBar->setValue(SliderValue);
        connect(ui->mZone2VolumeSldBar,SIGNAL(valueChanged(int)),this,SLOT(Zone2VolumeSetChanged(int)));
        ui->mZone2SliderValueLbl->setText("");
        mTrayVolumeCtrlGuiHandle->SetZone2Volume(SliderValue,Zone2Status.mMainVolume);
    }

    if(Status.mSyncZonesVolumes)
        ui->mZone2SyncVolumeChkBx->setCheckState(Qt::Checked);
    else
        ui->mZone2SyncVolumeChkBx->setCheckState(Qt::Unchecked);

    return RET_OK;
}

void CAvReceiverGui::MainZoneScene1BtnClicked(bool)
{
    mProgramHandle->SelectScenePressed(AV_RECEIVER_MAIN_ZONE,1);
}

void CAvReceiverGui::MainZoneScene2BtnClicked(bool)
{
    mProgramHandle->SelectScenePressed(AV_RECEIVER_MAIN_ZONE,2);
}
void CAvReceiverGui::MainZoneScene3BtnClicked(bool)
{
    mProgramHandle->SelectScenePressed(AV_RECEIVER_MAIN_ZONE,3);
}
void CAvReceiverGui::MainZoneScene4BtnClicked(bool)
{
    mProgramHandle->SelectScenePressed(AV_RECEIVER_MAIN_ZONE,4);
}

int CAvReceiverGui::ConvertVolumeToBarPosition(float Volume)
{
    int Pos;
    Pos = (int)((Volume + 80.5) *2);
    return Pos;
}

float CAvReceiverGui::ConvertBarPositionToVolume(int position)
{
    float Volume;
    Volume = (float)position;
    Volume = ((Volume/2) - 80.5);

    return Volume;
}

void CAvReceiverGui::MainZoneVolumeSetChanged(int value)
{
//    int BarPosition = ui->mMainZoneVolumeSldBar->value();
    int BarPosition = value;
    float Volume = ConvertBarPositionToVolume(BarPosition);

    ui->mMainZoneSliderValueLbl->setText(QString("%1").arg(Volume));
    mProgramHandle->MainZoneVolumeChanged(Volume);


//    if(mLockZone2VolumeWithZone1 == true)
    if(ui->mZone2SyncVolumeChkBx->checkState() == Qt::Checked)
    {
        ui->mZone2VolumeSldBar->setValue(BarPosition);
//        mVolumeBarMovementTimer->start();
//        //mProgramHandle->Zone2VolumeChanged(Volume);
    }
}

void CAvReceiverGui::Zone2VolumeSetChanged(int value)
{
//    int BarPosition = ui->mZone2VolumeSldBar->value();
    int BarPosition = value;
    float Volume = ConvertBarPositionToVolume(BarPosition);

    ui->mZone2SliderValueLbl->setText(QString("%1").arg(Volume));
    mProgramHandle->Zone2VolumeChanged(Volume);
   // mVolumeBarMovementTimer->start();

}

void CAvReceiverGui::VolumeBarMovementTimerExpired()
{
    int BarPosition = ui->mMainZoneVolumeSldBar->value();
    float Volume = ConvertBarPositionToVolume(BarPosition);
    mProgramHandle->Zone2VolumeChanged(Volume);
}

void CAvReceiverGui::Zone2LockVolumeChanged(bool checked)
{
    mLockZone2VolumeWithZone1 = checked;
    if(checked)
    {
//        //Set Zone 2 volume to main zone value
//        int BarPosition = ui->mMainZoneVolumeSldBar->value();
//        float Volume = ConvertBarPositionToVolume(BarPosition);

//        mProgramHandle->Zone2VolumeChanged(Volume);

        ui->mZone2VolumeSldBar->setEnabled(false);

        mProgramHandle->SyncZonesVolumesChanged(true);
    }
    else
    {
        ui->mZone2VolumeSldBar->setEnabled(true);
        mProgramHandle->SyncZonesVolumesChanged(false);
    }

}

void CAvReceiverGui::Zone2InputSelectionChanged()
{
    QString Src = ui->mZone2InputComboBx->currentText();
    mProgramHandle->Zone2InputSrcChanged(Src);
}
