class BuddyChatHistoryManager
{
    constructor()
    {
        this.resetAllHistory();
    }

    addMessage(targetBuddyName, senderName, isItMe, message)
    {
        // Initialize queue
        if (!this._buddyChatLists.has(targetBuddyName))
        {
            this._buddyChatLists.set(targetBuddyName, new Array());
            this._unreadMsgCounters.set(targetBuddyName, 0);
        }

        // Add message to queue
        const chatMsg = {
            target: targetBuddyName,
            sender: senderName,
			message: message,
			sentByMe: isItMe,
			date: new Date(),
        };

        this._buddyChatLists.get(targetBuddyName).push(chatMsg);

        // Return chat message
        return chatMsg;
    }

    increaseUnreadMessagesCount(targetBuddyName)
    {
        // Get current counter of unread messages
        let cnt = this._unreadMsgCounters.get(targetBuddyName);

        // Increase and update unread messages counter
        cnt++;
        this._unreadMsgCounters.set(targetBuddyName, cnt);

        // Return total number of unread messages
        return cnt;
    }

    getBuddyChatMessages(targetBuddyName)
    {
        return this._buddyChatLists.get(targetBuddyName);
    }

    getUnreadMessagesCount(targetBuddyName)
    {
        if (this._unreadMsgCounters.has(targetBuddyName))
            return this._unreadMsgCounters.get(targetBuddyName);

        return 0;
    }

    resetUnreadMessagesCount(targetBuddyName)
    {
        if (this._unreadMsgCounters.has(targetBuddyName))
            this._unreadMsgCounters.set(targetBuddyName, 0);
    }

    getUnreadChatsCount()
    {
        let cnt = 0;

        this._unreadMsgCounters.forEach((value, key) => {
            if (value > 0)
                cnt++;
        });
        
        return cnt;
    }

    resetHistory(targetBuddyName)
    {
        this._buddyChatLists.delete(targetBuddyName);
        this._unreadMsgCounters.delete(targetBuddyName);
    }

    resetAllHistory()
    {
        this._buddyChatLists = new Map();
        this._unreadMsgCounters = new Map();
    }
}

export default new BuddyChatHistoryManager();