using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace XMLSetting
{
    public partial class Form1 : Form
    {
        // Create a global variable containing the name and
        // location of the settings file.
        String SettingsFile;

        public Form1()
        {
            // Output a status message.
            System.Diagnostics.Debug.WriteLine("Entering the Constructor");
            
            InitializeComponent();

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

            // Check for a settings file.
            if (File.Exists(SettingsFile))
            {
                // Indent the output.
                System.Diagnostics.Debug.IndentLevel++;

                // Output a status message.
                System.Diagnostics.Debug.WriteLine(File.Exists(SettingsFile), "The File Exists");
                System.Diagnostics.Debug.Print("The File Exists");

                // Load the file containing the settings.
                XDocument SettingData = XDocument.Load(SettingsFile);

                // Make sure the document actually contains some data.
                System.Diagnostics.Debug.Assert(
                    SettingData.Document != null, "The document is empty.");

                // Output the SettingData content.
                System.Diagnostics.Debug.WriteLine(
                    "\r\n" + SettingData.Document, "SettingData.Document");

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

            // Outdent the output.
            System.Diagnostics.Debug.IndentLevel--;

            // Output a status message.
            System.Diagnostics.Debug.WriteLine("Leaving the Constructor");
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            // Display a status message.
            System.Diagnostics.Debug.WriteLine("The User Clicked Quit");

            // End the program.
            Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Display a status message.
            System.Diagnostics.Debug.WriteLine("Starting to Save the Settings");

            // Display a supplementary message.
            System.Diagnostics.Debug.IndentLevel++;
            System.Diagnostics.Debug.WriteLineIf(
                chkChecked.Checked, "Detected that chkChecked is Checked.");
            System.Diagnostics.Debug.IndentLevel--;

            // 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.Checked)));

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

            // Display a status message.
            System.Diagnostics.Debug.WriteLine("Settings Are Saved");
        }
    }
}
