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

namespace Hiperscan.Spectroscopy.Processing
{
    public partial class ReferenceWheelProcessor
    {
        private void CalculateTemperatureDependedCorrection()
        {
            Spectrum dark = this.InternalDark.Clone();
            Spectrum white = this.InternalWhite.Clone();
            Spectrum oxid = this.CorrectedOxideOfSample.Clone();

            Spectrum darkref = this.InternalDarkReference.Clone();
            Spectrum whiteref = this.InternalWhiteReference.Clone();
            Spectrum oxidref = this.CorrectedOxideOfReferences.Clone();

            if (this.HasAdcCorrection == false)
            {
                dark.AdcCorrection = null;
                white.AdcCorrection = null;
                oxid.AdcCorrection = null;
                darkref.AdcCorrection = null;
                whiteref.AdcCorrection = null;
                oxidref.AdcCorrection = null;
            }

            if (this.use_default_system_function && dark.HasSystemFunctionCorrection == false)
                dark.SystemFunctionCorrection = DataSet.GetDefaultSystemFunctionCorrectionCoefficients();
            if (this.use_default_system_function && white.HasSystemFunctionCorrection == false)
                white.SystemFunctionCorrection = DataSet.GetDefaultSystemFunctionCorrectionCoefficients();
            if (this.use_default_system_function && oxid.HasSystemFunctionCorrection == false)
                oxid.SystemFunctionCorrection = DataSet.GetDefaultSystemFunctionCorrectionCoefficients();

            if (this.use_default_system_function && darkref.HasSystemFunctionCorrection == false)
                darkref.SystemFunctionCorrection = DataSet.GetDefaultSystemFunctionCorrectionCoefficients();
            if (this.use_default_system_function && whiteref.HasSystemFunctionCorrection == false)
                whiteref.SystemFunctionCorrection = DataSet.GetDefaultSystemFunctionCorrectionCoefficients();
            if (this.use_default_system_function && oxidref.HasSystemFunctionCorrection == false)
                oxidref.SystemFunctionCorrection = DataSet.GetDefaultSystemFunctionCorrectionCoefficients();

            List<Spectrum> fit_spectra = new List<Spectrum>
            {
                oxid.Clone(),
                white.Clone(),
                dark.Clone()
            };
            foreach (Spectrum spectrum in fit_spectra)
            {
                spectrum.ApplyCorrectionsToRawData(this.CorrectionMask & ~CorrectionMask.DarkIntensity);
            }

            List<Spectrum> ref_spectra = new List<Spectrum>
            {
                oxidref.Clone(),
                whiteref.Clone(),
                darkref.Clone()
            };
            foreach (Spectrum spectrum in ref_spectra)
            {
                spectrum.ApplyCorrectionsToRawData(this.CorrectionMask & ~CorrectionMask.DarkIntensity);
            }

            LinearDistortion ld = Spectrum.MatchIntensities(ref_spectra, fit_spectra, 1100.0, 1700.0);
            whiteref.LinearDistortion = ld;

            whiteref.ApplyCorrectionsToRawData(this.CorrectionMask & ~CorrectionMask.DarkIntensity);
            white.ApplyCorrectionsToRawData(this.CorrectionMask & ~CorrectionMask.DarkIntensity);

            DataSet factor = white.RawIntensity / whiteref.RawIntensity;
            factor = factor.Filter(3, 51);

            this.linear_distortion = ld;
            this.correction_vector = factor;
        }
    }
}
