// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2018 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 Hiperscan.ApoIdentCore.SectionManager.Substance.Fields;
using Hiperscan.ApoIdentCore.SectionManager.Substance.SubstanceSelection;
using Hiperscan.ApoIdentCore.Misc;
using Hiperscan.ApoIdentCommon.ApoConfig;
using Hiperscan.ApoIdentCommon.ApoConfig.IO;
using Hiperscan.ApoIdentCore.Remoting;
using Hiperscan.AnalysisModule.Remoting;
using Hiperscan.AnalysisModule;
using Hiperscan.AnalysisModule.Identification;
using Hiperscan.AnalysisModule.Quantification;

namespace Hiperscan.ApoIdentCore.SectionManager.Substance
{
    public partial class SubstanceManager : ISectionManager
    {
        public SubstanceManager(RemoteManager remote_manager, 
                                ApoIdentFileSystem file_system,
                               GlobalStates global_states)
        {
            this.RemoteManager = remote_manager;
            this.ApoIdentVersion = this.GetVersion(ApoIdentInfo.QSAssemblyVersion);

            this.RemoteManager.LoadingProgressed += this.OnLoadingDataProgressed;
            this.SubstancesCatalog = new SubstancesCatalog();
            this.Pdfs = new SubstancePdfs(this, file_system);

            this.GlobalStates = global_states;
            this.ResetAvailableModules();
        }

        public void OnLoadingDataProgressed(object sender, EventArgs e)
        {
            RemoteConnector connector = sender as RemoteConnector;

            IdentModelClass model_class = connector.CurrentModelClass as IdentModelClass;
            if (model_class == null)
                model_class = ((QuantifierModelClass)connector.CurrentModelClass).Convert();

            this.SubstancesCatalog.AddModelClass(model_class);
        }

        public void LoadRemotePdfs(AnalysisType analysis_type, bool is_es20)
        {
            this.IsUpdatingDocuments = true;

            this.Pdfs.Clean();

            bool is_available = this.GlobalStates.IsIdentModuleAvailable;
            if (analysis_type == AnalysisType.Quantification)
                is_available = this.GlobalStates.IsQuantifierModuleAvailable;

            if (this.Pdfs.CheckForPdfUpdates(analysis_type, is_es20, is_available))
            {
                this.Pdfs.UpdateSubstancePdfs(analysis_type);
                this.Pdfs.UpdateValidationDocuments(analysis_type);
                this.Pdfs.UpdateAdditionalTestsPdfs(analysis_type);
                this.Pdfs.UpdateCertificate(analysis_type);
                this.Pdfs.UpdateLicenseText(analysis_type);
            }
            this.IsUpdatingDocuments = false;
        }

        public void UpdateAfterSubstanceSelection(ProfileManager profile_manager)
        {
            this.BakMarking.FireEvent = false;
            this.BakMarking.Entry = profile_manager.CurrentProfile.BakMarking.CurrentValue;
            this.BakMarking.FireEvent = true;
            this.RemoteManager.UpdateAuthentificationSet(this.SubstancesCatalog.ModelClass,
                                                         SubstancesCatalog.Substance.Id);
        }

        public void Clear()
        {
            this.Reset();
            this.SubstancesCatalog.Clear();
        }

        public void Reset()
        {
            this.BakMarking.FireEvent = false;
            this.BakMarking.SetDefaultValue();
            this.BakMarking.FireEvent = true;
            this.SubstancesCatalog.Reset();
        }

        public Dictionary<string, string> CheckUniqueSubstanceName(string cleaned_substance_name)
        {
            Dictionary<string, string> values = new Dictionary<string, string>();

            foreach (string substance_name in this.SubstancesCatalog.AllSubstanceNames)
            {
                if (substance_name.xCleanForSubstanceSearch() == cleaned_substance_name)
                {
                    foreach (IdentModelClass model_class in this.SubstancesCatalog.AllModelClasses)
                    {
                        IdentSubstance substance = model_class.GetSubstanceBySubstanceName(substance_name);

                        if (substance != null)
                            values.Add(model_class.Name, substance.PrimaryDefaultName);
                    }
                }
            }

            return values;
        }

        public bool IsComplete
        {
            get
            {
                return this.SubstancesCatalog.IsSubstanceSelected;
            }
        }

        public BakMarkingField BakMarking { get; private set; } = new BakMarkingField();

        public SubstancePdfs Pdfs { get; private set; }
        public SubstancesCatalog SubstancesCatalog { get; private set; }
        public bool IsUpdatingDocuments { get; private set; }
    }
}
