private void LoadMessengerLayout(string identifier, string requestText = "")
        {
            bool windowExists            = false;
            MessengerContainer msgWindow = null;

            foreach (MessengerContainer window in this.listMessengerContainer)
            {
                if (window.Name.Contains(identifier.ToString()))
                {
                    msgWindow    = window;
                    windowExists = true;
                    break;
                }

                window.Hide();
            }

            if (windowExists)
            {
                msgWindow.Show();
            }
            else
            {
                this.CreateMessengerLayout(identifier, requestText);
            }
        }
        private void CreateMessengerLayout(string messengerIdentifier, string requestText = "")
        {
            // Hide the picturebox with the logo
            this.pboxLogo.Hide();

            MessengerContainer messagerContainer = new MessengerContainer(this, this.accountInfo);

            messagerContainer.Location = new Point(250, 24);
            messagerContainer.Name     = "messenger_" + messengerIdentifier;

            messagerContainer.MouseClick      += messagerContainer_MouseClick;
            messagerContainer.InputBoxFocused += messagerContainer_InputBoxFocused;

            Controls.Add(messagerContainer);

            messagerContainer.inputContainer.Focus();

            this.listMessengerContainer.Add(messagerContainer);
            this.DumpMessage(Environment.NewLine + "\tClient started.");

            if (requestText != string.Empty)
            {
                this.DumpMessage(requestText);
            }
        }
        private void callDialog_CallStateChanged(bool state, string caller)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => callDialog_CallStateChanged(state, caller)));
                return;
            }

            // Stop the calling sound if user either accepts or declines the call
            this.callSoundPlayer.Stop();

            MessengerContainer msgWindow = GetActiveMessengerContainer(caller);

            if (msgWindow != null)
            {
                if (state)
                {
                    this.socketHelper.EstablishCall(this.accountInfo.Username);
                    this.socketHelper.SendMessage(caller, "", this.accountInfo.Username, @"/call/accept");
                }
                else
                {
                    this.socketHelper.SendMessage(caller, "", this.accountInfo.Username, @"/call/close");
                }

                if (msgWindow.InitiatingCall)
                {
                    msgWindow.InitiatingCall = false;
                }

                msgWindow.callDialog.CallStateChanged -= callDialog_CallStateChanged;
            }
        }
        private void socketHelper_CallEstablished()
        {
            MessengerContainer msgWindow = GetActiveMessengerContainer();

            if (msgWindow != null)
            {
                msgWindow.UpdateCallState();
            }
        }
        private void frmMessengerDialog_KeyDown(object sender, KeyEventArgs e)
        {
            MessengerContainer msgWindow = GetActiveMessengerContainer();

            if (msgWindow != null)
            {
                if (e.KeyCode == Keys.Escape && msgWindow.commandsContainer != null && msgWindow.commandsContainer.Visible)
                {
                    msgWindow.commandsContainer.Hide();
                    msgWindow.inputContainer.Text = string.Empty;
                }
            }
        }
        private void socketHelper_SocketDataReceived(string data)
        {
            if (socketHelper.IsHandshake)
            {
                BaseResponse <AccountInformation> response = JsonConvert.DeserializeObject <BaseResponse <AccountInformation> >(data);

                if (response != null && response.Status == 200)
                {
                    this.accountInfo = response.Data;

                    this.contacts = WebConnector.GetContactsData(accountInfo.Id);
                    this.requests = WebConnector.GetAvailableRequests(accountInfo.Id);

                    this.DisplayContactsAndRequests();

                    this.socketHelper.IsHandshake  = false;
                    this.socketHelper.IsAuthorized = true;

                    this.UpdateClientMenu(true);
                }
                else
                {
                    MessageBox.Show("An error occurred: " + response.ErrorMessage, "Error logging in", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    this.socketHelper.CloseConnection();
                    this.UpdateClientMenu(false);
                }

                return;
            }

            Message message       = Message.CreateMessageObject(data);
            bool    isCallContent = false;

            if (message == null || (message.Command.Equals("/disconnect") && message.Content.Equals("OK")))
            {
                socketHelper.CloseConnection();

                UpdateClientOnDisconnect();
                return;
            }
            else if (message.Command.Equals(@"/online"))
            {
                string username = message.Content;
                ChangeOnlineStatus(username);

                return;
            }
            else if (message.Command.Equals(@"/request"))
            {
                RequestInformation request = null;

                try
                {
                    //Console.WriteLine(message.Content);
                    request = JsonConvert.DeserializeObject <RequestInformation>(message.Content);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred while parsing request command: " + ex.Message);
                }

                if (request != null)
                {
                    this.requests.Add(request);
                    this.FillRequestsList();
                }

                return;
            }
            else if (message.Command.Equals(@"/accept"))
            {
                AccountInformation contact = null;

                try
                {
                    contact = JsonConvert.DeserializeObject <AccountInformation>(message.Content);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred while parsing accept command: " + ex.Message);
                }

                if (contact != null)
                {
                    this.contacts.ContactsList.Add(contact);
                    this.AppendContact(contact);

                    this.ChangeOnlineStatus(contact.Username);

                    socketHelper.SendMessage(contact.Username, this.accountInfo.Username, this.accountInfo.Username, @"/online");
                }

                return;
            }
            else if (message.Command.Equals(@"/offline"))
            {
                string username = message.Content;
                ChangeOnlineStatus(username);

                return;
            }
            else if (message.Command.Contains("call"))
            {
                isCallContent = true; // We set it to true to avoid dumping call ID's into the message box

                if (message.Command.Equals("/call"))
                {
                    this.callSoundPlayer.PlayLooping();

                    MessengerContainer msgWindow = GetActiveMessengerContainer(message.Sender);
                    if (msgWindow != null)
                    {
                        msgWindow.CreateCallDialog(message.Sender);
                        msgWindow.callDialog.CallStateChanged += callDialog_CallStateChanged;
                    }
                }
                else if (message.Command.Contains("/accept"))
                {
                    MessengerContainer msgWindow = GetActiveMessengerContainer(message.Sender);

                    if (msgWindow != null)
                    {
                        msgWindow.InitiatingCall = false;
                    }

                    this.socketHelper.EstablishCall(this.accountInfo.Username);
                }
                else if (message.Command.Contains("/close"))
                {
                    if (this.socketHelper.CallHelper != null)
                    {
                        this.socketHelper.CloseCall();
                    }

                    MessengerContainer msgWindow = GetActiveMessengerContainer(message.Sender);
                    if (msgWindow != null)
                    {
                        msgWindow.InitiatingCall = false;

                        msgWindow.UpdateCallState();
                        msgWindow.callDialog.CallStateChanged -= callDialog_CallStateChanged;
                    }
                }
            }

            if (message.Content != string.Empty && !isCallContent)
            {
                if (message.Sender == this.accountInfo.Username)
                {
                    this.DumpMessage(message, true);
                }
                else
                {
                    this.DumpMessage(message);

                    this.notificationSoundPlayer.Play();
                    SetContainerNotification(message.Sender);
                }
            }
        }
        private void sendIMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessengerContainer msgWindow = GetActiveMessengerContainer();

            msgWindow.inputContainer.Focus();
        }