#include "ChaletMasterCtrlInterface.h"
#include "ChaletData.h"
#include "CChalet.h"

CChaletMasterCtrlInterface::CChaletMasterCtrlInterface(CChalet *ProgramHandle)
{
    mMyDeviceID = ID_CHALET_INTERFACE;
    mNetworkPort = 2182;
    mMasterCtrlIPAddress = "127.0.0.1";
    mNetworkCommSocket = 0;
    mDeviceAddress = 1;

    mProgramHandle = ProgramHandle;
}

int CChaletMasterCtrlInterface::DeviceConnectedToMaster(bool Connected)
{

    if(Connected)
    {
        qDebug("Chalet Interface connected to Master.");
        mProgramHandle->ConnectedToMaster(Connected);

    }
    else
        return RET_ERROR;

    return RET_OK;
}

int CChaletMasterCtrlInterface::DeviceFrameReceived(int TargetDeviceID, int TargetDeviceAddress, int SenderID, int SenderAddress, int MessageID, int DataSize, QByteArray Data)
{
    Q_UNUSED(DataSize)
    Q_UNUSED(SenderID)
    Q_UNUSED(SenderAddress)


    if(TargetDeviceID == mMyDeviceID && (TargetDeviceAddress == BROADCAST_VALUE || TargetDeviceAddress == mDeviceAddress))
    {
        switch(MessageID)
        {
            case CHALET_INTERFACE_ACK:
            {
                qDebug("Chalet Interface ACK received :)");
                break;
            }

            case CHALET_INTERFACE_GENERAL_STATUS_RESPONSE:
            {
 //               qDebug("Chalet General Status received :)");
                CChaletMainStatus Status;
                Status.FromByteArray(Data);
                mProgramHandle->ChaletStatusReceived(Status);
                break;
            }

            case CHALET_INTERFACE_AC_POWER_STATE_STATUS_RESPONSE:
            {
                break;
            }

            case CHALET_INTERFACE_AC_POWER_SET_STATE_RESPONSE:
            {
                break;
            }

            case CHALET_INTERFACE_BATTERY_VOLTAGE_RESPONSE:
            {
                break;
            }

            case CHALET_INTERFACE_GET_TODAYS_DATA_LOG_RESPONSE:
            {
                mProgramHandle->ChaletLogReceived(&Data);
                break;
            }
            case CHALET_INTERFACE_CHALET_ACTIVITY_RESPONSE:
            {
                mProgramHandle->ChaletCommActivity();
                break;
            }
            case CHALET_INTERFACE_GET_DATA_LOG_RESPONSE:
            {
                mProgramHandle->ChaletLogReceived(&Data);
                break;
            }

            case CHALET_INTERFACE_WIFI_GET_DEVICE_PARAMS_RESPONSE:
            {
                mProgramHandle->DeviceWiFiParamsReceived(&Data);
                break;
            }
            case CHALET_INTERFACE_WIFI_SET_DEVICE_PARAMS_RESPONSE:
            {
                break;
            }
            case CHALET_INTERFACE_GET_FIRMWARE_VERSION_RESPONSE:
            {
                mProgramHandle->DeviceFirmwareVersionReceived(Data);
                break;
            }
            case CHALET_INTERFACE_GET_WIFI_STATUS_RESPONSE:
            {
                mProgramHandle->WifiStatusReceived(Data);
                break;
            }
            case CHALET_INTERFACE_GENERAL_STATUS_REQUEST:
            case CHALET_INTERFACE_AC_POWER_STATE_STATUS_REQUEST:
            case CHALET_INTERFACE_AC_POWER_SET_STATE_REQUEST:
            case CHALET_INTERFACE_BATTERY_VOLTAGE_REQUEST:
            case CHALET_INTERFACE_GET_TODAYS_DATA_LOG_REQUEST:
            case CHALET_INTERFACE_WIFI_GET_DEVICE_PARAMS_REQUEST:
            case CHALET_INTERFACE_WIFI_SET_DEVICE_PARAMS_REQUEST:
            default:
            {
                qDebug("Chalet: Invalid Ethernet Msg received from MasterCtrl: %d",MessageID);
                break;
            }

        }
    }
    return RET_OK;
}
