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

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

CMasterCtrl::CMasterCtrl()
{
    qDebug("Creation...");

    mChaletLoraModuleInterface = 0;
    mChaletLoraNetworkCommInterface = 0;

//    mDeadBoltDevice = new CDeadboltDevice(1);
    mAVReceiverDevice = new CAVReceiverDevice();
    mVoipMsSMSClient = new CVoipMsSMSClient;
    mSystemTrayManager = new CSystemTrayManager;
    mSystemTrayManager->mProgramHandle = this;
    mSettingsWindow = new CSettingsWindow();
    mSettingsWindow->mProgramHandle = this;

    mSettingsManager.LoadSettings(&mMasterCtrlSettings);





    if(mMasterCtrlSettings.mChaletUseLoraIF == true)
    {
        mChaletLoraModuleInterface = new CLoraModuleInterface();  //JFM LoraInterface Dev
        mChaletLoraDevice = new CChaletDevice(1,mChaletLoraModuleInterface);  //JFM LoraInterface Dev
    }
    else
    {
        mChaletLoraNetworkCommInterface = new CLoraNetworkCommIF();//JFM LoraInterface Dev
        mChaletLoraDevice = new CChaletDevice(1,mChaletLoraNetworkCommInterface);//JFM LoraInterface Dev
    }

    CEthernetNetworkCommIF *Dummy = new CEthernetNetworkCommIF(NULL);   //No memory leak here.. object will be deleted on reasignment.
    mChaletLTEDevice = new CChaletDevice(1,(CAbstractNetworkCommIF*)Dummy);


  //  mChaletLoraInterface = new CChaletLoraInterface(mRooftopTowerLoraDevice);


    mIspindelDevice = new CIspindelDevice;


    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;
    if(mChaletLoraNetworkCommInterface != 0)
        delete mChaletLoraNetworkCommInterface; //JFM LoraInterface Dev
    if(mChaletLoraModuleInterface != 0)
        delete mChaletLoraModuleInterface;
    delete mChaletLoraDevice;
    delete mIspindelDevice;
   // 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;
    mNetworkDevicesManager->mIspindelDevice = mIspindelDevice;
    mNetworkDevicesManager->mLoraModuleInterfaceDevice = mChaletLoraModuleInterface;
    mNetworkDevicesManager->mChaletLTEDevice = mChaletLTEDevice;


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

    mVoipMsSMSClient->mProgramHandle = this;

    mSprinklerManager->mProgramHandle = this;

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



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

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

     mAVReceiverDevice->Start();

     //JFM LoraInterface Dev
     if(mMasterCtrlSettings.mChaletUseLoraIF)
     {
         mChaletLoraModuleInterface->SetLoraModuleInterfaceParameters(mMasterCtrlSettings.mChaletComPort,QSerialPort::Baud9600);
         mChaletLoraModuleInterface->SetLoraDestinationAddress(mMasterCtrlSettings.mChaletLoraAddress, 65 /*mMasterCtrlSettings.mChaletLoraChannel*/);
     }
     else
     {
         mChaletLoraNetworkCommInterface->mMyLoraAddress = 5;
        // mChaletLoraNetworkCommInterface->mMyLoraChannel = 4;
         mChaletLoraNetworkCommInterface->mMyLoraChannel = mMasterCtrlSettings.mChaletLoraChannel;
         mChaletLoraNetworkCommInterface->SetLoraDestinationAddress(mMasterCtrlSettings.mChaletLoraAddress,mMasterCtrlSettings.mChaletLoraChannel);
         mChaletLoraNetworkCommInterface->SetLoraSerialPortSettings(mMasterCtrlSettings.mChaletComPort,QSerialPort::Baud9600);
     }
     //JFM LoraInterface Dev



//     mChaletLoraNetworkCommInterface->SetLoraSerialPortSettings("COM5",QSerialPort::Baud9600);
     mChaletLoraDevice->mChaletName = "Chalet Lora";
     mChaletLoraDevice->Start();


     mChaletLTEDevice->mHomeServerInterface = new CChaletHomeServerInterface;
     mChaletLTEDevice->mChaletName = "Chalet LTE";
     mChaletLTEDevice->Start();





  //   mAppWidget.show();


}

unsigned int CMasterCtrl::QuitApplicationRequest()
{
   // QApplication::exit(69);
    QCoreApplication::quit();
    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();

    if(mChaletLoraModuleInterface != 0)
    {
        mChaletLoraModuleInterface->SetLoraDestinationAddress(mMasterCtrlSettings.mChaletLoraAddress, 65 /*mMasterCtrlSettings.mChaletLoraChannel*/);    //JFM LoraInterface Dev
        mChaletLoraModuleInterface->SetLoraModuleInterfaceParameters(mMasterCtrlSettings.mChaletComPort,QSerialPort::Baud9600);
    }
    if(mChaletLoraNetworkCommInterface != 0)
    {
        mChaletLoraNetworkCommInterface->mMyLoraChannel = mMasterCtrlSettings.mChaletLoraChannel;
        mChaletLoraNetworkCommInterface->SetLoraDestinationAddress(mMasterCtrlSettings.mChaletLoraAddress,mMasterCtrlSettings.mChaletLoraChannel);    //JFM LoraInterface Dev
        mChaletLoraNetworkCommInterface->SetLoraSerialPortSettings(mMasterCtrlSettings.mChaletComPort,QSerialPort::Baud9600);   //JFM LoraInterface Dev
    }
    return RET_OK;
}

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