
// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2023. 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.Linq;
using System.Collections.Generic;

using Hiperscan.Unix;
using Hiperscan.PdfLib;
using Hiperscan.Spectroscopy;
using Hiperscan.Spectroscopy.Processing;
using Hiperscan.Spectroscopy.ReferenceChecks;
using Hiperscan.Extensions.ReferenceCheck.Control.Acquisition;

namespace Hiperscan.Extensions.ReferenceCheck.Control.Evaluation
{
	public partial class ResultData
	{
        private void ReferenceBlockSetWhitePEEvaluation()
        {
            this.WhitePESessionResults = new List<WhitePESessionResults>();

            foreach (var white_pe_session in this.SessionsWhiteInsert)
            {
                //ReferenceBlockProcessor set = this.NewReferenceBlockProcessorWithCalibrationSpectra(white_pe_session.Id);
                /*FIXME Placeholder for Referenceblock of FindeX
                foreach (var white_pe_spectrum in white_pe_session.Spectra)
                {
                    WhiteResult result = this.EvaluateWhiteReference(white_pe_spectrum.Value, 
                                                                     set.FixTwoGlassWhiteReference, 
                                                                     white_pe_session.Id, 
                                                                     white_pe_spectrum.Key);
                    this.WhiteResults.Add(result);
                }*/
            }
        }

        private void ReferenceWheelSetWhitePEEvaluation()
        {
            this.WhitePESessionResults = new List<WhitePESessionResults>();

            foreach (var white_pe_session in this.SessionsWhiteInsert)
            {
                AcquisitionSession black_session = this.SessionsBlack
                                                       .First(x => x.Id == white_pe_session.Id);
                Spectrum black_spectrum = black_session.Spectra.First().Value;
                DateTime session_time = this.CalibrationSessions.First(x => x.Id == black_session.Id).AcquisitionTime;

                WhitePESessionResults white_pe_session_results = new WhitePESessionResults()
                {
                    SessionNumber = white_pe_session.Id,
                    WhitePEResults = new List<WhitePEResult>()
                };

                foreach (var white_pe_spectrum in white_pe_session.Spectra)
                {
                    ReferenceWheelProcessor processor = this.NewReferenceWheelProcessorWithCalibrationSpectras(white_pe_session.Id, (Hiperscan.Spectroscopy.Processing.ReferenceWheelVersion)this.ReferenceWheelVersion);
                    processor.ExternalBlackReference = black_spectrum;
                    processor.ExternalSampleInsertWhiteReference = white_pe_spectrum.Value;

                    if (processor.ExternalSampleInsertWhiteReference == null |
                        processor.ExternalBlackReference == null)
                    {
                        throw new Exception(string.Format(Catalog.GetString("Cannot set reference for session ID {0}"), white_pe_session.Id));
                    }

                    ReferenceChecks check = new ReferenceChecks(processor);

                    white_pe_session_results.WhitePEResults.Add(new WhitePEResult()
                    {
                        SerialNumber = white_pe_spectrum.Key, 
                        SampleInsertWhiteReferenceCheckResult = check.CheckSampleInsertWhiteReference(processor.ExternalSampleInsertWhiteReference, false)
                    });
                }
                this.WhitePESessionResults.Add(white_pe_session_results);
            }
        }

        internal PdfTableStringCell[,] GetWhitePEReferenceEvaluationData(int session_id)
        {
            Dictionary<string, SampleInsertWhiteReferenceCheckResult> results = this.WhitePESessionResults
                                                                                    .First(x => x.SessionNumber == session_id)
                                                                                    .WhitePEResults
                                                                                    .ToDictionary(x => x.SerialNumber, y => y.SampleInsertWhiteReferenceCheckResult);
            PdfTableStringCell[,] table_data = this.CreateRerferenceTableWithHead(results.Count, 2);

            int index = 1;
            foreach (var item in results)
            {
                table_data[index, 0].Value = item.Key;
                table_data[index, 1].FontType = PDFjet.NET.CoreFont.HELVETICA_BOLD;
                table_data[index, 1].Align = PdfTextAlignments.Center;

                if (item.Value.Success == true)
                {
                    table_data[index, 1].FontColor = PdfColors.DarkGreen;
                    table_data[index, 1].Value = ResultData.PASS;
                }
                else
                {
                    table_data[index, 1].FontColor = PdfColors.Red;
                    table_data[index, 1].Value = ResultData.FAIL;
                }
                /*
                table_data[index, 2].Value = string.Format("{0} > {1}",
                                                           item.Value.Correlation.ToString("#0.000"),
                                                           item.Value.CorrelationThreshold);
                */
                index++;
            }

            return table_data;
        }
    }
}