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

CChalet::CChalet(CChaletGui *ChaletGuiPtr)
{
    mChaletGui = ChaletGuiPtr;
    mChaletGui->mProgramHandle = this;
    mNetworkInterface = new CChaletMasterCtrlInterface(this);


    mChaletPollTimer = new QTimer();
    mChaletPollTimer->setInterval(1000);
    mChaletPollTimer->setSingleShot(false);
    connect(mChaletPollTimer,SIGNAL(timeout()),this,SLOT(ChaletPollTimerExpired()));


}

CChalet::~CChalet()
{
    delete mNetworkInterface;
    delete mChaletPollTimer;
}

int CChalet::Start()
{
    mNetworkInterface->ConnectToMasterCtrl();
    mChaletPollTimer->start();

    return RET_OK;
}

void CChalet::ChaletPollTimerExpired()
{
    mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_GENERAL_STATUS_REQUEST,QByteArray());
}

int CChalet::ChaletStatusReceived(CChaletMainStatus Status)
{
    mChaletGui->UpdateChaletStatus(Status);

    return RET_OK;
}
int CChalet::ChaletLogReceived(QByteArray *Log)
{
    mChaletGui->UpdateChaletLogPlot(Log);
    return RET_OK;
}

int CChalet::ChaletCommActivity()
{
    return mChaletGui->ChaletCommActivity();
}

int CChalet::WiFiToggleButtonPressed(bool RequestedState)
{

    QByteArray WiFiState;
    WiFiState.clear();

    if(RequestedState)
    {
        WiFiState.append((char)0x01);
    }
    else
    {
        WiFiState.append((char)0x00);
    }
    return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_WIFI_SET_STATE_REQUEST,WiFiState);
}

int CChalet::InverterToggleButtonPressed(bool RequestedState)
{
    QByteArray InverterState;
    InverterState.clear();

    if(RequestedState)
    {
        InverterState.append((char)0x01);
    }
    else
    {
        InverterState.append((char)0x00);
    }
    return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_AC_POWER_SET_STATE_REQUEST,InverterState);
}

int CChalet::DoHarakiriButtonClicked(bool Verified)
{
    if(Verified)
    {
        return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_DO_HARAKIRI_REQUEST,QByteArray());
    }

    return RET_ERROR;
}

int CChalet::RebootCPUButtonPressed()
{
    return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_REBOOT_CPU_REQUEST,QByteArray());
}

int CChalet::ConnectedToMaster(bool connected)
{
    if(connected)
    {
        mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_GET_TODAYS_DATA_LOG_REQUEST,QByteArray());
    }
    return RET_OK;
}

int CChalet::RequestChaletLogs(QDate StartDate)
{
    if(StartDate > QDate::currentDate())
    {
        return false;
    }
    QByteArray StartDateData;
    QDataStream Strm(&StartDateData,QIODevice::WriteOnly);
    Strm.device()->seek(0);

    Strm << StartDate;

    return mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_GET_DATA_LOG_REQUEST,StartDateData);

}

int CChalet::RequestDeviceWifiParams()
{

    mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_WIFI_GET_DEVICE_PARAMS_REQUEST,QByteArray());

    return RET_OK;

}

int CChalet::RequestFirmwareVersion()
{
    mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_GET_FIRMWARE_VERSION_REQUEST,QByteArray());
    return RET_OK;
}

int CChalet::RequestClearCommStats()
{
    mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_CLEAR_COMMS_STATS_REQUEST,QByteArray());
    return RET_OK;
}

int CChalet::RequestWifiStatus()
{
    mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_GET_WIFI_STATUS_REQUEST,QByteArray());
    return RET_OK;
}

int CChalet::WifiStatusReceived(QByteArray Data)
{
    quint32 Add = 0;
    char byte;
    byte = Data.at(1);
    Add += (quint32)(byte & 0x000000FF);
    Add <<= 8;
    byte = Data.at(2);
    Add += (quint32)(byte & 0x000000FF);
    Add <<= 8;
    byte = Data.at(3);
    Add += (quint32)(byte & 0x000000FF);
    Add <<= 8;
    byte = Data.at(4);
    Add += (quint32)(byte & 0x000000FF);
    QHostAddress IP = QHostAddress(Add);

    mChaletGui->UpdateDeviceWifiStatus(Data[0],IP);
    return RET_OK;
}

int CChalet::DeviceWiFiParamsReceived(QByteArray *Data)
{
    quint32 Add = 0;
    char byte;
    byte = Data->at(0);
    Add += (quint32)(byte & 0x000000FF);
    Add <<= 8;
    byte = Data->at(1);
    Add += (quint32)(byte & 0x000000FF);
    Add <<= 8;
    byte = Data->at(2);
    Add += (quint32)(byte & 0x000000FF);
    Add <<= 8;
    byte = Data->at(3);
    Add += (quint32)(byte & 0x000000FF);
    QHostAddress IP = QHostAddress(Add);

    Add = 0;
    byte = Data->at(4);
    Add += (quint32)(byte & 0x000000FF);
    Add <<= 8;
    byte = Data->at(5);
    Add += (quint32)(byte & 0x000000FF);
    Add <<= 8;
    byte = Data->at(6);
    Add += (quint32)(byte & 0x000000FF);
    Add <<= 8;
    byte = Data->at(7);
    Add += (quint32)(byte & 0x000000FF);
    QHostAddress Gateway = QHostAddress(Add);

    QString APName, APPassword;
    quint8 APNameLenght, APPasswordLenght;
    bool UseDHCP = false;

    if(Data->size() > 9)
    {
    if(Data->at(8) == 1)
        UseDHCP = true;

        APNameLenght = Data->at(9);
        APPasswordLenght = Data->at(10);
        APName = QString(Data->mid(11,APNameLenght));
        APPassword = QString(Data->mid(11+APNameLenght,APPasswordLenght));
    }

    mChaletGui->UpdateDeviceWiFiParameters(IP, Gateway,APName,APPassword,UseDHCP);
    return RET_OK;
}

int CChalet::DeviceFirmwareVersionReceived(QByteArray Data)
{
    mChaletGui->UpdateFirmwareVersion(Data);
}

int CChalet::SetDeviceWifiParams(QString IP, QString Gateway, bool UseDHCP, QString APName, QString APPwd)
{
    QHostAddress DeviceIP;
    QHostAddress DeviceGateway;
    quint8 APNameLength, APPwdLength;

    if(APName.length() > 64)
        return RET_ERROR;
    if(APPwd.length() > 64)
        return RET_ERROR;


    if(DeviceIP.setAddress(IP) == false)
    {
        return RET_ERROR;
    }
    if(DeviceGateway.setAddress(Gateway) == false)
    {
        return RET_ERROR;
    }



    QByteArray Buffer;
    Buffer.resize(8);

    quint32 Address = DeviceIP.toIPv4Address();
    char byte = (char)(Address & 0x000000FF);
    Buffer[3] = byte;
    Address >>= 8;
    byte = (char)(Address & 0x000000FF);
    Buffer[2] = byte;
    Address >>= 8;
    byte = (char)(Address & 0x000000FF);
    Buffer[1] = byte;
    Address >>= 8;
    byte = (char)(Address & 0x000000FF);
    Buffer[0] = byte;
    Address >>= 8;

    Address = DeviceGateway.toIPv4Address();
    byte = (char)(Address & 0x000000FF);
    Buffer[7] = byte;
    Address >>= 8;
    byte = (char)(Address & 0x000000FF);
    Buffer[6] = byte;
    Address >>= 8;
    byte = (char)(Address & 0x000000FF);
    Buffer[5] = byte;
    Address >>= 8;
    byte = (char)(Address & 0x000000FF);
    Buffer[4] = byte;
    Address >>= 8;

    if(UseDHCP)
    {
        Buffer[8] = 1;
    }
    else
    {
        Buffer[8] = 0;
    }

    Buffer.append(APName.length());
    Buffer.append(APPwd.length());

    Buffer.append(APName);
    Buffer.append(APPwd);


    mNetworkInterface->SendMasterCtrlCommand(CHALET_INTERFACE_WIFI_SET_DEVICE_PARAMS_REQUEST,Buffer);

    return RET_OK;

}
