#include "MasterCtrl.h"
#include <QApplication>

// #include <QByteArray>
//#include <QBuffer>

CMasterCtrl::CMasterCtrl()
{
    qDebug("Creation...");
//    mDeadBoltDevice = new CDeadboltDevice(1);
    mAVReceiverDevice = new CAVReceiverDevice();
    mVoipMsSMSClient = new CVoipMsSMSClient;
    mSystemTrayManager = new CSystemTrayManager;
    mSystemTrayManager->mProgramHandle = this;
    mSettingsWindow = new CSettingsWindow();
    mSettingsWindow->mProgramHandle = this;

    mChaletLoraNetworkCommInterface = new CLoraNetworkCommIF();
    mChaletLoraNetworkCommInterface->mMyLoraAddress = 5;
    mChaletLoraNetworkCommInterface->mMyLoraChannel = 4;
    mChaletLoraDevice = new CChaletLoraDevice(1,mChaletLoraNetworkCommInterface);
  //  mChaletLoraInterface = new CChaletLoraInterface(mRooftopTowerLoraDevice);


    mEthernetNetworkServer = new CEthernetNetworkServer;
    mNetworkDevicesManager = new CNetworkDevicesMgr;
    mContactsRepository = new CContactRepository;
    mSprinklerManager = new CSprinklerMgr;

   // mMasterCtrlSettings = new CSettings;
}

CMasterCtrl::~CMasterCtrl()
{
    delete mAVReceiverDevice;
    delete mVoipMsSMSClient;
    delete mSystemTrayManager;
    delete mSettingsWindow;
    delete mNetworkDevicesManager;
    delete mEthernetNetworkServer;
    delete mContactsRepository;
    delete mSprinklerManager;
    delete mChaletLoraNetworkCommInterface;
    delete mChaletLoraDevice;
   // delete mMasterCtrlSettings;
}

void CMasterCtrl::Start()
{
    qDebug("Started!");


//    QByteArray FrameData;
//    QBuffer FrameBuffer(&FrameData);
//    QDataStream *FrameDataStrm = new QDataStream(&FrameData,QIODevice::ReadWrite);

//    QString DID = "Salut";
//    *FrameDataStrm << DID;

//    FrameBuffer.seek(0);



    mContactsRepository->LoadContacts();

    mNetworkDevicesManager->mVoipMSInterfaceHandle = mVoipMsSMSClient;
    mNetworkDevicesManager->mContactRepositoryHandle = mContactsRepository;
    mNetworkDevicesManager->mProgramHandle = this;
    mNetworkDevicesManager->mSprinklerMgrHandle = mSprinklerManager;
    mNetworkDevicesManager->mAvReceiverDevice = mAVReceiverDevice;
    mNetworkDevicesManager->mChaletLoraDevice = mChaletLoraDevice;


    mEthernetNetworkServer->mDevicesMgrHandle = mNetworkDevicesManager;
    mEthernetNetworkServer->mProgramHandle = this;

    mVoipMsSMSClient->mProgramHandle = this;

    mSprinklerManager->mProgramHandle = this;

    connect(mVoipMsSMSClient,SIGNAL(DIDSFetched(QStringList)),mSettingsWindow,SLOT(DIDsListFetched(QStringList)));


     mSettingsManager.LoadSettings(&mMasterCtrlSettings);
     mSettingsWindow->SetSettingsData(&mMasterCtrlSettings);
     mVoipMsSMSClient->SetVOIPMsSettings(&mMasterCtrlSettings.mVoipMSSettings);
     mVoipMsSMSClient->DownloadSMSFromServer();

     mNetworkDevicesManager->InitNetworkDevices();
     mEthernetNetworkServer->StartServer(2182);

     mAVReceiverDevice->Start();

     mChaletLoraNetworkCommInterface->SetLoraSerialPortSettings("COM3",QSerialPort::Baud9600);
     mChaletLoraDevice->Start();





  //   mAppWidget.show();


}

unsigned int CMasterCtrl::QuitApplicationRequest()
{
    QApplication::exit(69);
    return RET_OK;
}

unsigned int CMasterCtrl::ShowSettingsWindowRequest()
{
    mSettingsWindow->UpdateContacts();
    mSettingsWindow->show();
    return RET_OK;
}

unsigned int CMasterCtrl::FetchDIDsRequest(QString Username, QString Password)
{
    mVoipMsSMSClient->GetDidFromUserAccount(Username,Password);
    return RET_OK;
}

unsigned int CMasterCtrl::SaveSettings(CSettings *Settings)
{
    CSettings toto;
    toto = *Settings;
    mMasterCtrlSettings = toto;
    mSettingsManager.SaveSettings(&mMasterCtrlSettings);

    return RET_OK;
}

unsigned int CMasterCtrl::SettingsWindowClosed()
{
    mVoipMsSMSClient->SetVOIPMsSettings(&mMasterCtrlSettings.mVoipMSSettings);
    mVoipMsSMSClient->DownloadSMSFromServer();
    return RET_OK;
}

int CMasterCtrl::NewSMSMessagesArrived(QList<CSMSMessage> NewMessages)
{
    mNetworkDevicesManager->NewSMSMessagesReceived(NewMessages);
    return RET_OK;
}
