using System;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
namespace HearingAidLab
{
	/// <summary>
	/// Summary description for HearingAidConfig.
	/// </summary>
	public class HearingAidConfig
	{
		public HearingAidConfig()
		{
			//
			// TODO: Add constructor logic here
			//
		}
	}
	public class ApplicationSettings
	{
		private bool appSettingsChanged;
		// Variables used to store the application settings.
		private string m_Mic;
		private string m_Level;
		private string m_MaxIn;
		private string m_MaxInt;
		private string m_MaxOut;

		// Properties used to access the application settings variables.
		public string Mic 
		{
			get{return m_Mic;}
			set
			{
				if(value != m_Mic)
				{
					m_Mic = value;
					appSettingsChanged = true;
				}
			}
		}

		public string Level
		{
			get{return m_Level;}
			set
			{
				if(value != m_Level)
				{
					m_Level = value;
					appSettingsChanged = true;
				}
			}
		}

		public string MaxIn 
		{
			get{return m_MaxIn;}
			set
			{
				if(value != m_MaxIn)
				{
					m_MaxIn = value;
					appSettingsChanged = true;
				}
			}
		}
		public string MaxInt 
		{
			get{return m_MaxInt;}
			set
			{
				if(value != m_MaxInt)
				{
					m_MaxInt = value;
					appSettingsChanged = true;
				}
			}
		}
		public string MaxOut 
		{
			get{return m_MaxOut;}
			set
			{
				if(value != m_MaxOut)
				{
					m_MaxOut = value;
					appSettingsChanged = true;
				}
			}
		}
	
	
		// Serializes the class to the config file
		// if any of the settings have changed.
		public bool SaveAppSettings()
		{
			if(this.appSettingsChanged)
			{
				StreamWriter myWriter = null;
				XmlSerializer mySerializer = null;
				try
				{
					// Create an XmlSerializer for the 
					// ApplicationSettings type.
					mySerializer = new XmlSerializer( 
						typeof(ApplicationSettings));
					myWriter = 
						new StreamWriter(Application.LocalUserAppDataPath
						+ @"\HearingAid.config",false);
					// Serialize this instance of the ApplicationSettings 
					// class to the config file.
					mySerializer.Serialize(myWriter, this);
				}
				catch(Exception ex)
				{
					MessageBox.Show(ex.Message); 
				}
				finally
				{
					// If the FileStream is open, close it.
					if(myWriter != null)
					{
						myWriter.Close();
					}
				}
			}
			return appSettingsChanged;
		}
	

		// Deserializes the class from the config file.
		public bool LoadAppSettings()
		{
			XmlSerializer mySerializer = null;
			FileStream myFileStream = null;
			bool fileExists = false;

			try
			{
				// Create an XmlSerializer for the ApplicationSettings type.
				mySerializer = new XmlSerializer(typeof(ApplicationSettings));
				FileInfo fi = new FileInfo(Application.LocalUserAppDataPath
					+ @"\HearingAid.config");
				// If the config file exists, open it.
				if(fi.Exists)
				{
					myFileStream = fi.OpenRead();
					// Create a new instance of the ApplicationSettings by
					// deserializing the config file.
					ApplicationSettings myAppSettings = 
						(ApplicationSettings)mySerializer.Deserialize(
						myFileStream);
					// Assign the property values to this instance of 
					// the ApplicationSettings class.
					this.m_Mic = myAppSettings.Mic;
					this.m_Level = myAppSettings.Level;
					this.m_MaxIn = myAppSettings.MaxIn;
					this.m_MaxInt= myAppSettings.MaxInt;
					this.m_MaxOut = myAppSettings.MaxOut;
					fileExists = true;
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			finally
			{
				// If the FileStream is open, close it.
				if(myFileStream != null)
				{
					myFileStream.Close();
				}
			}

			
			return fileExists;
		}

	}

}
