#include "define.h"
#include "AudioConsole.h"
#include "timer.h"
#include "BoardCfg.h"
#include "ProtocolDefs.h"
#include "NetworkProtocol.h"

AVReceiverStatus_t mMAinZoneStatus, mZone2Status;
bool mMasterInterfaceConnected;

int mAudioConsoleState;

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);
    
}

int AudioConsoleSetReceiverStatus(AVReceiverStatus_t *MainZone, AVReceiverStatus_t *Zone2)
{
    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;
    
    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:
                {
                    break;
                }
                case AUDIO_CONSOLE_SM_CONNECTED_EVENT:
                {
                    break;
                }
                case AUDIO_CONSOLE_SM_DISCONNECTED_EVENT:
                {
                    break;
                }
            }
            break;
        }
        case AUDIO_CONSOLE_CONNECTING_STATE:
        {
            switch(Event)
            {
                case AUDIO_CONSOLE_SM_TICK_EVENT:
                {
                    if(IsAudioInterfaceConnected() == false)
                    {
                        //TODO reset console display
                        mAudioConsoleState = AUDIO_CONSOLE_INIT_STATE;
                        TimerStop(CONSOLE_IF_CONNECT_TIMER);
                        
                    }
                    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:
                {
                    //Connection timed out.... trying again.
                    ConnectAudioInterface();
                    break;
                }
            }
            break;
        }
        case AUDIO_CONSOLE_RUN_STATE:
        {
            switch(Event)
            {
                case AUDIO_CONSOLE_SM_TICK_EVENT:
                {
                    if(IsAudioInterfaceConnected() == false)
                    {
                        //TODO reset console display
                        mAudioConsoleState = AUDIO_CONSOLE_INIT_STATE;
                        TimerStop(CONSOLE_IF_CONNECT_TIMER);
                        
                    }
                    if(IsTimerExpired(CONSOLE_IF_CONNECT_TIMER))
                    {
                        RequestStatus();
                        TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_STATUS_REQUEST_TIMEOUT);
                    }
                    break;
                }
                case AUDIO_CONSOLE_SM_CONNECTED_EVENT: 
                {
                    //Should not happen....
                    break;
                }
                case AUDIO_CONSOLE_SM_DISCONNECTED_EVENT:
                {
                    mAudioConsoleState = AUDIO_CONSOLE_INIT_STATE;
                    TimerStop(CONSOLE_IF_CONNECT_TIMER);
                    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);
    }
}

void RequestStatus()
{
    AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_GENERAL_STATUS_REQUEST,0,0);
}