//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2022, 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 Hiperscan.ApoIdentCommon.ApoConfig.Cache;
using Hiperscan.AnalysisModule.Identification;
using System.Linq;

namespace Hiperscan.ApoIdentCommon.ApoConfig
{
    public partial class ProfileSettings
    {
        public void LoadCacheData(bool directory_changed)
        {
            if (this.ArchiveMissing == true)
                return;

            try
            {
                this.BakMarking.LoadAutoCompletion(this.BakAutoCompletionFile, directory_changed);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Cannot read BakMarking: {0}", ex);
            }

            try
            {
                this.GracePeriod.LoadAutoCompletion(this.GracePeriodAutoCompletionFile, directory_changed);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot read GracePeriod: {0}", ex);
            }

            try
            {
                this.AdditionalTest.LoadAutoCompletion(this.AdditionalTestAutoCompletionFile, directory_changed);
                this.AdditionalTest.IsThirdPartyProtocol = this.ExchangeData.ProtocolType != ThirdPartyDataExchange.ProtocolType.ApoIdent;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot read AdditionalTest: {0}", ex);
            }

            try
            {
                this.ModelExtension.LoadExtension(this.ModelClassExtensionFile, directory_changed);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot read SubstanceExtension: {0}", ex);
            }
        }

        public void SaveCacheData()
        {
            if (this.ArchiveMissing == true)
                return;

            try
            {
                this.BakMarking.SaveAutoCompletion(this.BakAutoCompletionFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot write BakMarking: {0}", ex);
            }

            try
            {
                this.GracePeriod.SaveAutoCompletion(this.GracePeriodAutoCompletionFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot write GracePeriod: {0}", ex);
            }

            try
            {
                this.AdditionalTest.SaveAutoCompletion(this.AdditionalTestAutoCompletionFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot write AdditionalTest: {0}", ex);
            }
        }

        public void SaveModelExtension()
        {
            if (this.ArchiveMissing == true)
                return;

            try
            {
                this.ModelExtension.SaveExtension(this.ModelClassExtensionFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot write SubstanceExtension: {0}", ex);
            }
        }

        public void ResetAutpCompletionsInitialValues()
        {
            this.BakMarking.Reset();
            this.AdditionalTest.Reset();
            this.GracePeriod.Reset();
        }

        public void SetAutpCompletionsInitialValues(IdentSubstance substance)
        {
            this.BakMarking.SetInitialValue(substance, substance.BAKMarking);
            this.GracePeriod.SetInitialValue(substance, new GracePeriodEntry());
            this.AdditionalTest.SetInitialValue(substance, String.Empty);
        }

        public bool HasSubstances
        {
            get
            {
                return this.ModelExtension.ModelClass.Classifications.Count > 0;
            }
        }

        public bool HasVisibleSubstances => this.ModelExtension.ModelClass.Classifications.SelectMany(c => c.Substances).
                                               Any(s => s.IsVisible);


        public BakMarking BakMarking { get; private set; } = new BakMarking();
        public AdditionalTest AdditionalTest { get; private set; } = new AdditionalTest();
        public GracePeriod GracePeriod { get; private set; } = new GracePeriod();
        public ModelExtension ModelExtension { get; private set; } = new ModelExtension();
    }
}
