// 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 Hiperscan.Unix;
using Hiperscan.Spectroscopy;

using Hiperscan.Spectroscopy.Processing;

namespace Hiperscan.Spectroscopy.ReferenceChecks
{
    public partial class ReferenceChecks
    {
        public Fix2GlassWhiteReferenceCheckResult CheckFix2GlassWhiteReference(Spectrum spectrum, bool throw_exception = true)
        {
            double intensity_mean_threshold = ReferenceChecks.FIX2GLASS_WHITE_INTENSITY_MEAN_THRESHOLD;

            return this.CheckFix2GlassWhiteReference(spectrum, intensity_mean_threshold, throw_exception);
        }


        public Fix2GlassWhiteReferenceCheckResult CheckFix2GlassWhiteReference(Spectrum spectrum,
                                                                              double intensity_mean_threshold,
                                                                              bool throw_exception = true)
        {
            if (this.SpectrumProcessor.InstrumentReferenceSystem != InstrumentReferenceSystem.ReferenceBlock)
                throw new Exception(Catalog.GetString("Cannot check reference. Invalid reference type."));

            ReferenceBlockProcessor processor = (ReferenceBlockProcessor)this.SpectrumProcessor;

            double intensity_mean = spectrum.Intensity.YMean();

            if (intensity_mean < intensity_mean_threshold)
            {
                this.SaveReferenceMethod?.Invoke(spectrum, SpectrumType.Fix2GlassWhite, true, string.Empty);

                if (this.CallFailureMethod)
                    this.FailureMethod?.Invoke();

                WhiteReferenceException ex = new WhiteReferenceException(Catalog.GetString("Invalid white reference. Please make sure the light source is not damaged."));

                if (throw_exception)
                    throw ex;

                else
                    return new Fix2GlassWhiteReferenceCheckResult(false,
                                                                  spectrum,
                                                                  ex,
                                                                  processor.InstrumentReferenceSystem,
                                                                  intensity_mean,
                                                                  intensity_mean_threshold);
            }

            return new Fix2GlassWhiteReferenceCheckResult(true,
                                                          spectrum,
                                                          null,
                                                          processor.InstrumentReferenceSystem,
                                                          intensity_mean,
                                                          intensity_mean_threshold);
        }
    }
}
