// Created with MonoDevelop
//
//    Copyright (C) 2011 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.IO;
using System.Reflection;
using System.Text;

using Hiperscan.Common;
using Hiperscan.SGS.Benchtop.FinderSetup;
using Hiperscan.SGS.Benchtop.Transformations;
using Hiperscan.SGS.Common;
using Hiperscan.SGS.SerialInterface;
using Hiperscan.Spectroscopy;
using Hiperscan.Spectroscopy.IO;
using Hiperscan.Spectroscopy.Math;
using Hiperscan.Unix;

using Math = System.Math;


namespace Hiperscan.SGS.Benchtop
{
    public class ReferenceSpectraStorage
    {
        public void ClearAll()
        {
            this.Spectra.Clear();
            this.FileNames.Clear();
        }

        public void ClearCalibrations()
        {
        }


        public void Add(Spectrum spectrum, string filename)
        {
            if (this.Spectra.ContainsKey(spectrum.SpectrumType) == false)
                this.Spectra.Add(spectrum.SpectrumType, null);

            this.Spectra[spectrum.SpectrumType] = spectrum;

            if (this.FileNames.ContainsKey(spectrum.SpectrumType) == false)
                this.FileNames.Add(spectrum.SpectrumType, String.Empty);

            this.FileNames[spectrum.SpectrumType] = filename;
        }

        public void Restore(string referencing_dir)
        {
            foreach (SpectrumType type in this.Spectra.Keys)
            {
                try
                {
                    Spectrum spectrum = this.Spectra[type];
                    string file = Path.Combine(referencing_dir, this.FileNames[type]);
                    string directory = Path.GetDirectoryName(file);

                    if (File.Exists(file))
                        continue;

                    if (Directory.Exists(directory) == false)
                        Directory.CreateDirectory(directory);

                    CSV.Write(spectrum, file);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Cannot copy spectra type {0}: {1}", type, ex.Message);
                }
            }
        }
        private Dictionary<SpectrumType, string> FileNames { get; set; } = new Dictionary<SpectrumType, string>();
        private Dictionary<SpectrumType, Spectrum> Spectra { get; set; } = new Dictionary<SpectrumType, Spectrum>();
    }
}
