// Created with MonoDevelop
//
//    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.ApoIdentCore.GuiControl;
using Hiperscan.ApoIdentCore.Misc;
using Hiperscan.ApoIdentCore.SectionManager.Substance;
using Hiperscan.ApoIdentCore.SectionManager.Result;
using Hiperscan.GuiExtensions.Dialog;
using Hiperscan.AnalysisModule;
using Hiperscan.Unix;

namespace Hiperscan.ApoIdentCore
{
    public partial class AppControl
    {
        private void InitDocumentManager()
        {
            if (this.DocumentManager == null)
            {
                this.DocumentManager = new DocumentManager(this.Locale, 
                                                           this.SubstanceManager,
                                                           this.FileSystem);

                this.DocumentManager.ErrorOccurred += this.OnDocumentOpenErrorOccurred;
                this.DocumentManager.PreShowSuggestion += this.OnPreShowSuggestion;
            }
        }

        public void ShowPdf(string file_name)
        {
            this.ProgramSync.EnterCriticialZone();
            this.DocumentManager.ShowPDF(file_name);
            this.ProgramSync.LeaveCriticalZone();
        }

        public void OpenSupportUrl(UriReferrerId id)
        {
            this.ProgramSync.EnterCriticialZone();
            this.DocumentManager.OpenUri(id);
            this.ProgramSync.LeaveCriticalZone();
        }

        public void ShowSubstancePdf(SubstancePdfs.PdfListType type)
        {
            this.ProgramSync.EnterCriticialZone();
            this.DocumentManager.ShowSubstancePdf(type);
            this.ProgramSync.LeaveCriticalZone();
        }

        public void ShowAdditionalTestPdf(AnalysisType analysis_type, string model_name = null)
        {
            this.ProgramSync.EnterCriticialZone();
            this.DocumentManager.ShowAdditionalTests(analysis_type, model_name);
            this.ProgramSync.LeaveCriticalZone();
        }

        public void ShowValidationDocument(AnalysisType analysis_type, string model_class)
        {
            this.ProgramSync.EnterCriticialZone();
            this.DocumentManager.ShowValidationPdf(analysis_type, model_class);
            this.ProgramSync.LeaveCriticalZone();
        }

        public UpdateArgs ShowUpdateCertificate(AnalysisType analysis_type)
        {
            UpdateArgs arg = null;

            if (this.DocumentManager.ShowUpadteCertificate(analysis_type) == false)
            {
                arg = new UpdateArgs();
                arg.DialogArgs.StandardDialogType = StandardDialogType.ErrorWithServiceMessage;
                arg.DialogArgs.Message = this.DocumentManager.ErrorMessage;
                arg.DialogArgs.Exception = this.DocumentManager.Exception;
            }

            return arg;
        }

        public string GetLicenseText(AnalysisType analysis_type)
        {
            string text = String.Empty;
            try
            {
                string file = this.FileSystem.GetLicenseFile(analysis_type);
                text = FileIO.ReadFileToEnd(file).Trim();
            }
            catch (Exception)
            {
                text = String.Empty;
            }

            return text;
        }

        public void ShowSuggestion()
        {
            this.ProgramSync.EnterCriticialZone();
            this.DocumentManager.ShowSuggestionPdf();
            this.ProgramSync.LeaveCriticalZone();
        }

        private void OnPreShowSuggestion(object sender, EventArgs e)
        {
            UpdateArgs arg = new UpdateArgs();
            arg.DialogArgs.StandardDialogType = StandardDialogType.Info;
            arg.DialogArgs.Message = ResultManager.SUGGESTION_INFO;
            arg.DialogArgs.UseMarkup = true;

            this.UpdateGuiNeeded?.Invoke(arg);
        }

        private void OnDocumentOpenErrorOccurred(object sender, EventArgs e)
        {
            UpdateArgs arg = new UpdateArgs();
            arg.DialogArgs.StandardDialogType = StandardDialogType.ErrorWithServiceMessage;
            arg.DialogArgs.Message = (sender as DocumentManager).ErrorMessage;
            arg.DialogArgs.Exception = (sender as DocumentManager).Exception;

            this.UpdateGuiNeeded?.Invoke(arg);
        }

        public DocumentManager DocumentManager { get; private set; }
    }
}
