// 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.ApoIdentCommon.ApoConfig.Enums;
using Hiperscan.Common.I18n;
using Hiperscan.AnalysisModule.Identification;

namespace Hiperscan.ApoIdentCommon.ApoConfig
{
    public class IdentModuleSettings : RemoteSettings
    {
        public static readonly string ACQUISITION_SETTINGS_TAG = "AcquisitionSettings";
        private static readonly string IDENTIFICATION_MODE_TAG = "IdentificationMode";
        private static readonly string IDENT_SERVER_VERSION_TAG = "IdentServerVersion";
        private static readonly string IDENT_SERVER_SUFFIX_TAG = "IdentServerSuffix";

        public IdentModuleSettings() : base()
        {
        }

        public IdentModuleSettings(XmlNode parent_node) : base(parent_node)
        {
        }

        protected override void Read(XmlNode acquisition_settings_node)
        {
            try
            {
                XmlNode identification_mode_node = acquisition_settings_node.SelectNodes(IdentModuleSettings.IDENTIFICATION_MODE_TAG)[0];
                this.ModuleLocation = (RemoteModuleLocationTypes)Enum.Parse(typeof(RemoteModuleLocationTypes), identification_mode_node.InnerText, true);
            }
            catch (Exception) { }

            try
            {
                XmlNode server_node = acquisition_settings_node.SelectNodes(IdentModuleSettings.IDENT_SERVER_VERSION_TAG)[0];
                this.ModuleVersion = server_node.InnerText;
            }
            catch (Exception) { }

            try
            {
                XmlNode suffix_node = acquisition_settings_node.SelectNodes(IdentModuleSettings.IDENT_SERVER_SUFFIX_TAG)[0];
                this.ServerSuffix = suffix_node.InnerText;
            }
            catch (Exception) { }

            base.Read(acquisition_settings_node);
        }

        public override void WriteToXml(XmlDocument doc, XmlNode parent)
        {
            XmlNode acquisition_settings_node_node = doc.CreateElement(IdentModuleSettings.ACQUISITION_SETTINGS_TAG);
            parent.AppendChild(acquisition_settings_node_node);

            parent = acquisition_settings_node_node;

            XmlNode remote_mode_node = doc.CreateElement(IdentModuleSettings.IDENTIFICATION_MODE_TAG);
            remote_mode_node.InnerText = this.ModuleLocation.ToString();
            parent.AppendChild(remote_mode_node);

            XmlNode server_node = doc.CreateElement(IdentModuleSettings.IDENT_SERVER_VERSION_TAG);
            server_node.InnerText = this.ModuleVersion;
            parent.AppendChild(server_node);

            XmlNode suffix_node = doc.CreateElement(IdentModuleSettings.IDENT_SERVER_SUFFIX_TAG);
            suffix_node.InnerText = this.ServerSuffix;
            parent.AppendChild(suffix_node);

            base.WriteToXml(doc, parent);
        }

        protected override RemoteSettings GetInstance()
        {
            return new IdentModuleSettings();
        }

        public override int RemotePort
        {  
            get
            {
                if (this.ModuleLocation == RemoteModuleLocationTypes.Local)
                    return IdentModuleClient.PORT;

                return this.TcpPort;
            }
        }
    }
}

