// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2017 Thomas Klose & Torsten Schache, Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;

using Hiperscan.Common.I18n;


namespace Hiperscan.Extensions.ApoIdent
{
    public partial class Configurations
    {
        public class SoftwareUpgradePreferences : Preferences<SoftwareUpgradePreferences>
        {
            public const int DEFAULT_PORT = 49152;

            public const string SOFTWARE_UPGRADE_SETTINGS_TAG = "SoftwareUpgradeSettings";
            private const string SEARCH_AUTOMATICALLY_TAG = "SearchAutomatically";
            private const string DELAY_TAG = "Delay";

            public SoftwareUpgradePreferences() : base()
            {

            }

            public SoftwareUpgradePreferences(XmlNode software_settings_node) : base(software_settings_node)
            {

            }

            protected override void Init(XmlNode parent_node = null)
            {
                this.SearchAutomatically = false;
                this.DelayInDays = 14;
                base.Init(parent_node);
            }

            protected override void Read(XmlNode parent_node)
            {
                try
                {
                    XmlNode automatic_node = parent_node.SelectNodes(SoftwareUpgradePreferences.SEARCH_AUTOMATICALLY_TAG)[0];
                    this.SearchAutomatically = Boolean.Parse(automatic_node.InnerText);
                }
                catch (Exception) { }

                try
                {
                    XmlNode delay_node = parent_node.SelectNodes(SoftwareUpgradePreferences.DELAY_TAG)[0];
                    this.DelayInDays = Int32.Parse(delay_node.InnerText);
                    if (this.DelayInDays < 14)
                        this.DelayInDays = 14;
                }
                catch (Exception) { }
            }

            public override void WriteToXml(XmlDocument doc, XmlNode parent)
            {
                XmlNode software_settings_node = doc.CreateElement(SoftwareUpgradePreferences.SOFTWARE_UPGRADE_SETTINGS_TAG);
                parent.AppendChild(software_settings_node);

                XmlNode automatic_node = doc.CreateElement(SoftwareUpgradePreferences.SEARCH_AUTOMATICALLY_TAG);
                automatic_node.InnerText = this.SearchAutomatically.ToString();
                software_settings_node.AppendChild(automatic_node);

                XmlNode delay_node = doc.CreateElement(SoftwareUpgradePreferences.DELAY_TAG);
                delay_node.InnerText = this.DelayInDays.ToString();
                software_settings_node.AppendChild(delay_node);
            }

            public override SoftwareUpgradePreferences Clone()
            {
                SoftwareUpgradePreferences tmp = new SoftwareUpgradePreferences();
                tmp.SearchAutomatically = this.SearchAutomatically;
                tmp.DelayInDays = this.DelayInDays;

                return tmp;
            }

            protected override bool SettingsChanged()
            {
                if (this.Stashed == null)
                    return false;

                if (this.Stashed.SearchAutomatically != this.SearchAutomatically)
                    return true;

                if (this.Stashed.DelayInDays != this.DelayInDays)
                    return true;

                return false;
            }

            public bool SearchAutomatically { get; set; }
            public int DelayInDays { get; set; }
        }
    }
}

