// 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.ApoIdentCommon.ApoConfig;
using Hiperscan.Unix;
using Hiperscan.UpdateCertificate;

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 ShowUpdateCertificatePdf(AnalysisType analysis_type)
        {
            RemoteSettings settings = this.SubstanceManager.GetRemoteSettings(analysis_type);

            if (settings.UpdateUpdateCertificate)
            {
                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 = UpdateCertificateDrawer.GetOutFilename(settings.ModuleVersion,
                                                                     settings.InstallDate,
                                                                     analysis_type,
                                                                     settings.Locale);

            file = this.FileSystem.GetUpdateCertficatFile(analysis_type, file);

            if (false == FileIO.ExistsFile(file))
                throw new Exception(Catalog.GetString("Cannot find the file."));

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

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

                string dir = this.FileSystem.GetUpdateCertificateDir(analysis_type);
                FileIO.RecreateDirectory(dir, this.NeedUpdatCertificate(analysis_type));

                RemoteSettings settings = this.SubstanceManager.GetRemoteSettings(analysis_type);
                string file = UpdateCertificateDrawer.GetOutFilename(settings.ModuleVersion,
                                                                     settings.InstallDate,
                                                                     analysis_type,
                                                                     settings.Locale);

                file = this.FileSystem.GetUpdateCertficatFile(analysis_type, file);
                if (this.NeedUpdatCertificate(analysis_type, file) == false)
                    return;

                FileIO.DeleteFile(file, true);

                UpdateCertificateDrawer.Run(settings.ModuleVersion, 
                                            settings.InstallDate, 
                                            analysis_type, 
                                            settings.Locale, 
                                            file,
                                            false);


                this.SubstanceManager.GetRemoteSettings(analysis_type).UpdateUpdateCertificate = false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("UpdateCertificate() failed: {0}", ex.ToString());
            }
        }

        public void UpdateLicenseText(AnalysisType analysis_type)
        {
            try
            {
                string dir = this.FileSystem.GetLicenseDir(analysis_type);
                FileIO.RecreateDirectory(dir, this.NeedUpdateLicense(analysis_type));

                string file = this.FileSystem.GetLicenseFile(analysis_type);
                if (this.NeedUpdateLicense(analysis_type, file) == false)
                    return;

                FileIO.DeleteFile(file, true);

                try
                {
                    byte[] stream = this.SubstanceManager.GetLicense(analysis_type);
                    using (FileStream fs = new FileStream(file, FileMode.CreateNew))
                    {
                        fs.Write(stream, 0, stream.Length);
                    }
                }
                catch { }

                this.SubstanceManager.GetRemoteSettings(analysis_type).UpdateLicenceText = false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("UpdateIMLicenseText() failed: {0}", ex.ToString());
            }
        }

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

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

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

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

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