using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Windows.Resources;

namespace EmbeddedSource
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // Create a global storage variable.
        XDocument MyData;

        // Track the current item number.
        Int32 CurrentItem;

        // Record the maximum number of items.
        Int32 MaxItems;

        public MainWindow()
        {
            InitializeComponent();

            // Create a URI that points to the data embedded in the
            // application.
            Uri FileUri = new Uri("/TestData.XML", UriKind.Relative);

            // Open a connection to this data.
            StreamResourceInfo Info = Application.GetResourceStream(FileUri);

            // Use the connection as a means of loading information into
            // the global storage variable.
            MyData = XDocument.Load(Info.Stream);

            // Place the first data item into txtValue.
            txtValue.Text = MyData.Root.Element("Item").Value;

            // Configure the buttons for use.
            btnNext.IsEnabled = true;
            btnPrevious.IsEnabled = false;

            // Set the current item number.
            CurrentItem = 0;

            // Obtain the maximum number of items.
            MaxItems = MyData.Root.Elements().Count();
        }

        private void btnQuit_Click(object sender, RoutedEventArgs e)
        {
            // End the program.
            Close();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            // Determine if there are more items to display.
            if (CurrentItem + 1 < MaxItems)
            {
                // Increment the item number.
                CurrentItem++;

                // Change the output.
                txtValue.Text = MyData.Root.Elements().ElementAt(CurrentItem).Value;

                // Make sure the Previous button is enabled.
                btnPrevious.IsEnabled = true;
            }
            // Otherwise, there aren't any more items to display.
            else
            {
                // Disable the Next button.
                btnNext.IsEnabled = false;

                // Display a message to the user.
                MessageBox.Show("End of the items!");
            }
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            // Determine if there are more items to display.
            if (CurrentItem > 0)
            {
                // Decrement the item number.
                CurrentItem--;

                // Change the output.
                txtValue.Text = MyData.Root.Elements().ElementAt(CurrentItem).Value;

                // Make sure the Next button is enabled.
                btnNext.IsEnabled = true;
            }
            // Otherwise, there aren't any more items to display.
            else
            {
                // Disable the Previous button.
                btnPrevious.IsEnabled = false;

                // Display a message to the user.
                MessageBox.Show("Beginning of the items!");
            }
        }
    }
}
