/*
         * Display the join table UI
         * UC-02 R02
         */
        /*
           public void displayJoinGameUI()
           {
           joinTableUI = new JoinTableUI(this);
           joinTableUI.ShowDialog();
           }
           */

        /*
         * Send the Join Game request to the Server.
         */
        public void joinGame(string nickname, IPAddress serverIP)
        {
            this.nickname = nickname;

            client = new Client(this, nickname);
            client.InitializeConnection(serverIP);

            if (!client.Connected)
                return;

            //set hasGame to true
            hasGame = true;

            //Display the non-host player version of TableUI
            sendToHost("J" + nickname);

            tableUI = new TableUI(this);
            /*
            I commented these lines out.  Previously, they were only enabled if you were the creator.  Which kind of makes sense.
            tableUI.addAIButton.IsEnabled = false;
            tableUI.removeAIButton.IsEnabled = false;
            tableUI.leaders_Checkbox.IsEnabled = false;
            */
            tableUI.ShowDialog();

            if (playerNames == null)
            {
                // If we get here and playerNames is null, the user closed the table UI dialog box
                // before pressing the ready button (or another player didn't press the ready button)
                // In that case, we will quit the game without showing the Main Window.
                hasGame = false;
                client.CloseConnection();
            }

            // create the leader draft window if the Leaders expansion is enabled.
            if (isLeadersEnabled)
                leaderDraftWindow = new LeaderDraft(this, false);
        }
        /// <summary>
        /// Client has received a message
        /// Call the appropriate action based on the first character
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void receiveMessage(string message)
        {
            bool messageHandled = false;

            int msgParamIndex = message.IndexOf('&');

            if (msgParamIndex < 0)
                msgParamIndex = message.Length;

            NameValueCollection qcoll;

            switch (message.Substring(0, msgParamIndex))
            {
                case "UpdateUI":
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));

                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        gameUI.updateCoinsAndCardsPlayed(qcoll);
                    }));
                    messageHandled = true;
                    break;

                case "ChngMode":
                    // Basic/Leaders/Cities
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));

                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        if (qcoll["Leaders"] != null)
                        {
                            tableUI.leaders_Checkbox.IsChecked = qcoll["Leaders"] == "True";
                            isLeadersEnabled = (bool)tableUI.leaders_Checkbox.IsChecked;
                        }

                        if (qcoll["Cities"] != null)
                        {
                            tableUI.cities_Checkbox.IsChecked = qcoll["Cities"] == "True";
                        }
                    }));
                    messageHandled = true;
                    break;

                case "Courtesn":        // send which neighboring leader is being copied.
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));

                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        LeaderDraft leaderDraft = new LeaderDraft(this, true);
                        leaderDraft.UpdateUI(qcoll);
                        leaderDraft.Show();
                    }));
                    messageHandled = true;
                    break;

                case "EnableFB":
                    isFreeBuildButtonEnabled = true;
                    messageHandled = true;
                    break;

                case "FinalSco":
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));
                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        finalScoreUI = new FinalScore(gameUI, qcoll);
                        finalScoreUI.Show();
                    }));
                    messageHandled = true;
                    break;

                case "LdrDraft":
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));
                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        leaderDraftWindow.UpdateUI(qcoll);
                        leaderDraftWindow.Show();
                    }));
                    messageHandled = true;
                    break;

                case "LeadrIcn":
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));
                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        gameUI.updateLeaderIcons(qcoll);
                    }));
                    messageHandled = true;
                    break;

                case "Military":
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));

                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        gameUI.updateMilitaryTokens(qcoll);
                    }));
                    messageHandled = true;
                    break;

                case "PlyrInfo":
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));
                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        tableUI.SetPlayerInfo(qcoll);
                    }));

                    messageHandled = true;
                    break;

                case "Failed":
                    MessageBox.Show(message.Substring(msgParamIndex + 1));
                    messageHandled = true;
                    break;

                case "StrtGame":
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));
                    int nPlayers = int.Parse(qcoll["PlayerCount"]);
                    //tell the server UI initialisation is done

                    // I may be able to set this to playerNames, but I'm not sure about thread safety.
                    playerNames = qcoll["PlayerNames"].Split(',');

                    if (playerNames.Length != nPlayers)
                    {
                        throw new Exception(string.Format("Server said there were {0} players, but sent {1} names.", nPlayers, playerNames.Length));
                    }

                    //close the TableUI
                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        tableUI.Close();
                    }));
                    messageHandled = true;
                    break;

                case "SetBoard":
                    // Parse the query string variables into a NameValueCollection.
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));

                    foreach (string s in qcoll.Keys)
                    {
                        Application.Current.Dispatcher.Invoke(new Action(delegate
                        {
                            gameUI.showBoardImage(s, qcoll[s]);
                        }));
                    }

                    // Tell game server this client is ready to receive its first UI update, which will
                    // include coins and hand of cards.
                    // sendToHost("r");

                    messageHandled = true;
                    break;

                case "SetPlyrH":        // Set player hand
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));

                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        gameUI.showHandPanel(qcoll);
                    }));

                    messageHandled = true;
                    break;

                case "GetDebtTokens":
                    qcoll = HttpUtility.ParseQueryString(message.Substring(msgParamIndex + 1));

                    Application.Current.Dispatcher.Invoke(new Action(delegate
                    {
                        GetDebtToken debtTokenResponse = new GetDebtToken(this, qcoll);
                        debtTokenResponse.Show();
                    }));

                    messageHandled = true;
                    break;
            }

            if (!messageHandled)
            {
                throw new NotImplementedException();
            }

            /*
            //chat
            //enable Olympia power OR Rome power
            //activate the Olympia UI
            //receive the information on the current turn
            if (message[0] == 'T')
            {
                //get the current turn information
                //currentTurn = int.Parse(message[1] + "");
            }
            //received an unable to join message from the server
            //UC-02 R07
            else if (message[0] == '0')
            {
                MessageBox.Show(message.Substring(2));

                tableUI.Close();

                // displayJoinGameUI();
            }
            else if (message[0] == '1')
            {
                // don't do anything
            }
            else
            {
                // recieved a message from the server that the client cannot handle.
                throw new Exception();
            }
            */
        }