#include "ChaletInterface.h"
#include "ChaletDataLogger.h"

CChaletInterface::CChaletInterface(int Address, CAbstractNetworkCommIF *NetworkInterface, CChaletDevice *DevicePtr):
    CNetworkDevice(ID_CHALET_INTERFACE,Address,NetworkInterface)
{
    mChaletLoraDevice = DevicePtr;
    mChaletDataLogger = mChaletLoraDevice->GetDataLoggerHandle();

    connect(mChaletLoraDevice,SIGNAL(DeviceWifiStoredParamsReceived()),this,SLOT(DeviceWiFiDataReceived()));
    connect(mChaletLoraDevice,SIGNAL(DeviceFirmwareVersionReceived()),this,SLOT(DeviceFirmwareVersionReceived()));
    connect(mChaletLoraDevice,SIGNAL(DeviceWifiStatusReceived(QByteArray)),this,SLOT(DeviceWifiStatusReceived(QByteArray)));

}


int CChaletInterface::NewDeviceFrameReceived(int DeviceID, int DeviceAddress, int MessageID, int DataSize, QByteArray Data)
{

    Q_UNUSED(DeviceID)
    Q_UNUSED(DeviceAddress)
    Q_UNUSED(DataSize)
    Q_UNUSED(Data)

    switch(MessageID)
    {
        case CHALET_INTERFACE_ACK:
        {
            break;
        }
        case CHALET_INTERFACE_GENERAL_STATUS_REQUEST:
        {
            QByteArray data = mChaletLoraDevice->GetChaletMainStatus().ToByteArray();
            mNetworkInterfacePtr->SendNetworkMessage(ID_CHALET_INTERFACE,mDeviceAddress,CHALET_INTERFACE_GENERAL_STATUS_RESPONSE,data.size(),&data);

            break;
        }
        case CHALET_INTERFACE_AC_POWER_STATE_STATUS_REQUEST:
        {

            break;
        }
        case CHALET_INTERFACE_AC_POWER_SET_STATE_REQUEST:
        {
            qDebug("AC power state set request received from interface");
            if(Data[0] == (char)1)
            {
               mChaletLoraDevice->SendInverterPowerRelayState(true);
            }
            else if(Data[0] == (char)0)
            {
               mChaletLoraDevice->SendInverterPowerRelayState(false);
            }

            break;
        }
        case CHALET_INTERFACE_BATTERY_VOLTAGE_REQUEST:
        {
            break;
        }
        case CHALET_INTERFACE_WIFI_SET_STATE_REQUEST:
        {
            qDebug("WiFi state set request received from interface");
            if(Data[0] == (char)1)
            {
               mChaletLoraDevice->SendWiFiModuleSetState(true);
            }
            else if(Data[0] == (char)0)
            {
               mChaletLoraDevice->SendWiFiModuleSetState(false);
            }
            break;
        }
        case CHALET_INTERFACE_DO_HARAKIRI_REQUEST:
        {
            mChaletLoraDevice->SendDOHarakiri();
            break;
        }
        case CHALET_INTERFACE_REBOOT_CPU_REQUEST:
        {
            mChaletLoraDevice->SendRebootCmd();
            break;
        }
        case CHALET_INTERFACE_GET_TODAYS_DATA_LOG_REQUEST:
        {
            QByteArray *data = mChaletDataLogger->GetTodaysData();
            mNetworkInterfacePtr->SendNetworkMessage(ID_CHALET_INTERFACE,mDeviceAddress,CHALET_INTERFACE_GET_TODAYS_DATA_LOG_RESPONSE,data->size(),data);
            delete data;
            break;
        }
        case CHALET_INTERFACE_GET_DATA_LOG_REQUEST:
        {
            QDate StartDate;
            QDataStream Strm(Data);
            Strm >> StartDate;
            QByteArray *data = mChaletDataLogger->GetChaletData(StartDate);
            mNetworkInterfacePtr->SendNetworkMessage(ID_CHALET_INTERFACE,mDeviceAddress,CHALET_INTERFACE_GET_DATA_LOG_RESPONSE,data->size(),data);
            delete data;
            break;
        }
        case CHALET_INTERFACE_WIFI_GET_DEVICE_PARAMS_REQUEST:
        {
            mChaletLoraDevice->SendGetWifiParams();
            break;
        }
        case CHALET_INTERFACE_WIFI_SET_DEVICE_PARAMS_REQUEST:
        {
            mChaletLoraDevice->SendSetWifiParams(Data);
            break;
        }
        case CHALET_INTERFACE_GET_FIRMWARE_VERSION_REQUEST:
        {
            mChaletLoraDevice->SendGetFirmwareVersion();
            break;
        }
        case CHALET_INTERFACE_CLEAR_COMMS_STATS_REQUEST:
        {

            mChaletLoraDevice->SendClearCommStatsRequest();
//            mChaletLoraDevice->ResetCommStats();
            break;
        }
        case CHALET_INTERFACE_GET_WIFI_STATUS_REQUEST:
        {
            mChaletLoraDevice->SendGetWifiStatusRequest();
            break;
        }
        case CHALET_INTERFACE_WIFI_GET_DEVICE_PARAMS_RESPONSE:
        case CHALET_INTERFACE_WIFI_SET_DEVICE_PARAMS_RESPONSE:
        case CHALET_INTERFACE_GET_DATA_LOG_RESPONSE:
        case CHALET_INTERFACE_GET_TODAYS_DATA_LOG_RESPONSE:
        case CHALET_INTERFACE_GENERAL_STATUS_RESPONSE:
        case CHALET_INTERFACE_AC_POWER_STATE_STATUS_RESPONSE:
        case CHALET_INTERFACE_AC_POWER_SET_STATE_RESPONSE:
        case CHALET_INTERFACE_BATTERY_VOLTAGE_RESPONSE:
        case CHALET_INTERFACE_WIFI_SET_STATE_RESPONSE:
        case CHALET_INTERFACE_CLEAR_COMMS_STATS_RESPONSE:
        case CHALET_INTERFACE_GET_WIFI_STATUS_RESPONSE:
        {
            break;

        }






    }

    return RET_OK;
}

void CChaletInterface::SendChaletCommActivityBeacon()
{
     mNetworkInterfacePtr->SendNetworkMessage(ID_CHALET_INTERFACE,mDeviceAddress,CHALET_INTERFACE_CHALET_ACTIVITY_RESPONSE,0,0);
}

void CChaletInterface::DeviceWiFiDataReceived()
{
    QByteArray Data = mChaletLoraDevice->GetDeviceWiFiStoredParams();
    mNetworkInterfacePtr->SendNetworkMessage(ID_CHALET_INTERFACE,mDeviceAddress,CHALET_INTERFACE_WIFI_GET_DEVICE_PARAMS_RESPONSE,Data.size(),&Data);
}

void CChaletInterface::DeviceFirmwareVersionReceived()
{
    QByteArray Data = mChaletLoraDevice->GetDeviceFirmwareVersion();
    mNetworkInterfacePtr->SendNetworkMessage(ID_CHALET_INTERFACE,mDeviceAddress,CHALET_INTERFACE_GET_FIRMWARE_VERSION_RESPONSE,Data.size(),&Data);
}

void CChaletInterface::DeviceWifiStatusReceived(QByteArray Data)
{
    mNetworkInterfacePtr->SendNetworkMessage(ID_CHALET_INTERFACE,mDeviceAddress,CHALET_INTERFACE_GET_WIFI_STATUS_RESPONSE,Data.size(),&Data);
}
