using System;
using System.Collections.Generic;
using System.Xml;
using Hiperscan.AnalysisModule;
using Hiperscan.AnalysisModule.Authentification;
using Hiperscan.AnalysisModule.Identification;
using Hiperscan.Spectroscopy.Method;
using static Hiperscan.Common.I18n.CatalogLocale;

namespace Hiperscan.ApoIdentCommon.ApoConfig.Cache
{
    public partial class ModelExtension
    {
        private IdentModelClass GetLegacyModelClass(string file)
        {
            this.LegacyXmlDocument = new XmlDocument();
            this.LegacyXmlDocument.Load(file);
            return this.ReadLegacy();
        }

        private IdentModelClass ReadLegacy()
        {
            XmlNode model_class_node = this.LegacyXmlDocument.GetElementsByTagName("ModelClassExtensionInfo")[0];

            IdentModelClass model_class = new IdentModelClass();
            model_class.Id = "NO_ID";
            model_class.Name = model_class_node["ModelClassName"].InnerText;
            model_class.SubstanceType = SubstanceType.NotMeasurable;
            model_class.HasValidationDocument = false;
            model_class.DefaultSupplierName = String.Empty;
            model_class.AccessDefinitionName = String.Empty;

            this.CreateClassifications(model_class);
            return model_class;
        }
        
        private void CreateClassifications(IdentModelClass model_class)
        {
            XmlNodeList classification_nodes = this.LegacyXmlDocument.GetElementsByTagName("ClassificationInfo");
            foreach (XmlNode classification_node in classification_nodes)
            {
                IdentClassification classification = new IdentClassification();
                classification.Id = classification_node["Id"].InnerText;
                classification.Name = classification_node["ClassificationName"].InnerText;
                classification.ClassificationType = ClassificationType.NotMeasurable;
                bool is_user_defined = Boolean.Parse(classification_node["IsUserDefined"].InnerText);
                if (is_user_defined)
                    classification.ClassificationType = ClassificationType.UserDefined;

                this.CreateSubstances(classification, classification_node);
                model_class.Classifications.Add(classification);
            }
        }

        private void CreateSubstances(IdentClassification classification, XmlNode classification_node)
        {
            XmlNode substances_node = classification_node["Substances"];
            foreach (XmlNode substance_node in substances_node.ChildNodes)
            {
                IdentSubstance substance = new IdentSubstance();
                substance.Id = substance_node["Id"].InnerText;
                substance.PrimaryDefaultName = substance_node["PrimaryDefaultName"].InnerText;
                substance.PrimaryLatinName = substance_node["PrimaryLatinName"].InnerText;
                substance.AdditionalSubstanceInfo = String.Empty;
                substance.PdfLinkAdditionalTest = String.Empty;
                substance.CanInsert = false;
                substance.IsVisible = Boolean.Parse(substance_node["IsVisible"].InnerText);
                substance.OtherDefaultNames = this.GetNames(substance_node["OtherDefaultNames"]);
                substance.OtherLatinNames = this.GetNames(substance_node["OtherLatinNames"]);

                substance.BAKMarking = BAKMarkingState.None;
                string bak_marking = substance_node["BAKMarking"].InnerText;
                if (String.IsNullOrEmpty(bak_marking) == false)
                    substance.BAKMarking = (BAKMarkingState)Enum.Parse(typeof(BAKMarkingState), bak_marking);

                classification.Substances.Add(substance);
            }
        }

        private List<string> GetNames(XmlNode node)
        {
            List<string> names = new List<string>();

            foreach (XmlNode name_node in node.ChildNodes)
                names.Add(name_node.InnerText);

            return names;
        }

        private XmlDocument LegacyXmlDocument { get; set; }
    }
}
