#include "AudioConsoleLedMgr.h"
#include "timer.h"
//BtnLedDimmer_t mLinkZ2LEDDimmer;
//int DimmerSlope = 5;

BtnLedDimmer_t mLedDimmerList[AUDIO_CONSOLE_NB_LED];
BtnLedDimmer_t mLinkZ2LEDDimmer;
int DimmerSlope = 5;

void AudioConsoleLedMgrInit()
{
    mLinkZ2LEDDimmer.IsON = false;
    mLinkZ2LEDDimmer.TotalPeriod = 200;
    mLinkZ2LEDDimmer.ONPeriod = 200;
    mLinkZ2LEDDimmer.Counter = 0;
    mLinkZ2LEDDimmer.BreathEffect = true;
    
    int i;
    for(i = 0; i < AUDIO_CONSOLE_NB_LED; i++)
    {
        mLedDimmerList[i].IsON = false;
        mLedDimmerList[i].TotalPeriod = 200;
        mLedDimmerList[i].ONPeriod = 200;
        mLedDimmerList[i].Counter = 0;
        mLedDimmerList[i].BreathEffect = false;
        mLedDimmerList[i].DimmerSlope = 5;
    }
    
    TimerStart(LED_EFFECT_TIMER,LED_EFFECT_TIMEOUT);
}

void AudioConsoleLedMgrTick()
{
    if(IsTimerExpired(LED_EFFECT_TIMER) == true)  //Reuse pushbutton timer :)
    {
        int i;
        for(i = 0; i < AUDIO_CONSOLE_NB_LED; i++)
        {
            if(mLedDimmerList[i].BreathEffect == true && mLedDimmerList[i].IsON == true)
            {
                mLedDimmerList[i].ONPeriod += mLedDimmerList[i].DimmerSlope;
                if(mLedDimmerList[i].ONPeriod >= mLedDimmerList[i].TotalPeriod)
                {
                    mLedDimmerList[i].DimmerSlope = -5;
                    mLedDimmerList[i].ONPeriod = mLedDimmerList[i].TotalPeriod - 5;
                }
                else if(mLedDimmerList[i].ONPeriod <= 5)
                {
                    mLedDimmerList[i].DimmerSlope = 5;
                }
            }
        }
        TimerStart(LED_EFFECT_TIMER,LED_EFFECT_TIMEOUT);
    }
    
    int i;
    for(i = 0; i < AUDIO_CONSOLE_NB_LED; i++)
    {
        if(mLedDimmerList[i].IsON)
        {
            if(mLedDimmerList[i].Counter++ >= mLedDimmerList[i].ONPeriod)
            {
                TurnOFF(i);
                if(mLedDimmerList[i].Counter >= mLedDimmerList[i].TotalPeriod)
                {
                    TurnON(i);
                    mLedDimmerList[i].Counter = 0;
                }
            }
        }
    }
    
    
}

int UpdatePushButtonLEDs(AVReceiverStatus_t *MainZoneStatus, AVReceiverStatus_t* Z2Status)
{
    if(MainZoneStatus->MainPwrStatus == true)
    {
        LedMgrSetLED(AUDIO_CONSOLE_MAIN_PWR_LED,true,100,false);
    }
    else
    {
        LedMgrSetLED(AUDIO_CONSOLE_MAIN_PWR_LED,false,100,false);
    }
    
    if(Z2Status->MainPwrStatus == true)
    {
        LedMgrSetLED(AUDIO_CONSOLE_Z2_PWR_LED,true,100,false);
    }
    else
    {
        LedMgrSetLED(AUDIO_CONSOLE_Z2_PWR_LED,false,100,false);
    }
    
    if(MainZoneStatus->SyncZones == true)
    {
        LedMgrSetLED(AUDIO_CONSOLE_SYNC_Z2_LED,true,100,false);
    }
    else
    {
        LedMgrSetLED(AUDIO_CONSOLE_SYNC_Z2_LED,false,100,false);
    }
    
    if(MainZoneStatus->IsMute == true)
    {
        LedMgrSetLED(AUDIO_CONSOLE_MAIN_MUTE_LED,true,100,true);
    }
    else
    {
        LedMgrSetLED(AUDIO_CONSOLE_MAIN_MUTE_LED,false,100,false);
    }
     if(Z2Status->IsMute == true)
    {
        LedMgrSetLED(AUDIO_CONSOLE_Z2_MUTE_LED,true,100,true);
    }
    else
    {
        LedMgrSetLED(AUDIO_CONSOLE_Z2_MUTE_LED,false,100,false);
    }
    
    return RET_OK;
}

int LedMgrSetLED(int LedID, bool ON, int Percent, bool breathe)
{
    if(LedID >= AUDIO_CONSOLE_NB_LED)
        return RET_ERROR;
    if(Percent > 100)
        return RET_ERROR;
    
    if(ON == true)
    {
        TurnON(LedID);
    }
    else
    {
        TurnOFF(LedID);
    }
    
    
    if(ON != mLedDimmerList[LedID].IsON || breathe == false)
    {
        mLedDimmerList[LedID].ONPeriod = (((float)Percent/100)*mLedDimmerList[LedID].TotalPeriod);
        mLedDimmerList[LedID].Counter = 0;
    }
    mLedDimmerList[LedID].IsON = ON;
    mLedDimmerList[LedID].BreathEffect = breathe;
    
    
    
    
    return RET_OK;
}

int TurnON(int LedID)
{
    switch(LedID)
    {
        case AUDIO_CONSOLE_SYNC_Z2_LED:
        {
            AUDIO_CONSOLE_LINK_Z2_STATUS_LED = LED_ON;
            break;
        }
        case AUDIO_CONSOLE_MAIN_PWR_LED:
        {
            AUDIO_CONSOLE_MAIN_PWR_STATUS_LED = LED_ON;
            break;
        }
        case AUDIO_CONSOLE_Z2_PWR_LED:
        {
            AUDIO_CONSOLE_ZONE2_PWR_STATUS_LED = LED_ON;
            break;
        }
        case AUDIO_CONSOLE_MAIN_MUTE_LED:
        {
            AUDIO_CONSOLE_MAIN_MUTE_STATUS_LED = LED_ON;
            break;
        }
        case AUDIO_CONSOLE_Z2_MUTE_LED:
        {
            AUDIO_CONSOLE_Z2_MUTE_STATUS_LED = LED_ON;
            break;
        }
        case AUDIO_CONSOLE_PUSHBTN_4_LED:
        {
            PUSH_BUTTON_LED4_PIN = LED_ON;
            break;
        }
        case AUDIO_CONSOLE_PUSHBTN_5_LED:
        {
            PUSH_BUTTON_LED5_PIN = LED_ON;
            break;
        }
        case AUDIO_CONSOLE_PUSHBTN_6_LED:
        {
            PUSH_BUTTON_LED6_PIN = LED_ON;
            break;
        }
        case AUDIO_CONSOLE_PUSHBTN_7_LED:
        {
            PUSH_BUTTON_LED7_PIN = LED_ON;
            break;
        }
    }
    return RET_OK;
}
int TurnOFF(int LedID)
{
    switch(LedID)
    {
        case AUDIO_CONSOLE_SYNC_Z2_LED:
        {
            AUDIO_CONSOLE_LINK_Z2_STATUS_LED = LED_OFF;
            break;
        }
        case AUDIO_CONSOLE_MAIN_PWR_LED:
        {
            AUDIO_CONSOLE_MAIN_PWR_STATUS_LED = LED_OFF;
            break;
        }
        case AUDIO_CONSOLE_Z2_PWR_LED:
        {
            AUDIO_CONSOLE_ZONE2_PWR_STATUS_LED = LED_OFF;
            break;
        }
        case AUDIO_CONSOLE_MAIN_MUTE_LED:
        {
            AUDIO_CONSOLE_MAIN_MUTE_STATUS_LED = LED_OFF;
            break;
        }
        case AUDIO_CONSOLE_Z2_MUTE_LED:
        {
            AUDIO_CONSOLE_Z2_MUTE_STATUS_LED = LED_OFF;
            break;
        }
        case AUDIO_CONSOLE_PUSHBTN_4_LED:
        {
            PUSH_BUTTON_LED4_PIN = LED_OFF;
            break;
        }
        case AUDIO_CONSOLE_PUSHBTN_5_LED:
        {
            PUSH_BUTTON_LED5_PIN = LED_OFF;
            break;
        }
        case AUDIO_CONSOLE_PUSHBTN_6_LED:
        {
            PUSH_BUTTON_LED6_PIN = LED_OFF;
            break;
        }
        case AUDIO_CONSOLE_PUSHBTN_7_LED:
        {
            PUSH_BUTTON_LED7_PIN = LED_OFF;
            break;
        }
    }
    return RET_OK;
}

int LedMgrShowOfflineMode(bool Offline)
{
    int i = 0;

    for(i = 0; i < AUDIO_CONSOLE_NB_LED; i++)
    {
        LedMgrSetLED(i,Offline,100,true);
    }
    
}