// Created with MonoDevelop
//
//    Copyright (C) 2017 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.IO;
using System.Runtime.Serialization;
using System.Xml;


namespace Hiperscan.Spectroscopy.IdentModule
{
    [Serializable()]
    [DataContract(Name = "ModelClassInfo", Namespace = "http://www.hiperscan.com/spectroscopy/identmodule")]
    public class ModelClassInfo
    {
        private class SubstanceNameInfo
        {
            public ClassificationInfo ClassificationInfo { get; set; }
            public SubstanceInfo      SubstanceInfo      { get; set; }
        }

        public ModelClassInfo(string model_class_name)
        {
            this.ModelClassName = model_class_name;
            this.IsFluid = false;
            this.ValidationDocument = false;
            this.DefaultSupplierName = String.Empty;
            this.Classifications = new List<ClassificationInfo>();
            this.SubstanceNameResolver = new Dictionary<string,SubstanceNameInfo>();
        }

        public void InitResolver()
        {
            this.SubstanceNameResolver = new Dictionary<string,SubstanceNameInfo>();

            foreach (ClassificationInfo classification_info in this.Classifications)
            {
                foreach (SubstanceInfo substance_info in classification_info.Substances)
                {
                    if (substance_info.IsVisible == false)
                        continue;
                    
                    foreach (string name in substance_info.AllNames)
                    {
                        SubstanceNameInfo info = new SubstanceNameInfo();
                        info.ClassificationInfo = classification_info;
                        info.SubstanceInfo = substance_info;
                        this.SubstanceNameResolver.Add(name, info);
                    }
                }
            }
        }

        public ClassificationInfo GetClassificationInfo(string substance_name)
        {
            return this.SubstanceNameResolver[substance_name].ClassificationInfo;
        }

        public SubstanceInfo GetSubstanceInfo(string substance_name)
        {
            return this.SubstanceNameResolver[substance_name].SubstanceInfo;
        }

        [DataMember] public string ModelClassName { get; set; }
        [DataMember] public bool IsFluid { get; set; }
        [DataMember] public bool ValidationDocument { get; set; }
        [DataMember] public string DefaultSupplierName { get; set; }
        [DataMember] public List<ClassificationInfo> Classifications { get; set; }

        [NonSerialized]
        public AccessDatabaseInfo DatabaseInfo = null;

        [NonSerialized]
        private bool? is_insert_model_class = null;
        public bool IsInsertModelClass
        {
            get
            {
                //TODO #31922
                return false;

                if (is_insert_model_class == null)
                {
                    is_insert_model_class = true;
                    foreach (ClassificationInfo classification_info in this.Classifications)
                    {
                        foreach (SubstanceInfo substance_info in classification_info.Substances)
                        {
                            if (substance_info.CanInsert == false)
                            {
                                this.is_insert_model_class = false;
                                break;
                            }
                        }

                        if (this.is_insert_model_class == false)
                            break;
                    }
                }

                return is_insert_model_class ?? false;
            }
        }


        [NonSerialized][IgnoreDataMember]
        private Dictionary<string,SubstanceNameInfo> SubstanceNameResolver = null;
    }
}