#include "define.h"
#include "AudioConsole.h"
#include "timer.h"

#include "ProtocolDefs.h"
#include "NetworkProtocol.h"
#include "VUMeter.h"
#include "PushBtnMgr.h"
#include "AudioConsoleLedMgr.h"

AVReceiverStatus_t mMainZoneStatus, mZone2Status;
bool mMasterInterfaceConnected;

int mAudioConsoleState;
int mAudioConsoleStatusRetryCount;

int AudioConsoleInit()
{
    ProtocolInit(NETWORK_PROTOCOL_USER_AUDIO_CONSOLE_IF);
    mMasterInterfaceConnected = false;
    mAudioConsoleState = AUDIO_CONSOLE_INIT_STATE;
    TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_WIFI_TIMEOUT);
    mAudioConsoleStatusRetryCount = 0;
    mZone2Status.MainVolume = 0;
    mMainZoneStatus.MainVolume = 0;
}

int AudioConsoleSetReceiverStatus(AVReceiverStatus_t *MainZone, AVReceiverStatus_t *Zone2)
{
    mAudioConsoleStatusRetryCount = 0;
    
    mMainZoneStatus.MainPwrStatus = MainZone ->MainPwrStatus;
    mMainZoneStatus.MainSleepStatus = MainZone->MainSleepStatus;
    mMainZoneStatus.IsMute = MainZone->IsMute;
    mMainZoneStatus.DataValid = MainZone->DataValid;
    mMainZoneStatus.ReceiverOnline = MainZone->ReceiverOnline;
    mMainZoneStatus.SyncZones = MainZone->SyncZones;
    mMainZoneStatus.MainVolume = MainZone->MainVolume;
    
    mZone2Status.MainPwrStatus = Zone2 ->MainPwrStatus;
    mZone2Status.MainSleepStatus = Zone2->MainSleepStatus;
    mZone2Status.IsMute = Zone2->IsMute;
    mZone2Status.DataValid = Zone2->DataValid;
    mZone2Status.ReceiverOnline = Zone2->ReceiverOnline;
    mZone2Status.SyncZones = Zone2->SyncZones;
    mZone2Status.MainVolume = Zone2->MainVolume;
    
    VUMeterSetVolume(mMainZoneStatus.MainVolume,mZone2Status.MainVolume);
    
   
    UpdatePushButtonLEDs(&mMainZoneStatus,&mZone2Status);
    return RET_OK;
}

int AudioConsoleSM(int Event)
{
    switch(mAudioConsoleState)
    {
        case AUDIO_CONSOLE_INIT_STATE:
        {
            //At boot, we wait for wifi to connect first...
            if(IsTimerExpired(CONSOLE_IF_CONNECT_TIMER))
            {
                if(IsWiFiConnected() == true)
                {
                    ConnectAudioInterface();
                    mAudioConsoleState = AUDIO_CONSOLE_CONNECTING_STATE;
                    TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_CONNECT_TIMEOUT);
                }
                else
                {
                    TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_WIFI_TIMEOUT);
                }
            }
            break;
        }
        case AUDIO_CONSOLE_DISCONNECTED_STATE:
        {
            switch(Event)
            {
                case AUDIO_CONSOLE_SM_TICK_EVENT:
                {
                    if(IsWiFiConnected() == true)
                    {
                        if(ConnectAudioInterface() == RET_ERROR)
                        {
                            //Socket don't work anymore... not shure what to do.
                            int toto = 1;
                            toto ++;
                        }
                        mAudioConsoleState = AUDIO_CONSOLE_CONNECTING_STATE;
                        TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_CONNECT_TIMEOUT);
                    }
                    else
                    {
                        TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_WIFI_TIMEOUT);
                    }
                    break;
                }
                case AUDIO_CONSOLE_SM_CONNECTED_EVENT:
                {
                    TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_STATUS_REQUEST_TIMEOUT);
                    RequestStatus();
                    mAudioConsoleState = AUDIO_CONSOLE_RUN_STATE;
                    break;
                }
                case AUDIO_CONSOLE_SM_DISCONNECTED_EVENT:
                {
                    break;
                }
            }
            break;
        }
        case AUDIO_CONSOLE_CONNECTING_STATE:
        {
            switch(Event)
            {
                case AUDIO_CONSOLE_SM_TICK_EVENT:
                {
                    if(IsWiFiConnected() == false)
                    {
                        AudioConsoleDisconnected();                        
                    }
                    else if(IsTimerExpired(CONSOLE_IF_CONNECT_TIMER))
                    {
                        AudioConsoleDisconnected(); //Retry to connect...
                    }
                    break;
                }
                case AUDIO_CONSOLE_SM_CONNECTED_EVENT:
                {
                    TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_STATUS_REQUEST_TIMEOUT);
                    RequestStatus();
                    mAudioConsoleState = AUDIO_CONSOLE_RUN_STATE;
                    LedMgrShowOfflineMode(false);
                    break;
                }
                case AUDIO_CONSOLE_SM_DISCONNECTED_EVENT:
                {
                    AudioConsoleDisconnected();
                    break;
                }
            }
            break;
        }
        case AUDIO_CONSOLE_RUN_STATE:
        {
            switch(Event)
            {
                case AUDIO_CONSOLE_SM_TICK_EVENT:
                {
                    if(IsAudioInterfaceConnected() == false  || IsWiFiConnected() == false)
                    {
                        
                        AudioConsoleDisconnected();
                    }
                    else if(IsTimerExpired(CONSOLE_IF_CONNECT_TIMER))
                    {
                        if(RequestStatus() == RET_OK)
                        {
                            TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_STATUS_REQUEST_TIMEOUT);
                        }
                        else
                        {
                            AudioConsoleDisconnected();
                        }
                        
                    }
                    break;
                }
                case AUDIO_CONSOLE_SM_CONNECTED_EVENT: 
                {
                    //Should not happen....
                    break;
                }
                case AUDIO_CONSOLE_SM_DISCONNECTED_EVENT:
                {
                    AudioConsoleDisconnected();
                    
                    break;
                }
            }
            break;
        }
    }
}

void AudioConsoleTick()
{
    AudioConsoleSM(AUDIO_CONSOLE_SM_TICK_EVENT);
}

int AudioConsoleSetIFConnectionStatus(bool Connected)
{
    if(Connected == true)
    {
        ONBOARD_LED1_PIN = LED_ON;
        AudioConsoleSM(AUDIO_CONSOLE_SM_CONNECTED_EVENT);
    }
    else
    {
        ONBOARD_LED1_PIN = LED_OFF;
        AudioConsoleSM(AUDIO_CONSOLE_SM_DISCONNECTED_EVENT);
    }
}

int RequestStatus()
{
    if(mAudioConsoleStatusRetryCount >= AUDIO_CONSOLE_MAX_STATUS_RETRY)
    {
        return RET_ERROR;
    }
    
    mAudioConsoleStatusRetryCount++;
    AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_GENERAL_STATUS_REQUEST,0,0);
    return RET_OK;
}


int SendMainVolumeToMaster()
{
    char Volume = mMainZoneStatus.MainVolume;
    AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_MAIN_VOLUME_REQUEST,&Volume,1);
    return RET_OK;
}
int SendZone2VolumeToMaster()
{
    char Volume = mZone2Status.MainVolume;
    AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_REQUEST,&Volume,1);
    return RET_OK;
}
int SendIncrementMainVolume(bool Increment)
{
    char data;
    if(Increment == true)
    {
        data = 1;
    }
    else
    {
        data = 0;
    }
    AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_INCREMENT_MAIN_VOLUME_REQUEST,&data,1);
    return RET_OK;
}
int SendIncrementZ2Volume(bool Increment)
{
    char data;
    if(Increment == true)
    {
        data = 1;
    }
    else
    {
        data = 0;
    }
    AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_INCREMENT_Z2_VOLUME_REQUEST,&data,1);
    return RET_OK;
}

int AudioConsoleDisconnected()
{
    //TODO reset console display
    ONBOARD_LED1_PIN = LED_OFF;
    mAudioConsoleStatusRetryCount = 0;
    DisconnectAudioInterface();
    mAudioConsoleState = AUDIO_CONSOLE_DISCONNECTED_STATE;
    TimerStop(CONSOLE_IF_CONNECT_TIMER);
    LedMgrShowOfflineMode(true);
}

int AudioConsoleVolumeUp(int Zone)
{
    switch(Zone)
    {
        case AUDIO_CONSOLE_SALON_ZONE:
        {
            SendIncrementMainVolume(true);
            break;
        }
        case AUDIO_CONSOLE_CUISINE_ZONE:
        {
            SendIncrementZ2Volume(true);
            break;
        }
    }
    RequestStatus();
    return RET_OK;
}
int AudioConsoleVolumeDown(int Zone)
{
    switch(Zone)
    {
        
        
        case AUDIO_CONSOLE_SALON_ZONE:
        {
            SendIncrementMainVolume(false);
            break;
        }
        case AUDIO_CONSOLE_CUISINE_ZONE:
        {
            SendIncrementZ2Volume(false);
            break;
        }
       
    }
     RequestStatus();
    return RET_OK;
}

int LinkZ2BtnPressed()
{
    if(mMainZoneStatus.SyncZones == true)
    {
        SendLinkZ2VolumeToMaster(false);
    }
    else
    {
        SendLinkZ2VolumeToMaster(true);
    }
    RequestStatus();
    return RET_OK;
}
int MainZonePwrBtnPressed()
{
    if(mMainZoneStatus.MainPwrStatus == true)
    {
        SendMainZonePwrToMaster(false);
    }
    else
    {
        SendMainZonePwrToMaster(true);
    }
    RequestStatus();
    return RET_OK;
}
int Zone2PwrBtnPressed()
{
    if(mZone2Status.MainPwrStatus == true)
    {
        SendZone2PwrToMaster(false);
    }
    else
    {
        SendZone2PwrToMaster(true);
    }
    RequestStatus();
    return RET_OK;
}

int SendLinkZ2VolumeToMaster(bool link)
{
    char data;
    if(link == true)
    {
        data = 1;
    }
    else
    {
        data = 0;
    }
    AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_SYNC_Z2_WITH_Z1_REQUEST,&data,1);
    return RET_OK;
}


int SendMainZonePwrToMaster(bool ON)
{
    char data;
    if(ON == true)
    {
        data = 1;
    }
    else
    {
        data = 0;
    }
    AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_MAIN_POWER_REQUEST,&data,1);
    return RET_OK;
}
int SendZone2PwrToMaster(bool ON)
{
        char data;
    if(ON == true)
    {
        data = 1;
    }
    else
    {
        data = 0;
    }
    AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_ZONE2_REQUEST,&data,1);
    return RET_OK;
}