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



CSMSClient::CSMSClient(CSMSGui *GuiHandle, CVoipMSSMSSettings *ProgramSettings)
{
    qDebug("Creation...");
    mSMSDatabase.mProgramHandle = this;
    mGuiHandle = GuiHandle;
    mGuiHandle->mProgramHandle = this;
    mGuiHandle->mSMSDatabase = &mSMSDatabase;
    mSettings = ProgramSettings;

//    mSystemTrayManager = new CSystemTrayManager;
//    mSystemTrayManager->mProgramHandle = this;
//    mSettingsWindow = new CSettingsWindow();
//    mSettingsWindow->mProgramHandle = this;
    //mMainPanel = new Panel;
 //   mMasterCtrlIF = new CMasterCtrlInterface();


}

CSMSClient::~CSMSClient()
{
//    delete mSystemTrayManager;
//    delete mSettingsWindow;
 //   delete mMainPanel;
 //   delete mMasterCtrlIF;

}

void CSMSClient::Start()
{
    qDebug("Started!");
    mMasterCtrlIF = new CSMSMasterCtrlInterface(this);
    mSMSDatabase.mContactRepository = &mContactRepository;

    mMasterCtrlIF->ConnectToMasterCtrl();
}

int CSMSClient::SetAllMsgsDatabase(QString DID, QList<CSMSMessage> *MessagesList)
{
    if(mSMSDatabase.SetAllMessages(DID,MessagesList) == RET_OK)
    {
        mGuiHandle->FullConversationsUpdate();
    }
    return RET_OK;
}

int CSMSClient::SetDIDDatabase(QString DID)
{
    mSMSDatabase.SetDID(DID);
    return RET_OK;
}

int CSMSClient::NewMessagesEvent(QList<CSMSMessage> *MessagesList)
{
    mSMSDatabase.SetNewMessages("",MessagesList);
    return RET_OK;
}

int CSMSClient::SetContacts(QList<CContact> *ContactsList)
{
    mContactRepository.SetContacts(ContactsList);
    mSMSDatabase.ContactsRepositoryChanged();
    return RET_OK;
}

int CSMSClient::ShowConversation(QString Contact)
{
    CSMSConversation* Conversation;
    Conversation = mSMSDatabase.GetConversation(Contact);

    if(Conversation == 0)
    {
        return RET_ERROR;
    }

    return RET_OK;
}


int CSMSClient::SendSMSMessage(CSMSMessage Message)
{
    mMasterCtrlIF->SendSMS(Message);
    return RET_OK;
}

int CSMSClient::NewMessageNotification(CSMSConversation *Conversation, int MessageIndex)
{
    mGuiHandle->NewMessageNotification(Conversation,MessageIndex);

    MessageCountChangeNotification(mSMSDatabase.GetNbUnreadMsgs());

    return RET_OK;
}

int CSMSClient::SyncConversations()
{
    return RET_OK;
}

int CSMSClient::MessageCountChangeNotification(int MsgCount)
{
    mTrayIconMgr->NewSMSMessagesPendingCount(MsgCount);
    return RET_OK;
}

