//#include <proc/p32mx440f256h.h>

#include "ChaletPowerRelay.h"
#include "BoardCfg.h"
#include "timer.h"
#include "BatteryMonitor.h"

#define PIN_ACTIVE 1
#define PIN_INACTIVE 0

bool PowerRelayState;

void InitChaletPowerRelay()
{
    POWER_RELAY_ON_PIN = PIN_INACTIVE;
    POWER_RELAY_OFF_PIN = PIN_INACTIVE;
    PowerRelayState = CHALET_POWER_RELAY_UNKNOWN_STATE;
    TimerStop(CHALET_POWER_RELAY_COIL_TIMER);
    TimerStop(CHALET_POWER_RELAY_LVD_TIMER);
}

void ChaletPowerRelayTick()
{
    if(CHALET_12V_PRESENCE_PIN == CHALET_12V_POWER_STATE_ON)
    {
		if(PowerRelayState == CHALET_POWER_RELAY_OFF_STATE)
		{
			printf("Inverter turned ON\n");
		}
        PowerRelayState = CHALET_POWER_RELAY_ON_STATE;
    }
    else
    {
		if(PowerRelayState == CHALET_POWER_RELAY_ON_STATE)
		{
			printf("Inverter turned OFF\n");
		}
        PowerRelayState = CHALET_POWER_RELAY_OFF_STATE;
    }
    
    if((CHALET_POWER_RELAY_COIL_TIMER) == true)
    {
        if(IsTimerExpired(CHALET_POWER_RELAY_COIL_TIMER))
        {
            POWER_RELAY_ON_PIN = PIN_INACTIVE;
            POWER_RELAY_OFF_PIN = PIN_INACTIVE;
            TimerStop(CHALET_POWER_RELAY_COIL_TIMER);
        }
    }
    if(IsTimerRunning(CHALET_POWER_RELAY_LVD_TIMER) == true)
    {
        if(PowerRelayState != CHALET_POWER_RELAY_ON_STATE)
        {
            TimerStop(CHALET_POWER_RELAY_LVD_TIMER);
        }
        else if(IsTimerExpired(CHALET_POWER_RELAY_LVD_TIMER))
        {
            //Check if it was just a glitch
            if(GetBatteryVoltage(0) >= POWER_RELAY_LVD_VOLTAGE_TRESHOLD)
            {
                TimerStop(CHALET_POWER_RELAY_LVD_TIMER);
            }
            else
            {
                TimerStop(CHALET_POWER_RELAY_LVD_TIMER);
                ChaletPowerRelayTurnOff();
            }
        }
        
    }
    else
    {
        //LVD: if battery voltage is too low, turn off the power relay
        if(PowerRelayState == CHALET_POWER_RELAY_ON_STATE && GetBatteryVoltage(0) < POWER_RELAY_LVD_VOLTAGE_TRESHOLD && GetBatteryVoltage(0) != 0.0)
        {
            //ChaletPowerRelayTurnOff();
            TimerStart(CHALET_POWER_RELAY_LVD_TIMER,CHALET_POWER_RELAY_LVD_DEBOUNCE_TIMEOUT);
        }
    }
    
    if(IsTimerExpired(CHALET_POWER_RELAY_AUTOTURNOFF_TIMER))
    {
        ChaletPowerRelayTurnOff();
    }
}

void ChaletPowerRelayKickTimer()
{
    TimerStartSeconds(CHALET_POWER_RELAY_AUTOTURNOFF_TIMER,CHALET_OFFLINE_POWER_RELAY_RESET_TIMEOUT);
}

bool ChaletPowerRelayTurnOn()
{
    //LVD: if battery voltage is too low, turn off the power relay
    if(GetBatteryVoltage(0) < POWER_RELAY_LVD_VOLTAGE_TRESHOLD)
    {
        return false;
    }
    POWER_RELAY_ON_PIN = PIN_ACTIVE;
    TimerStart(CHALET_POWER_RELAY_COIL_TIMER,CHALET_POWER_RELAY_COIL_TIMEOUT);
 //   PowerRelayState = CHALET_POWER_RELAY_ON_STATE;
    return true;
}

bool ChaletPowerRelayTurnOff()
{
    POWER_RELAY_OFF_PIN = PIN_ACTIVE;
    TimerStart(CHALET_POWER_RELAY_COIL_TIMER,CHALET_POWER_RELAY_COIL_TIMEOUT);
    TimerStop(CHALET_POWER_RELAY_LVD_TIMER);
 //   PowerRelayState = CHALET_POWER_RELAY_OFF_STATE;
    return true;
}

char GetChaletPowerRelayState()
{
    return (char)PowerRelayState;
}