// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2024, 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.IO;

using Hiperscan.AnalysisModule;
using Hiperscan.AnalysisModule.Identification;
using Hiperscan.ApoIdentCommon.ApoConfig;
using Hiperscan.Unix;

namespace Hiperscan.ApoIdentCore.SectionManager.Substance
{

#pragma warning disable RECS0022 // catch-Klausel, die System.Exception abfängt und keinen Text aufweist

    public partial class SubstancePdfs
    {
        public void ShowValidationPdf(AnalysisType analysis_type, string model_class_name)
        {
            RemoteSettings settings = this.SubstanceManager.GetRemoteSettings(analysis_type);

            if (settings.UpdateValidationPdfs)
            {
                string message = String.Format(Catalog.GetString("An error occurred while getting the document from the {0}."),
                                               analysis_type.GetProductName());

                throw new Exception(message);
            }

            string file = this.FileSystem.GetValidationFile(analysis_type, model_class_name);
            if (false == FileIO.ExistsFile(file))
                throw new Exception(Catalog.GetString("Cannot find the file."));

            Process.OpenUri("file://" + file.Replace("\\", "/"));
        }

        public void UpdateValidationDocuments(AnalysisType analysis_type)
        {
            try
            {
                Console.WriteLine("UpdateValidationDocument() for {0} ...", analysis_type.GetProductName());

                string dir = this.FileSystem.GetValidationDir(analysis_type);
                FileIO.RecreateDirectory(dir, this.NeedUpdateValidationDocuments(analysis_type));

                this.UpdateAllValidationDocument(analysis_type);
                this.SubstanceManager.GetRemoteSettings(analysis_type).UpdateValidationPdfs = false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("UpdateValidationDocument() for {0} failed: {1}",
                                  analysis_type.GetProductName(),
                                  ex);
            }
        }

        private void UpdateAllValidationDocument(AnalysisType analysis_type)
        {
            foreach (IdentModelClass model_class in this.SubstancesCatalog.ValidationDocuModelClasses)
            {

                if (model_class.DatabaseInfo.AnyalysisType != analysis_type)
                    continue;

                try
                {
                    string file = this.FileSystem.GetValidationFile(analysis_type, model_class.Name);
                    if (this.NeedUpdateValidationDocuments(analysis_type, file) == false)
                        continue;

                    FileIO.DeleteFile(file, true);

                    byte[] stream = this.SubstanceManager.GetValidationDocument(model_class);

                    using (FileStream fs = new FileStream(file, FileMode.CreateNew))
                    {
                        fs.Write(stream, 0, stream.Length);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Cannot read validation document {0}: {1}",
                                      model_class.Name,
                                      ex.Message);
                }
            }
        }

        private bool NeedUpdateValidationDocuments(AnalysisType analysis_type, string file = null)
        {
            if (String.IsNullOrEmpty(file) == false)
            {
                if (false == FileIO.ExistsFile(file))
                    return true;
            }

            return this.SubstanceManager.GetRemoteSettings(analysis_type).UpdateValidationPdfs;
        }
    }

#pragma warning restore RECS0022 // catch-Klausel, die System.Exception abfängt und keinen Text aufweist
}
