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

namespace Hiperscan.Extensions.ReferenceCheck.Control.Acquisition
{
    public static class SpectrumExtension
    {
        private const string REFERENCE_CHECK_HEADER = "[Reference-Check::";

        public static readonly string REFERENCE_CHECK_OPERATOR = String.Format("{0}{1}",
                                                                               SpectrumExtension.REFERENCE_CHECK_HEADER,
                                                                              "Operator]");

        public static readonly string REFERENCE_CHECK_SESSION = String.Format("{0}{1}",
                                                                               SpectrumExtension.REFERENCE_CHECK_HEADER,
                                                                              "Session]");

        public static readonly string REFERENCE_CHECK_SERIAL = String.Format("{0}{1}",
                                                                               SpectrumExtension.REFERENCE_CHECK_HEADER,
                                                                              "Serial]");


        public static bool xIsValidReferenceCheckSpectrum(this Spectrum spectrum)
        {
            return string.IsNullOrEmpty(spectrum.Comment) == false && spectrum.Comment.
                         Contains(SpectrumExtension.REFERENCE_CHECK_HEADER);
        }

        public static string xOperator(this Spectrum spectrum)
        {
            string operator_name = string.Empty;
            string[] comment_data = spectrum.Comment.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in comment_data)
            {
                if (line.StartsWith(SpectrumExtension.REFERENCE_CHECK_OPERATOR, StringComparison.InvariantCulture) == true)
                {
                    operator_name = line.Replace(SpectrumExtension.REFERENCE_CHECK_OPERATOR, string.Empty);
                    break;
                }

            }

            return operator_name;
        }

        public static int xSessionId(this Spectrum spectrum)
        {
            string session_id = string.Empty;
            string[] comment_data = spectrum.Comment.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in comment_data)
            {
                if (line.StartsWith(SpectrumExtension.REFERENCE_CHECK_SESSION, StringComparison.InvariantCulture) == true)
                {
                    session_id = line.Replace(SpectrumExtension.REFERENCE_CHECK_SESSION, string.Empty);
                    break;
                }

            }

            return Int32.Parse(session_id);
        }

        public static string xSerial(this Spectrum spectrum)
        {
            string serial = string.Empty;
            string[] comment_data = spectrum.Comment.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in comment_data)
            {
                if (line.StartsWith(SpectrumExtension.REFERENCE_CHECK_SERIAL, StringComparison.InvariantCulture) == true)
                {
                    serial = line.Replace(SpectrumExtension.REFERENCE_CHECK_SERIAL, string.Empty);
                    break;
                }

            }

            return serial;
        }
    }
}
