#include "SMSDatabase.h"
#include "SMSClient.h"

CSMSDatabase::CSMSDatabase()
{
    mProgramHandle = 0;
    mNbUnreadMessages = 0;
    mConversations.clear();
}


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

int CSMSDatabase::SetAllMessages(QString DID, QList<CSMSMessage> *MessagesList)
{
    Q_UNUSED(DID)
    mConversations.clear();
    mNbUnreadMessages = 0;
 //   SetDID(DID);
    mContactsHash.clear();

    for(int i = 0; i < MessagesList->size(); i++)
    {
        CSMSMessage *CurMsg = new CSMSMessage(MessagesList->at(i));
        QString ContactNbr = CurMsg->mContact;

        if(mContactsHash.contains(ContactNbr) == true)
        {
            int Index = mContactsHash.value(ContactNbr);
            mConversations.at(Index)->InsertMessage(CurMsg);
        }
        else
        {
            CSMSConversation *NewConversation = new CSMSConversation(CContact(CurMsg->mContact));
            CContact *Contact = mContactRepository->FindContact(CurMsg->mContact);
            if(Contact != 0)
            {
                NewConversation->SetConversationContact(Contact);
            }
            mConversations.append(NewConversation);
            int Index = mConversations.size()-1;
            mContactsHash.insert(CurMsg->mContact,Index);
            mConversations.at(Index)->InsertMessage(CurMsg);
        }
    }

    qDebug("Rx %d Messages",MessagesList->size());

    return RET_OK;
}

int CSMSDatabase::SetNewMessages(QString DID, QList<CSMSMessage> *MessagesList)
{
    Q_UNUSED(DID)

    bool SyncConversations = false;

    for(int i = 0; i < MessagesList->size(); i++)
    {
        CSMSMessage *CurMsg = new CSMSMessage(MessagesList->at(i));
        QString ContactNbr = CurMsg->mContact;
  //      if(CurMsg->mType == SMS_RECEIVED_TYPE)
        {
            mNbUnreadMessages++;
            CurMsg->mIsRead = false;
        }

        if(mContactsHash.contains(ContactNbr) == true)
        {
            int Index = mContactsHash.value(ContactNbr);
            mConversations.at(Index)->InsertNewMessage(CurMsg);


            mProgramHandle->NewMessageNotification(mConversations.at(Index),0);

//            if(Index != 0)
//            {
//                CSMSConversation *Conversation = mConversations.takeAt(Index);
//                mConversations.prepend(Conversation);
//                mContactsHash.clear();

//                for(int i = 0; i < mConversations.size(); i++)
//                {
//                    mContactsHash.insert(mConversations.at(i)->mConversationContact.mRAWContactNbr,i);
//                }

//                SyncConversations = true;
//            }
        }
        else
        {
            CSMSConversation *NewConversation = new CSMSConversation(CContact(CurMsg->mContact));
            mConversations.append(NewConversation);
            int Index = mConversations.size()-1;
            mContactsHash.insert(CurMsg->mContact,Index);
            mConversations.at(Index)->InsertNewMessage(CurMsg);

            SyncConversations = true;
            mProgramHandle->MessageCountChangeNotification(mNbUnreadMessages);
        }
    }

    if(SyncConversations)
    {
        mProgramHandle->SyncConversations();
    }

    qDebug("Rx %d New Messages",MessagesList->size());

    return RET_OK;
}

QList<CSMSConversation*>* CSMSDatabase::GetConversations(QString DID)
{
    Q_UNUSED(DID);

    return &mConversations;
}

CSMSConversation * CSMSDatabase::GetConversation(QString Contact)
{
    if(mContactsHash.contains(Contact))
    {
        return mConversations.at(mContactsHash.value(Contact));
    }

    return 0;
}

int CSMSDatabase::SetDID(QString DID)
{
    mClientDID = DID;

    mFormattedDID = DID;
    mFormattedDID.insert(0,'(');
    mFormattedDID.insert(4,") ");
    mFormattedDID.insert(9,'-');

    return RET_OK;

}

int CSMSDatabase::SetConversationAsRead(CSMSConversation *Conversation, bool ForceAll)
{
    if(Conversation == 0)
    {
        return RET_ERROR;
    }

    QList<CSMSMessage*> *Messages = Conversation->GetMessages();

    for(int i = 0; i < Messages->size(); i++)
    {
        if(Messages->at(i)->mIsRead == true)
        {
            if(ForceAll == false)
            {
                break;
            }
        }
        else
        {
            Messages->at(i)->mIsRead = true;
       //     if(Messages->at(i)->mType == SMS_RECEIVED_TYPE)
            {
                mNbUnreadMessages--;
            }
        }
    }

    mProgramHandle->MessageCountChangeNotification(mNbUnreadMessages);

    return RET_OK;
}


int CSMSDatabase::ContactsRepositoryChanged()
{
    CContact *Contact;
    for(int i = 0; i < mConversations.size(); i++)
    {
        Contact = mContactRepository->FindContact(mConversations.at(i)->GetConversationContact()->mRAWContactNbr);
        if(Contact != 0)
        {
            mConversations.at(i)->SetConversationContact(Contact);
        }
    }
    return RET_OK;
}








