// 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.Xml;

namespace Hiperscan.ApoIdentCommon.ApoConfig
{
    public class SoftwareUpgradeSettings : SettingsTemplate<SoftwareUpgradeSettings>
    {
        public const int DEFAULT_PORT = 49152;

        public const string SOFTWARE_UPGRADE_SETTINGS_TAG = "SoftwareUpgradeSettings";
        private const string SEARCH_AUTOMATICALLY_TAG = "SearchAutomatically";
        private const string SEARCH_IM_TAG = "SearchIM";
        private const string SEARCH_QM_TAG = "SearchQM";
        private const string DELAY_TAG = "Delay";

        public SoftwareUpgradeSettings() : base()
        {

        }

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

        }

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

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

            try
            {
                XmlNode delay_node = parent_node.SelectNodes(SoftwareUpgradeSettings.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(SoftwareUpgradeSettings.SOFTWARE_UPGRADE_SETTINGS_TAG);
            parent.AppendChild(software_settings_node);

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

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

        public override SoftwareUpgradeSettings Clone()
        {
            SoftwareUpgradeSettings tmp = new SoftwareUpgradeSettings();
            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; }
        public bool CanUpgrade { get; set; }
    }
}

