#include "NetworkDevicesMgr.h"
#include "232NetworkCommIF.h"
#include "485NetworkCommIF.h"
#include "EthernetNetworkCommIF.h"
#include "MasterCtrl.h"





CNetworkDevicesMgr::CNetworkDevicesMgr()
{
    mVoipMSInterfaceHandle = 0;
    mProgramHandle = 0;
    mSprinklerMgrHandle = 0;
    mAvReceiverDevice = 0;
    mChaletLoraDevice = 0;
    mIspindelDevice = 0;
    mLoraModuleInterfaceDevice = 0;
}

CNetworkDevicesMgr::~CNetworkDevicesMgr()
{
    for(int i = 0; i < mNetworkDevicesList.size(); i++)
    {
        delete mNetworkDevicesList.at(i);
    }
    mNetworkDevicesList.clear();
}

int CNetworkDevicesMgr::InitNetworkDevices()
{

    C232NetworkCommIF *NetworkInterface = new C232NetworkCommIF();
    CDeadboltDevice *DeadboltDevice = new CDeadboltDevice(1,(CAbstractNetworkCommIF*)NetworkInterface);
    mNetworkDevicesList.append(DeadboltDevice);

    return RET_OK;
}

int CNetworkDevicesMgr::CreateNewSMSDevice(int Address, CAbstractNetworkCommIF *NetworkIF)
{
    if(mVoipMSInterfaceHandle == 0)
    {
        return RET_ERROR;
    }


    CSMSDevice *SMSDevice = new CSMSDevice(Address,NetworkIF,mVoipMSInterfaceHandle);
    SMSDevice->mContactRepositoryHandle = mContactRepositoryHandle;

    mNetworkDevicesList.append((CNetworkDevice*)SMSDevice);


    return RET_OK;
}

int CNetworkDevicesMgr::CreateNewSprinklerDevice(int Address, CAbstractNetworkCommIF *NetworkIF)
{
    if(mSprinklerMgrHandle == 0)
    {
        return RET_ERROR;
    }

    CSprinklerDevice *SprinklerDevice = new CSprinklerDevice(Address,NetworkIF,mSprinklerMgrHandle);
    mNetworkDevicesList.append((CNetworkDevice*)SprinklerDevice);
    mSprinklerMgrHandle->NewSprinklerDeviceCreated(SprinklerDevice);


    return RET_OK;
}

int CNetworkDevicesMgr::CreateNewSprinklerInterface(int Address, CAbstractNetworkCommIF *NetworkIF)
{
    if(mSprinklerMgrHandle == 0)
    {
        return RET_ERROR;
    }

    CSprinklerInterface *SprinklerInterface = new CSprinklerInterface(Address,NetworkIF,mSprinklerMgrHandle);
    mNetworkDevicesList.append((CNetworkDevice*)SprinklerInterface);

    return RET_OK;
}

int CNetworkDevicesMgr::CreateNewAvReceiverInterface(int Address, CAbstractNetworkCommIF *NetworkIF)
{
    if(mAvReceiverDevice == 0)
    {
        return RET_ERROR;
    }

    CAvReceiverInterface *AvReceiverInterface = new CAvReceiverInterface(Address,NetworkIF,mAvReceiverDevice);
    mNetworkDevicesList.append((CNetworkDevice*)AvReceiverInterface);

    return RET_OK;
}

int CNetworkDevicesMgr::CreateNewChaletInterface(int Address, CAbstractNetworkCommIF *NetworkIF)
{
    if(mChaletLoraDevice == 0)
    {
        return RET_ERROR;
    }
//    CChaletInterface *ChaletInterface = new CChaletInterface(Address, NetworkIF,mChaletLoraDevice);
    CChaletInterface *ChaletInterface = new CChaletInterface(Address, NetworkIF,mChaletLTEDevice);
    mNetworkDevicesList.append((CNetworkDevice*)ChaletInterface);
    return RET_OK;
}

int CNetworkDevicesMgr::CreateNewIspindleInterface(int Address, CAbstractNetworkCommIF *NetworkIF)
{
    if(mIspindelDevice == 0)
    {
        return RET_ERROR;
    }
    CIspindelInterface *IspindelInterface = new CIspindelInterface(Address, NetworkIF, mIspindelDevice);
    mNetworkDevicesList.append((CNetworkDevice*)IspindelInterface);
    return RET_OK;
}

int CNetworkDevicesMgr::CreateNewLoraInterfaceInterface(int Address, CAbstractNetworkCommIF *NetworkIF)
 {
    if(mLoraModuleInterfaceDevice == 0)
    {
        return RET_ERROR;
    }

    CLoraModuleInterfaceInterface *LoraModuleIFInterface = new CLoraModuleInterfaceInterface(Address,NetworkIF,mLoraModuleInterfaceDevice);
    mNetworkDevicesList.append((CNetworkDevice*)LoraModuleIFInterface);
    return RET_OK;
}

int CNetworkDevicesMgr::BindNetworkChaletDevice(int Address, CAbstractNetworkCommIF *NetworkIF)
{
    mChaletLTEDevice->mDeviceAddress = Address;
    mChaletLTEDevice->SetNetworkInterface(NetworkIF);
    return RET_OK;
}

int CNetworkDevicesMgr::CreateNewAVReceiverConsoleInterface(int Address, CAbstractNetworkCommIF *NetworkIF)
{
    if(mAvReceiverDevice == 0)
    {
        return RET_ERROR;
    }

    CAvReceiverConsoleInterface *AvReceiverConsoleInterface = new CAvReceiverConsoleInterface(Address,NetworkIF,mAvReceiverDevice);
    mNetworkDevicesList.append((CNetworkDevice*)AvReceiverConsoleInterface);

    return RET_OK;
}

void CNetworkDevicesMgr::EthernetNetworkDeviceDisconnected(CNetworkDevice *Device)
{
    qDebug("Device disconnected.  ID: %d, Address: %d",Device->GetDeviceID(),Device->GetDeviceAddress());
    int index = FindDeviceByPtr(Device);
    if(index != -1)
    {
        delete mNetworkDevicesList.at(index);
        mNetworkDevicesList.removeAt(index);
    }
}

int CNetworkDevicesMgr::FindDeviceByPtr(CNetworkDevice *Device)
{
    for(int i = 0; i < mNetworkDevicesList.size(); i++)
    {
        if(mNetworkDevicesList.at(i) == Device)
        {
            return i;
        }
    }

    return -1;
}

CNetworkDevice* CNetworkDevicesMgr::GetDevice(int DeviceID, int Address)
{
    for(int i = 0; i < mNetworkDevicesList.size(); i++)
    {
        if(mNetworkDevicesList.at(i)->GetDeviceID() == DeviceID &&
           mNetworkDevicesList.at(i)->GetDeviceAddress() == Address)
        {
            return mNetworkDevicesList.at(i);
        }
    }

    return 0;
}

QList<CNetworkDevice*> CNetworkDevicesMgr::GetDevices(int DeviceID)
{
    QList<CNetworkDevice*> DevicesList;
    for(int i = 0; i < mNetworkDevicesList.size(); i++)
    {
        if(mNetworkDevicesList.at(i)->GetDeviceID() == DeviceID)
        {
            DevicesList.append(mNetworkDevicesList.at(i));
        }
    }

    return DevicesList;
}

int CNetworkDevicesMgr::NewSMSMessagesReceived(QList<CSMSMessage> NewMessages)
{
    for(int i = 0; i < mNetworkDevicesList.size(); i++)
    {
        if(mNetworkDevicesList.at(i)->GetDeviceID() == ID_SMS_CLIENT)
        {
            CSMSDevice *SMSDevice = (CSMSDevice*)mNetworkDevicesList.at(i);
            SMSDevice->SendNewSMSReceived(NewMessages);
        }
    }

    return RET_OK;
}
