#ifndef CVOIPMSSMSCLIENT_H
#define CVOIPMSSMSCLIENT_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "GlobalDefine.h"
#include <QList>
#include "SMSMessage.h"
#include <QStringList>
#include <QJsonArray>
#include "ProgramSettings.h"
#include <QTcpServer>
#include <QTimer>
#include "NetworkDevice.h"

#define VOIP_MS_API_URL "https://www.voip.ms/api/v1/rest.php?"
#define VOIP_MS_SMS_DATESPAN 90
#define VOIP_MS_SMS_MAX_COUNT 800
#define VOIP_MS_SMS_CHECK_TIMEOUT 5000 //Check for new SMS every 5 seconds

enum VoipMSRetValues
{
    VOIPMS_RET_SUCCESS,
    VOIPMS_RET_FAILED,
    VOIPMS_RET_NO_DID_FOUND
};

enum eVoipMSSMSDownloadStates
{
    VOIPMS_SMS_DOWNLOAD_INIT_STATE,
    VOIPMS_SMS_DOWNLOAD_SPAN_MSGS_STATE,
    VOIPMS_SMS_DOWNLOAD_LAST_BLOCK_MSGS_STATE,
    VOIPMS_SMS_GET_NEW_MSGS_STATE
};

class CMasterCtrl;

class CVoipMsSMSClient : public QObject
{
    Q_OBJECT
public:
    CVoipMsSMSClient();
    virtual ~CVoipMsSMSClient();

    int DownloadSMSFromServer();
    int SendSMSDownloadRequest(QDate StartDate, QDate EndDate);
    int DownloadNewSMS();
    int CheckForNewSMS();

    QList<CSMSMessage> mSMSMessagesList;

    CMasterCtrl *mProgramHandle;

    unsigned int GetDidFromUserAccount(const QString username, const QString Password);
    unsigned int SetVOIPMsSettings(CVoipMSSMSSettings *SettingsData);
    unsigned int NewSMSNotification(QString DID);



    QList<CSMSMessage> *GetSMSList();
    int GetSMSCount();
    QString GetDefaultDID();

    int SendSMS(QList<CSMSMessage> *SMSList);
    int SendSMS(CSMSMessage Message);


private:
    QNetworkAccessManager *mVOIPMSSocket;

    unsigned int SMSReceived(QJsonArray *sms);
    unsigned int DIDsInfoReceived(QJsonArray *DIDsInfo);
    int SendSMSAckReceived(bool Success);

    void StartSMSCheckTimer();
    void StopSMSCheckTimer();


    CVoipMSSMSSettings mVOIPMsSettings;

    QDate mCurStartDate;

    QTcpServer *mVOIPMsNotificationServer;
    QTcpSocket *mClientConnection;
    QTimer *mCheckNewSMSTimer;

  //  bool mFirstDownloadDone;
    QString mLastRequestCmd;
    QList<CSMSMessage> mPendingSMSSendList;

    int mSMSDownloadState;


signals:
    void DIDSFetched(QStringList);

public slots:
    void VoipServerReplyFinished(QNetworkReply*);
    void VoipNotificationServerConnected();
    void VoipNotificationServerDataAvail();
    void CheckNewSMSTimerExpired();
    void sslErrors(QNetworkReply *, QList<QSslError>);

};

#endif // CVOIPMSSMSCLIENT_H
