// Created with MonoDevelop
//
//    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.Collections.Generic;
using System.Linq;
using Hiperscan.AnalysisModule.Authentification;
using Hiperscan.AnalysisModule.Quantification;
using Hiperscan.Common;
using Hiperscan.Common.RegistryManagement;
using Hiperscan.Spectroscopy;
using static Hiperscan.Common.I18n.CatalogLocale;

namespace Hiperscan.AnalysisModule.Remoting
{
    public class QuantifierModuleConnector : RemoteConnector
    {
        public QuantifierModuleConnector(string mono_exe,
                                    string address = "localhost",
                                    int port = QuantifierModuleClient.PORT)
                                    : base(address, port, mono_exe)
        {
        }

        public byte[] GetAdditionalTestPdf(IModelClass model_class)
        {
            this.UpdateAuthentificationSet(model_class);
            return this.Client.Service.GetAdditionalTestPdf(this.Authentification);
        }

        protected override RemoteClient CreateInstance()
        {
            return new QuantifierModuleClient(this.Address, this.Port);
        }

        public override void Analyse(Spectrum spectrum)
        {
            try
            {
                this.Result = this.Client.Service.Quantify(this.Authentification, spectrum);
            }
            catch (Exception ex)
            {
                this.DiscartResult();
                throw new Exception(ex.Message, ex);
            }
        }

        public override void DiscartResult()
        {
            this.Result = null;
        }

        public List<QuantifierModelClass> ModelClasses
        {
            get
            {
                return this.LoadedModelClasses?.Select(m => (QuantifierModelClass)m).ToList();
            }
        }

        public QuantifierModuleClient Client
        {
            get
            {
                return (QuantifierModuleClient)this.RemoteClient;
            }
        }

        public override bool HasValidResult
        {
            get
            {
                return this.Result != null;
            }
        }

        public PlsResultSet Result { get; private set; }

        public override bool IsDebug => Environment.GetEnvironmentVariable(Env.QuantifierModule.DEBUG) == "1";
        protected override string UnixCommand => DefaultNames.QuantifierServer.UNIX_COMMAND;
        protected override HSRegistryEntry ModuleRegistryEntry => MSRegistry.Create().QuantifierModule;
        protected override string ProductName => DefaultNames.QuantifierServer.PRODUCTNAME;
    }
}
