using System;
using System.Net;
using System.IO;

namespace WheelchairControlWM
{
    /// <summary>
    /// Class to store settings to smartphone's flash.
    /// </summary>
    [Serializable]
    public class SettingsClass
    {
        #region Constants

        /// <summary>
        /// Settings file path.
        /// </summary>
        private const String DEFAULT_CONFIG_FILE_ADDRESS = @"\WheelchairControlSettings.ini";

        #endregion

        #region Private variables

        /// <summary>
        /// Ip address of target.
        /// </summary>
        private IPAddress targetIp = null;

        /// <summary>
        /// Trackbar gain last saved value;
        /// </summary>
        private Byte gain = 0;

        /// <summary>
        /// Checkbox reset idle timer check state.
        /// </summary>
        private Boolean resetIdleTimer = true;

        /// <summary>
        /// Checkbox enable remote start check state.
        /// </summary>
        private Boolean enableRemoteStart = true;

        #endregion

        #region Properties

        /// <summary>
        /// Target IP setting.
        /// </summary>
        public IPAddress TargetIP
        {
            get { return targetIp; }
            set { targetIp = value; }
        }

        /// <summary>
        /// Gain setting.
        /// </summary>
        public Byte Gain
        {
            get { return gain; }
            set { gain = value; }
        }

        /// <summary>
        /// Reset idle timer setting.
        /// </summary>
        public Boolean ResetIdleTimerEnabled
        {
            get { return resetIdleTimer; }
            set { resetIdleTimer = value; }
        }

        /// <summary>
        /// Remote start enabled setting.
        /// </summary>
        public Boolean RemoteStartEnabled
        {
            get { return enableRemoteStart; }
            set { enableRemoteStart = value; }
        }

        #endregion

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <remarks>Reads configuration from default file.</remarks>
        /// <see cref="DEFAULT_CONFIG_FILE_ADDRESS"/>
        public SettingsClass()
        {
            StreamReader reader = null;

            try
            {
                reader = new StreamReader(DEFAULT_CONFIG_FILE_ADDRESS);

                //Read IP
                targetIp = IPAddress.Parse(reader.ReadLine());

                //Read gain
                gain = Byte.Parse(reader.ReadLine());

                //Read idle timer enable status
                resetIdleTimer = Boolean.Parse(reader.ReadLine());

                //Read remote start enable status
                enableRemoteStart = Boolean.Parse(reader.ReadLine());
            }
            catch (Exception)
            {
                //Use ActiveSync default address
                if(targetIp == null)
                    targetIp = IPAddress.Parse("169.254.2.0");

                //Other values assigned during initialization
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

        /// <summary>
        /// Save settings to file.
        /// </summary>
        /// <see cref="DEFAULT_CONFIG_FILE_ADDRESS"/>
        public void Save()
        {
            StreamWriter writer = null;

            try
            {
                //New file writer, always overwrites old file!
                writer = new StreamWriter(DEFAULT_CONFIG_FILE_ADDRESS, false);

                //Write IP
                writer.WriteLine(targetIp.ToString());

                //Write gain
                writer.WriteLine(gain.ToString());

                //Write idle timer enable status
                writer.WriteLine(resetIdleTimer.ToString());

                //Write remote start enable status
                writer.WriteLine(enableRemoteStart.ToString());
            }
            catch (Exception)
            {
                //Pass exception
                throw;
            }
            finally
            {
                if(writer != null)
                    writer.Close();
            }
        }
    }
}