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.IO;
using System.Reflection;

namespace WPF_XML
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // Create a global variable containing the name and
        // location of the settings file.
        String SettingsFile;

        // Create a global variable used to hold the directory
        // for the settings file.
        String SettingsDirectory;

        public MainWindow()
        {
            // Perform the default configuration.
            InitializeComponent();

            // Obtain the configuration information for this application.
            Object[] Configuration = Assembly.GetEntryAssembly().GetCustomAttributes(false);

            // Obtain the company name.
            String CompanyName = "Company";
            foreach (var Value in Configuration)
                if (Value.GetType() == typeof(AssemblyCompanyAttribute))
                {
                    CompanyName = ((AssemblyCompanyAttribute)Value).Company;
                    break;
                }

            // Obtain the executable name.
            String Title = "Title";
            foreach (var Value in Configuration)
                if (Value.GetType() == typeof(AssemblyTitleAttribute))
                {
                    Title = ((AssemblyTitleAttribute)Value).Title;
                    break;
                }

            // Obtain the version number.
            String Version = "0.0.0.0";
            foreach (var Value in Configuration)
                if (Value.GetType() == typeof(AssemblyFileVersionAttribute))
                {
                    Version = ((AssemblyFileVersionAttribute)Value).Version;
                    break;
                }

            // Store the location of the settings directory.
            SettingsDirectory = 
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + 
                "\\" + CompanyName + "\\" + Title + "\\" + Version;

            // Store the location of the settings file.
            SettingsFile = SettingsDirectory + "\\Settings.XML";

            // Check for a settings file.
            if (File.Exists(SettingsFile))
            {
                // Load the file containing the settings.
                XDocument SettingData = XDocument.Load(SettingsFile);

                // Change the control settings to match the file settings.
                txtMessage.Text = SettingData.Root.Element("txtMessage").Value;
                chkChecked.IsChecked = 
                    Boolean.Parse(SettingData.Root.Element("chkChecked").Value);
            }
        }

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

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // Create a document to hold the settings.
            XDocument SettingData = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XElement("Settings",
                    new XElement("txtMessage", txtMessage.Text),
                    new XElement("chkChecked", chkChecked.IsChecked)));

            // Create the folder used to store the data if necessary.
            if (!Directory.Exists(SettingsDirectory))
                Directory.CreateDirectory(SettingsDirectory);

            // Save the settings to disk.
            SettingData.Save(SettingsFile);
        }
    }
}
