public MainPage()
        {
            firstOnelineGamesLoaded = false;

            InitializeComponent();
            NavigationCacheMode = NavigationCacheMode.Required; // Don't reload, but cache page when navigating
            App.Current.FicsClientReady += OnFicsClientReady;

            foreach (GameType gameType in Enum.GetValues(typeof(GameType)).OfType<GameType>().OrderBy(o => o.ToString()))
            {
                ListView listView = new ListView();
                listView.Visibility = Visibility.Collapsed;
                listView.IsItemClickEnabled = true;
                listView.ItemClick += (o, e) =>
                {
                    dynamic gameText = e.ClickedItem;
                    Game g = gameText.Game;

                    if (!g.Private)
                    {
                        Frame rootFrame = Window.Current.Content as Frame;
                        rootFrame.Navigate(typeof(GamePage), g);
                    }
                };
                OnlineGamesList.Items.Clear();
                OnlineChildGamesPanel.Children.Add(listView);
                gameTypeGameLists.Add(gameType, listView);
            }

            foreach (var typeGame in gameTypeGameLists.OrderBy(kvp => kvp.Key.ToString()))
            {
                UpdateGamesListItem(typeGame);
            }

            OnlineGamesList.IsItemClickEnabled = true;
            OnlineGamesList.ItemClick += (o, e) =>
            {
                foreach (var typeGame in gameTypeGameLists)
                {
                    typeGame.Value.Visibility = Visibility.Collapsed;
                }

                dynamic text = e.ClickedItem;

                gameTypeGameLists[(GameType)text.GameType].Visibility = Visibility.Visible;
            };

            OnlineGames = true;

#if xxDEBUG
            GameControl GameControl = new GameControl();

            GameControl.VerticalAlignment = VerticalAlignment.Bottom;
            GameControl.HorizontalAlignment = HorizontalAlignment.Center;
            GameControl.BlackShouldBeDown = false;
            GameControl.Width = 600;
            GameControl.Height = 600;
            Grid.Children.Insert(0, GameControl);

            GameInfo gameInfo = new GameInfo();

            gameInfo.WhitePlayer = new Player()
            {
                Username = "******",
                Rating = 1500,
            };
            gameInfo.BlackPlayer = new Player()
            {
                Username = "******",
                Rating = 1500,
            };
            gameInfo.Type = GameType.Crazyhouse;
            GameControl.InitializeGameInfo(gameInfo);

            GameState gameState = new GameState();

            gameState.Board = new ChessPieceWithColor[8, 8];
            gameState.Board[0, 0] = ChessPieceWithColor.BlackRook;
            gameState.Board[0, 1] = ChessPieceWithColor.BlackKnight;
            gameState.Board[0, 2] = ChessPieceWithColor.BlackBishop;
            gameState.Board[0, 3] = ChessPieceWithColor.BlackQueen;
            gameState.Board[0, 4] = ChessPieceWithColor.BlackKing;
            gameState.Board[0, 5] = ChessPieceWithColor.BlackBishop;
            gameState.Board[0, 6] = ChessPieceWithColor.BlackKnight;
            gameState.Board[0, 7] = ChessPieceWithColor.BlackRook;
            gameState.Board[1, 0] = ChessPieceWithColor.BlackPawn;
            gameState.Board[1, 1] = ChessPieceWithColor.BlackPawn;
            gameState.Board[1, 2] = ChessPieceWithColor.BlackPawn;
            gameState.Board[1, 3] = ChessPieceWithColor.BlackPawn;
            gameState.Board[1, 4] = ChessPieceWithColor.BlackPawn;
            gameState.Board[1, 5] = ChessPieceWithColor.BlackPawn;
            gameState.Board[1, 6] = ChessPieceWithColor.BlackPawn;
            gameState.Board[1, 7] = ChessPieceWithColor.BlackPawn;
            gameState.Board[6, 0] = ChessPieceWithColor.WhitePawn;
            gameState.Board[6, 1] = ChessPieceWithColor.WhitePawn;
            gameState.Board[6, 2] = ChessPieceWithColor.WhitePawn;
            gameState.Board[6, 3] = ChessPieceWithColor.WhitePawn;
            gameState.Board[6, 4] = ChessPieceWithColor.WhitePawn;
            gameState.Board[6, 5] = ChessPieceWithColor.WhitePawn;
            gameState.Board[6, 6] = ChessPieceWithColor.WhitePawn;
            gameState.Board[6, 7] = ChessPieceWithColor.WhitePawn;
            gameState.Board[7, 0] = ChessPieceWithColor.WhiteRook;
            gameState.Board[7, 1] = ChessPieceWithColor.WhiteKnight;
            gameState.Board[7, 2] = ChessPieceWithColor.WhiteBishop;
            gameState.Board[7, 3] = ChessPieceWithColor.WhiteQueen;
            gameState.Board[7, 4] = ChessPieceWithColor.WhiteKing;
            gameState.Board[7, 5] = ChessPieceWithColor.WhiteBishop;
            gameState.Board[7, 6] = ChessPieceWithColor.WhiteKnight;
            gameState.Board[7, 7] = ChessPieceWithColor.WhiteRook;
            gameState.WhiteClock = TimeSpan.FromMilliseconds(10436);
            gameState.BlackClock = TimeSpan.FromMilliseconds(9456);
            gameState.WhiteMove = true;
            gameState.WhitePieces = new List<ChessPieceType>() { ChessPieceType.Pawn, ChessPieceType.Pawn, ChessPieceType.Rook, ChessPieceType.Rook };
            gameState.BlackPieces = new List<ChessPieceType>() { ChessPieceType.Pawn, ChessPieceType.Knight };
            gameState.LastMoveVerbose = "Q/d1-a4";

            GameControl.OnGameStateChanged(gameState);
#endif
        }
        public void OnGameStateChanged(GameState gameState)
        {
            if (gameState.Board != null)
            {
                for (int y = 0; y < 8; y++)
                    for (int x = 0; x < 8; x++)
                        ChessBoard[y, x] = gameState.Board[y, x];

                WhitePlayerSymbol.Opacity = gameState.WhiteMove ? 1 : 0;
                BlackPlayerSymbol.Opacity = !gameState.WhiteMove ? 1 : 0;

                ShowTime(WhitePlayerTime, WhiteClock = gameState.WhiteClock);
                ShowTime(BlackPlayerTime, BlackClock = gameState.BlackClock);

                dispatcherTimer.Start();
                lastTick = DateTime.UtcNow;

                if (gameState.LastMoveVerbose != null)
                {
                    List<Tuple<int, int>> lastMoveFields = new List<Tuple<int, int>>();

                    if (gameState.LastMoveVerbose == "o-o")
                    {
                        if (gameState.WhiteMove)
                        {
                            lastMoveFields.Add(Tuple.Create(0, 4));
                            lastMoveFields.Add(Tuple.Create(0, 6));
                        }
                        else
                        {
                            lastMoveFields.Add(Tuple.Create(7, 4));
                            lastMoveFields.Add(Tuple.Create(7, 6));
                        }
                    }
                    else if (gameState.LastMoveVerbose == "o-o-o")
                    {
                        if (gameState.WhiteMove)
                        {
                            lastMoveFields.Add(Tuple.Create(0, 4));
                            lastMoveFields.Add(Tuple.Create(0, 2));
                        }
                        else
                        {
                            lastMoveFields.Add(Tuple.Create(7, 4));
                            lastMoveFields.Add(Tuple.Create(7, 2));
                        }
                    }
                    else if (gameState.LastMoveVerbose.Contains('/'))
                    {
                        string[] tokens = gameState.LastMoveVerbose.Split("/-".ToCharArray());

                        if (tokens[1] != "@@")
                            lastMoveFields.Add(ConvertNotationToPosition(tokens[1]));
                        lastMoveFields.Add(ConvertNotationToPosition(tokens[2]));
                    }
                    else if (gameState.LastMoveVerbose != "none")
                    {
                        Debug.Assert(false, gameState.LastMoveVerbose);
                    }

                    ChessBoard.SetMarkedFields(lastMoveFields);
                    GameResultText.Text = gameState.LastMoveVerbose;
                }
            }

            if (gameState.WhitePieces != null)
                foreach (ChessPieceType pieceType in Enum.GetValues(typeof(ChessPieceType)))
                    SetChessPieceCount(ChessPieceColor.White, pieceType, gameState.WhitePieces.Count(cp => cp == pieceType));

            if (gameState.BlackPieces != null)
                foreach (ChessPieceType pieceType in Enum.GetValues(typeof(ChessPieceType)))
                    SetChessPieceCount(ChessPieceColor.Black, pieceType, gameState.BlackPieces.Count(cp => cp == pieceType));
        }