// Created with MonoDevelop
//
//    Copyright (C) 2013 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.IO;
using Hiperscan.Spectroscopy;
using Hiperscan.Spectroscopy.IO;
using Hiperscan.Unix;

namespace Hiperscan.SGS.Benchtop
{
    public partial class StabilizedFinderX
    {
        public void SaveReferenceSpectrum(Spectrum spectrum, SpectrumType spectrum_type, bool error = false, string name = "")
        {

            spectrum.SpectrumType = spectrum_type;

            string fname = string.Empty;
            string dirname = string.Empty;

            bool spectrum_infos_can_be_changed = true;

            switch (spectrum_type)
            {

                case SpectrumType.Fix2GlassBlack:
                    fname = SpectrumFileNames.GetFix2GlassBlack(spectrum);
                    spectrum.Label += " " + Catalog.GetString("Black reference");
                    dirname = SpectrumDirNames.FIX2GLASS_BLACK;
                    break;

                case SpectrumType.Fix2GlassWhite:
                    fname = SpectrumFileNames.GetFix2GlassWhite(spectrum);
                    spectrum.Label += " " + Catalog.GetString("White reference");
                    dirname = SpectrumDirNames.FIX2GLASS_WHITE;
                    break;

                case SpectrumType.Fix2GlassOxid:
                    fname = SpectrumFileNames.GetFix2GlassWhite(spectrum);
                    spectrum.Label += " " + Catalog.GetString("Oxide");
                    dirname = SpectrumDirNames.FIX2GLASS_OXIDE;
                    break;

                case SpectrumType.InternalDark:
                    fname = SpectrumFileNames.GetInternalDark(spectrum);
                    if (name.EndsWith("master", StringComparison.InvariantCulture))
                        fname = SpectrumFileNames.GetInternalDarkMaster(spectrum);
                    spectrum.Label += " " + Catalog.GetString("Dark");
                    dirname = SpectrumDirNames.INTERNAL_DARK;
                    break;

                case SpectrumType.ExternalFluidWhite:
                    fname = SpectrumFileNames.GetExternalTransflectance(spectrum);
                    spectrum.Label += " " + Catalog.GetString("Transflectance insert");
                    dirname = SpectrumDirNames.EXTERNAL_TRANSFLECTANCE;
                    break;


                case SpectrumType.SolidSpecimen:
                    fname = SpectrumFileNames.GetSample(spectrum);
                    spectrum.Label += " " + Catalog.GetString("Sample (solids)");
                    dirname = SpectrumDirNames.SAMPLE;
                    break;

                case SpectrumType.FluidSpecimen:
                    fname = SpectrumFileNames.GetSample(spectrum);
                    spectrum.Label += " " + Catalog.GetString("Sample (liquids)");
                    dirname = SpectrumDirNames.SAMPLE;
                    break;

                

                case SpectrumType.ReferenceSet:
                    spectrum_infos_can_be_changed = false;
                    fname = name;
                    dirname = SpectrumDirNames.REFERENCE_SET;
                    fname += ".csv";
                    break;

                case SpectrumType.ProcessedSolidSpecimen:
                    fname = SpectrumFileNames.GetSample(spectrum);
                    dirname = SpectrumDirNames.PROCESSED;
                    break;

                case SpectrumType.ProcessedFluidSpecimen:
                    fname = SpectrumFileNames.GetSample(spectrum);
                    dirname = SpectrumDirNames.PROCESSED;
                    break;

            }

            if (error)
                dirname = SpectrumDirNames.ERROR;

            if (spectrum_infos_can_be_changed == true)
                this.BeforeSaveSpectrum?.Invoke(spectrum);

            try
            {
                spectrum.Creator = this.creator_name;

                if (this.AutoSaveReferences == false)
                    return;

                string save_dir = Path.Combine(this.AutoSaveDirectory, dirname);
                FileIO.CreateDirectory(save_dir);

                fname = Path.Combine(save_dir, fname);
                CSV.Write(spectrum, fname);
                spectrum.FileInfo = new FileInfo(fname);
            }
            catch (IOException)
            {
                this.ShowInfoMessage?.Invoke(Catalog.GetString("Failed to save reference spectrum automatically. There might be a problem with a network resource."));
            }

        }

        public void SetFinderStatus(FinderStatus status)
        {
            this.FinderStatus = status;
            this.FinderStatusChanged?.Invoke(this.Device, status);
        }
    }
}
