// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 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.Collections.Generic;
using System.Linq;
using Hiperscan.AnalysisModule;
using Hiperscan.AnalysisModule.Authentification;
using Hiperscan.AnalysisModule.Identification;

namespace Hiperscan.ApoIdentCore.SectionManager.Substance.SubstanceSelection
{
    public class SubstanceResolver
    {
        public void Clear()
        {
            this.SubstanceNamesCache.Clear();
            this.ModelClasses.Clear();
        }

        public void AddModelClass(IdentModelClass model_class)
        {
            this.ModelClasses.Add(model_class);
            this.SubstanceNamesCache.Clear();
        }

        public void RemoveUserdefinedModelClass()
        {
            IdentModelClass user_defined_model_class = this.ModelClasses.
                                                          FirstOrDefault(x => x.DatabaseInfo.DatabaseType == AccessDatabaseType.UserDefined);

            if (user_defined_model_class != null)
            {
                this.ModelClasses.Remove(user_defined_model_class);
                this.SubstanceNamesCache.Clear();
            }
        }

        private List<IdentClassification> GetClassifications(string substance_name)
        {
            return this.ModelClasses.
                       Where(x => x.GetSubstanceBySubstanceName(substance_name) != null).
                       Where(x => x.GetSubstanceBySubstanceName(substance_name).IsVisible).
                       Select(x => x.GetSubstanceBySubstanceName(substance_name).Classification).
                       ToList();
        }

        public List<IdentSubstance> GetSubstanceInfosByName(string substance_name)
        {
            return this.ModelClasses.
                       Where(x => x.GetSubstanceBySubstanceName(substance_name) != null).
                       Where(x => x.GetSubstanceBySubstanceName(substance_name).IsVisible).
                       Select(x => x.GetSubstanceBySubstanceName(substance_name)).
                       ToList();
        }

        public List<IdentSubstance> GetSubstanceInfosById(string substance_id)
        {           
            return this.ModelClasses.
                       Where(x => x.GetSubstanceBySubstanceId(substance_id) != null).
                       Where(x => x.GetSubstanceBySubstanceId(substance_id).IsVisible).
                       Select(x => x.GetSubstanceBySubstanceId(substance_id)).
                       ToList();
        }

        public bool IsQuantifierSubstance(string substance_name)
        {
            return this.ModelClasses.Where(m => m.DatabaseInfo.AnyalysisType == AnalysisModule.AnalysisType.Quantification).
                       Any(m => m.GetSubstanceBySubstanceName(substance_name) != null);
        }

        public List<IdentModelClass> AllModelClasses
        {
            get
            {
                return this.ModelClasses;
            }
        }

        public List<IdentModelClass> MasterModelClasses
        {
            get
            {
                return this.ModelClasses.
                           Where(x => x.DatabaseInfo.DatabaseType == AccessDatabaseType.Master).
                           ToList();
            }
        }

        public List<SubstanceListAutoCompletionEntry> AutoCompletionEntries
        {
            get
            {
                List<SubstanceListAutoCompletionEntry> values = new List<SubstanceListAutoCompletionEntry>();

                foreach (string substance_name in this.AllSubstanceNames)
                {
                    List<IdentClassification> tmp = this.GetClassifications(substance_name);
                    AnalysisType analysis_type = AnalysisType.None;
                    if (this.IsQuantifierSubstance(substance_name))
                        analysis_type = AnalysisType.Quantification;

                    SubstanceListAutoCompletionEntry entry = new SubstanceListAutoCompletionEntry(substance_name, tmp, analysis_type);
                    values.Add(entry);
                }

                values.Sort();
                return values;
            }
        }

        public HashSet<string> AllSubstanceNames
        {
            get
            {
                if (this.SubstanceNamesCache.Count == 0)
                {
                    this.SubstanceNamesCache = this.ModelClasses.
                                    SelectMany(x => x.Classifications).
                                    SelectMany(x => x.Substances).
                                    Where(x => x.IsVisible).
                                    SelectMany(x => x.AllSubstanceNames).
                                    Distinct().
                                    ToHashSet();
                }
                return this.SubstanceNamesCache;
            }
        }

        private IdentModelClass not_measurable_model_class = null;
        public IdentModelClass NotMeasurableModelClass
        {
            get
            {
                return this.not_measurable_model_class?.Clone();
            }
            private set
            {
                this.not_measurable_model_class = value;
            }
        }

        private HashSet<string> SubstanceNamesCache { get; set; } = new HashSet<string>();
        private List<IdentModelClass> ModelClasses = new List<IdentModelClass>();
    }
}
