/// <summary>Handles the Click event of the btnRemove control. Allows the user to remove an item.</summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        ///     The <see cref="RoutedEventArgs" /> instance containing the event data.
        /// </param>
        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (lstData.SelectedIndex == -1)
            {
                return;
            }

            var conf = (Conference) lstData.SelectedItem;
            var r = MessageBox.Show(
                "Are you sure you want to delete the " + conf.Name + " conference?",
                "NBA Stats Tracker",
                MessageBoxButton.YesNo,
                MessageBoxImage.Question);
            if (r == MessageBoxResult.No)
            {
                return;
            }

            var db = new SQLiteDatabase(MainWindow.CurrentDB);

            MainWindow.Conferences.Remove(conf);
            db.Delete("Conferences", "ID = " + conf.ID);
            MainWindow.Divisions.RemoveAll(division => division.ConferenceID == conf.ID);
            db.Delete("Divisions", "Conference = " + conf.ID);

            if (MainWindow.Conferences.Count == 0)
            {
                MainWindow.Conferences.Add(new Conference { ID = 0, Name = "League" });
                db.Insert("Conferences", new Dictionary<string, string> { { "ID", "0" }, { "Name", "League" } });
                MainWindow.Divisions.Add(new Division { ID = 0, Name = "League", ConferenceID = 0 });
                db.Insert("Divisions", new Dictionary<string, string> { { "ID", "0" }, { "Name", "League" }, { "Conference", "0" } });
            }
            lstData.ItemsSource = null;
            lstData.ItemsSource = MainWindow.Conferences;

            TeamStats.CheckForInvalidDivisions();
        }
        /// <summary>Handles the Click event of the btnAdd control. Allows the user to add a new item.</summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        ///     The <see cref="RoutedEventArgs" /> instance containing the event data.
        /// </param>
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var ibw = new InputBoxWindow("Enter the name for the new conference:");
            if (ibw.ShowDialog() == true)
            {
                var name = InputBoxWindow.UserInput.Replace(':', '-');
                if (MainWindow.Conferences.Any(conference => conference.Name == name))
                {
                    MessageBox.Show("There's already a conference with the name " + name + ".");
                    return;
                }
                var usedIDs = new List<int>();
                MainWindow.Conferences.ForEach(conference => usedIDs.Add(conference.ID));
                var i = 0;
                while (usedIDs.Contains(i))
                {
                    i++;
                }

                MainWindow.Conferences.Add(new Conference { ID = i, Name = name });

                var db = new SQLiteDatabase(MainWindow.CurrentDB);
                db.Insert("Conferences", new Dictionary<string, string> { { "ID", i.ToString() }, { "Name", name } });
                lstData.ItemsSource = null;
                lstData.ItemsSource = MainWindow.Conferences;

                var confToEdit = new Conference();
                foreach (Conference item in lstData.Items)
                {
                    if (item.Name == name)
                    {
                        confToEdit = item;
                        break;
                    }
                }

                showEditConferenceWindow(confToEdit);
            }
        }
        /// <summary>
        ///     Handles the Click event of the btnOK control. The conference is renamed, the divisions are deleted and recreated, and if any
        ///     teams are left in division IDs that no longer exist, they get reassigned.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        ///     The <see cref="RoutedEventArgs" /> instance containing the event data.
        /// </param>
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            var db = new SQLiteDatabase(MainWindow.CurrentDB);
            if (String.IsNullOrWhiteSpace(txtName.Text))
            {
                txtName.Text = "League";
            }

            MainWindow.Conferences.Single(conference => conference.ID == _curConf.ID).Name = txtName.Text;
            db.Update("Conferences", new Dictionary<string, string> { { "Name", txtName.Text } }, "ID = " + _curConf.ID);

            MainWindow.Divisions.RemoveAll(division => division.ConferenceID == _curConf.ID);
            db.Delete("Divisions", "Conference = " + _curConf.ID);

            var usedIDs = new List<int>();
            db.GetDataTable("SELECT ID FROM Divisions")
              .Rows.Cast<DataRow>()
              .ToList()
              .ForEach(row => usedIDs.Add(ParseCell.GetInt32(row, "ID")));

            var list = Tools.SplitLinesToList(txtDivisions.Text, false);
            foreach (var newDiv in list)
            {
                var newName = newDiv.Replace(':', '-');
                var i = 0;
                while (usedIDs.Contains(i))
                {
                    i++;
                }
                MainWindow.Divisions.Add(new Division { ID = i, Name = newName, ConferenceID = _curConf.ID });
                usedIDs.Add(i);
            }

            if (MainWindow.Divisions.Any(division => division.ConferenceID == _curConf.ID) == false)
            {
                var i = 0;
                while (usedIDs.Contains(i))
                {
                    i++;
                }
                MainWindow.Divisions.Add(new Division { ID = i, Name = txtName.Text, ConferenceID = _curConf.ID });
                usedIDs.Add(i);
            }

            foreach (var div in MainWindow.Divisions.Where(division => division.ConferenceID == _curConf.ID))
            {
                db.Insert(
                    "Divisions",
                    new Dictionary<string, string>
                        {
                            { "ID", div.ID.ToString() },
                            { "Name", div.Name },
                            { "Conference", div.ConferenceID.ToString() }
                        });
            }

            TeamStats.CheckForInvalidDivisions();

            ListWindow.MustUpdate = true;
            Close();
        }