public void Refresh()
        {
            if (!_canWriteData)
            {
                return;
            }
            _canWriteData = false;

            var selectedTeam = TabTeam.SelectedIndex;

            // refresh all roster info
            var blitzData = Blitzball.ReadBlitzballData();

            _teamData = blitzData.TeamData;
            var blitzTeamSizes = Blitzball.GetTeamSizes();
            var teamSize       = blitzTeamSizes[selectedTeam];

            if (teamSize < 6)
            {
                ComboRosterSize.SelectedIndex = 0;
            }
            else
            {
                ComboRosterSize.SelectedIndex = teamSize - 6;
            }

            // refresh panel for current team
            for (int i = 0; i < 8; i++)
            {
                var playerButton = (Button)FindName("ButtonPlayer" + (i + 1).ToString().Trim());
                var contractBox  = (TextBox)FindName("TextPlayer" + (i + 1).ToString().Trim());

                if (i > teamSize - 1)
                {
                    playerButton.Visibility = Visibility.Collapsed;
                    contractBox.Visibility  = Visibility.Collapsed;

                    continue;
                }

                playerButton.Visibility = Visibility.Visible;
                contractBox.Visibility  = Visibility.Visible;

                var playerIndex = _teamData[(selectedTeam * 8) + i];

                if (playerIndex >= 0x3C) // empty slot
                {
                    playerButton.Content = "< EMPTY >";
                    contractBox.Text     = string.Empty;
                }
                else
                {
                    var player = BlitzballValues.Players.FirstOrDefault(p => p.Index == playerIndex);
                    playerButton.Content = player.Name;
                    contractBox.Text     = blitzData.PlayerContracts[player.Index].ToString();
                }
            }

            _canWriteData = true;
        }
        private void TextPlayer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                return;
            }
            try
            {
                var textBox     = sender as TextBox;
                var buttonIndex = int.Parse(textBox.Name.Substring(10));
                var teamIndex   = TabTeam.SelectedIndex;

                var playerIndex = _teamData[teamIndex * 8 + buttonIndex - 1];

                var blitzData = Blitzball.ReadBlitzballData();
                blitzData.PlayerContracts[playerIndex] = byte.Parse(textBox.Text);
                Blitzball.WriteBlitzballData(blitzData);

                textBox.SelectAll();
            }
            catch
            {
                Error.Show("Please enter a number between 0 and 255.");
            }
        }
        public void Refresh()
        {
            _refresh = true;
            var prizes    = BlitzballValues.Prizes;
            var blitzData = Blitzball.ReadBlitzballData();

            // Tournament status
            var tournamentStatusIndex =
                Array.IndexOf(BlitzballValues.TournamentStates, BlitzballValues.TournamentStates.First(state => state.Index == blitzData.TournamentStatus));

            ComboTournamentStatus.SelectedIndex = tournamentStatusIndex;

            // Prizes
            for (int i = 0; i < 8; i++)
            {
                var prizeButton = FindName($"Prize{i}") as Button;
                if (prizeButton == null)
                {
                    continue;
                }
                var currentPrize = prizes.FirstOrDefault(prize => prize.Index == blitzData.BlitzballPrizes[i]);
                if (currentPrize != null)
                {
                    prizeButton.Content = currentPrize.Name;
                }
                else
                {
                    prizeButton.Content = $"???? [{blitzData.BlitzballPrizes[i].ToString("X2")}]";
                }
            }

            // Tournament teams
            for (int i = 0; i < 6; i++)
            {
                var comboTeam = FindName($"ComboTeam{i}") as ComboBox;
                if (comboTeam == null)
                {
                    continue;
                }

                var currentTeam = BlitzballValues.Teams.First(team => team.Index == blitzData.TournamentMatchups[i]);
                comboTeam.SelectedIndex = Array.IndexOf(BlitzballValues.Teams, currentTeam);
            }

            // Tournament winners
            for (int i = 0; i < 8; i++)
            {
                var comboTeam = FindName($"ComboWinner{i}") as ComboBox;
                if (comboTeam == null)
                {
                    continue;
                }

                var currentTeam = BlitzballValues.Teams.First(team => team.Index == blitzData.TournamentWinners[i]);
                comboTeam.SelectedIndex = Array.IndexOf(BlitzballValues.Teams, currentTeam);
            }
            _refresh = false;
        }
        private void CurrentRound_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_refresh)
            {
                return;
            }
            var roundBox   = sender as ComboBox;
            var roundIndex = BlitzballValues.LeagueStates[roundBox.SelectedIndex].Index;
            var blitzData  = Blitzball.ReadBlitzballData();

            blitzData.LeagueStatus = (byte)roundIndex;
            Blitzball.WriteBlitzballData(blitzData);
            Refresh();
        }
        private void ComboWinner_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_refresh)
            {
                return;
            }
            var teamBox   = sender as ComboBox;
            var teamNum   = int.Parse(teamBox.Name.Substring(11));
            var teamIndex = BlitzballValues.Teams[teamBox.SelectedIndex].Index;
            var blitzData = Blitzball.ReadBlitzballData();

            blitzData.TournamentWinners[teamNum] = (byte)teamIndex;
            Blitzball.WriteBlitzballData(blitzData);
            Refresh();
        }
        public void Refresh()
        {
            _refresh = true;
            var prizes    = BlitzballValues.Prizes;
            var blitzData = Blitzball.ReadBlitzballData();

            // Update League status
            var leagueStatusIndex =
                Array.IndexOf(BlitzballValues.LeagueStates, BlitzballValues.LeagueStates.First(state => state.Index == blitzData.LeagueStatus));

            ComboLeagueStatus.SelectedIndex = leagueStatusIndex;

            // Update team matchups
            for (int i = 0; i < 6; i++)
            {
                var teamCombo = FindName($"LeagueTeam{i}") as ComboBox;
                if (teamCombo == null)
                {
                    continue;
                }
                teamCombo.SelectedIndex = blitzData.LeagueMatchups[i];
            }

            // Update prizes
            for (int i = 0; i < 8; i++)
            {
                var prizeButton = FindName($"Prize{i}") as Button;
                if (prizeButton == null)
                {
                    continue;
                }
                var currentPrize = prizes.FirstOrDefault(prize => prize.Index == blitzData.BlitzballPrizes[i]);
                if (currentPrize != null)
                {
                    prizeButton.Content = currentPrize.Name;
                }
                else
                {
                    prizeButton.Content = $"???? [{blitzData.BlitzballPrizes[i].ToString("X2")}]";
                }
            }

            _refresh = false;
        }
        private void Prize_OnClick(object sender, RoutedEventArgs e)
        {
            var prizeButton  = sender as Button;
            var prizeDialog  = new SearchDialog(BlitzballValues.Prizes.Select(prize => prize.Name).ToList(), string.Empty, false);
            var dialogResult = prizeDialog.ShowDialog();

            if (dialogResult == false)
            {
                return;
            }

            var prizeNumber = int.Parse(prizeButton.Name.Substring(5));
            var prizeIndex  = BlitzballValues.Prizes[prizeDialog.ResultIndex].Index;
            var blitzData   = Blitzball.ReadBlitzballData();

            blitzData.BlitzballPrizes[prizeNumber] = (ushort)prizeIndex;
            Blitzball.WriteBlitzballData(blitzData);

            Refresh();
        }
        private void MovePlayer(int playerIndex, int destTeamIndex, int destPos)
        {
            var blitzData = Blitzball.ReadBlitzballData();



            // Find index of player if already assigned to team
            var playerTeamIndex = Array.IndexOf(blitzData.TeamData, (byte)playerIndex);

            var destTeamData = new byte[8];

            Array.Copy(blitzData.TeamData, destTeamIndex * 8, destTeamData, 0, 8);
            var destTeamSize = 8 - Array.FindAll(destTeamData, b => b == (byte)0x3C).Length;

            if (destTeamSize == 8)
            {
                return;
            }

            if (playerIndex == -1)
            {
                // Check destination team

                destTeamData[destPos] = 0x3C;
                Array.Copy(destTeamData, 0, blitzData.TeamData, destTeamIndex * 8, 8);
                Blitzball.WriteBlitzballData(blitzData);
                Blitzball.SetTeamSize(destTeamIndex, (byte)(destTeamSize + 1));
                return;
            }

            var player   = BlitzballValues.Players[playerIndex];
            var destTeam = BlitzballValues.Teams[destTeamIndex];

            // If assigned to team, we need to remove player from the team
            if (playerTeamIndex != -1)
            {
                var sourceTeamIndex = playerTeamIndex / 8;
                var sourceTeam      = BlitzballValues.Teams[sourceTeamIndex];

                // Copy team data to array
                var sourceTeamData = new byte[8];
                Array.Copy(blitzData.TeamData, sourceTeamIndex * 8, sourceTeamData, 0, 8);

                // Check source team size
                var sourceTeamSize = Array.FindAll(sourceTeamData, b => b != (byte)0x3C).Length;
                if (sourceTeamSize <= 6)
                {
                    // Team too small to remove player, return error
                    MessageBox.Show(
                        string.Format(
                            "{0} is already assigned to the {1}, but could not be removed as the {1} must have at least 6 players.\n\n" +
                            "Remove {0} from the {1} or add another player before trying again.",
                            player.Name, sourceTeam.Name),
                        "Unable to assign player", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                // Player is in a team, prompt user
                var movePlayer =
                    MessageBox.Show(
                        string.Format("{0} is already in the {1}. Press OK to move {0} to the {2}", player.Name, sourceTeam.Name,
                                      destTeam.Name), "Player already assigned",
                        MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (movePlayer != MessageBoxResult.OK)
                {
                    return;
                }

                // Remove player from source team

                // Find player position
                var playerPos = Array.IndexOf(sourceTeamData, (byte)player.Index);

                for (int i = playerPos; i < 7; i++)
                {
                    sourceTeamData[i] = sourceTeamData[i + 1];  // Move other players up one position
                    sourceTeamData[7] = 0x3C;                   // Empty final slot
                }

                // Copy updated roster back to blitz data
                Array.Copy(sourceTeamData, 0, blitzData.TeamData, sourceTeamIndex * 8, 8);
                Blitzball.WriteBlitzballData(blitzData);

                // Resize team
                Blitzball.SetTeamSize(sourceTeamIndex, (byte)(sourceTeamSize - 1));
            }

            // Check destination team

            if (destTeamSize == 8)
            {
                return;
            }

            // Insert player into team roster
            Array.Copy(blitzData.TeamData, destTeamIndex * 8, destTeamData, 0, 8);

            destTeamData[destPos] = (byte)player.Index;
            Array.Copy(destTeamData, 0, blitzData.TeamData, destTeamIndex * 8, 8);
            Blitzball.WriteBlitzballData(blitzData);
        }