#include "ProgramSettings.h"

CProgramSettings::CProgramSettings()
{

}


unsigned int CProgramSettings::LoadSettings(CSettings* Settings)
{
    QFile* SettingsFile = new QFile("./Configuration/Settings.mcs");
    if(SettingsFile)
    {
        if(SettingsFile->open(QIODevice::ReadOnly | QIODevice::Unbuffered) == false)
        {
            LoadDefaultSettings(Settings);
            SaveSettings(Settings);
            return RET_ERROR;
        }
    }
    else
    {
        LoadDefaultSettings(Settings);
        return RET_ERROR;
    }

    QDataStream * InputStream = new QDataStream(SettingsFile);

    quint32 MagicNbr;// = 0xBAADCAFE;

    *InputStream >> MagicNbr;

    if(MagicNbr != 0xBAADCAFE)
    {
        LoadDefaultSettings(Settings);
        return RET_ERROR;
    }

    *InputStream >> *Settings;

    SettingsFile->close();
    delete SettingsFile;
    delete InputStream;

    return RET_OK;
}

void CProgramSettings::LoadDefaultSettings(CSettings *Settings)
{
    Settings->mVoipMSSettings.mDefaultDID = "555555555";
    Settings->mVoipMSSettings.mPassword = "Voip.ms Password";
    Settings->mVoipMSSettings.mUsername = "Voip.ms Username";
    Settings->mVoipMSSettings.mStartDate = QDate::currentDate();
    Settings->mChaletComPort = "COM3";
    Settings->mChaletLoraAddress = 6;
    Settings->mChaletLoraChannel = 4;
    Settings->mChaletUseLoraIF = true;
}

unsigned int CProgramSettings::SaveSettings(CSettings *SettingsData)
{
    QFile* SettingsFile = new QFile("./Configuration/Settings.mcs");
    if(SettingsFile)
    {
        if(SettingsFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered) == false)
        {
            return RET_ERROR;
        }
    }
    else
    {
       return RET_ERROR;
    }

    QDataStream * OutputStream = new QDataStream(SettingsFile);

    quint32 MagicNbr = 0xBAADCAFE;

    *OutputStream << MagicNbr;

    *OutputStream << *SettingsData;

    SettingsFile->flush();
    SettingsFile->close();

    delete SettingsFile;
    delete OutputStream;

    return RET_OK;
}


QDataStream &operator<<(QDataStream &out, const CSettings &source)
{
    out << source.mVoipMSSettings;
    out << source.mChaletComPort;
    out << source.mChaletLoraAddress;
    out << source.mChaletLoraChannel;
    out << source.mChaletUseLoraIF;

    return out;
}

QDataStream &operator>>(QDataStream &in, CSettings &dest)
{
    in >> dest.mVoipMSSettings;
    in >> dest.mChaletComPort;
    in >> dest.mChaletLoraAddress;
    in >> dest.mChaletLoraChannel;
    in >> dest.mChaletUseLoraIF;

    return in;
}

QDataStream &operator<<(QDataStream &out, const CVoipMSSMSSettings &source)
{
    out << source.mDefaultDID
        << source.mPassword
        << source.mStartDate
        << source.mUsername;

    return out;
}

QDataStream &operator>>(QDataStream &in, CVoipMSSMSSettings &dest)
{
    in >> dest.mDefaultDID
       >> dest.mPassword
       >> dest.mStartDate
       >> dest.mUsername;

    return in;
}

CVoipMSSMSSettings& CVoipMSSMSSettings::operator=(const CVoipMSSMSSettings *source)
{
    if(source == this)
    {
        return *this;
    }

    this->mDefaultDID = source->mDefaultDID;
    this->mPassword = source->mPassword;
    this->mStartDate = source->mStartDate;
    this->mUsername = source->mUsername;

    return *this;
}

CSettings& CSettings::operator =(const CSettings &source)
{
    if(this == &source)
    {
        return *this;
    }
    this->mVoipMSSettings = source.mVoipMSSettings;
    this->mChaletComPort = source.mChaletComPort;
    this->mChaletLoraAddress = source.mChaletLoraAddress;
    this->mChaletLoraChannel = source.mChaletLoraChannel;
    this->mChaletUseLoraIF = source.mChaletUseLoraIF;
    return *this;
}
