// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2017 Thomas Klose & Torsten Schache, 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.Diagnostics;

using Hiperscan.Extensions.ApoIdentCommon;
using Hiperscan.SGS;
using Hiperscan.SGS.Benchtop;
using Hiperscan.SGS.Benchtop.Transformations;
using Hiperscan.SGS.Common;
using Hiperscan.SGS.SerialInterface;
using Hiperscan.Spectroscopy;
using Hiperscan.Unix;

using Hiperscan.Common;
using Hiperscan.Common.SearchIpAddress;
using System.Threading;

namespace Hiperscan.Extensions.ApoIdent
{
    public partial class CombinedFinder
    {
        public void UpdateReferencingDir(bool is_archive_missing)
        {
            try
            {
                if (this.FinderExists == false)
                    return;

                if (is_archive_missing)
                {
                    this.ResetFinderAutoSaveDirectory();
                    return;
                }

                this.CreateReferencingDir();
                this.SetFinderAutoSaveDirectory(FilesAndDirs.ArchiveReferencingDir);
                this.CopyAutoSaveData(FilesAndDirs.ArchiveReferencingDir);

            }
            catch(Exception ex)
            {
                Console.WriteLine("Cannot update finder referencing dir: {0}", ex);
                this.ResetFinderAutoSaveDirectory();
            }
        }

        private void ResetFinderAutoSaveDirectory()
        {
            if (this.StandardFinder != null)
                this.StandardFinder.AutoSaveReferences = false;

            if (this.InsertFinder != null)
                this.InsertFinder.AutoSaveReferences = false;
        }

        private void CreateReferencingDir()
        {
            if (Directory.Exists(FilesAndDirs.ArchiveReferencingDir) == false)
            {
                Directory.CreateDirectory(FilesAndDirs.ArchiveReferencingDir);
                new DirectoryInfo(FilesAndDirs.ArchiveReferencingDir).Attributes = FileAttributes.Hidden;
            }
        }


        private void SetFinderAutoSaveDirectory(string referencing_directory)
        {
            if (this.StandardFinder != null)
            {
                this.StandardFinder.AutoSaveDirectory = referencing_directory;
                this.StandardFinder.AutoSaveReferences = true;
            }

            if (this.InsertFinder != null)
            {
                this.InsertFinder.AutoSaveDirectory = referencing_directory;
                this.InsertFinder.AutoSaveReferences = true;
            }
        }

        private void CopyAutoSaveData(string referencing_directory)
        {
            try
            {
                if (this.StandardFinder != null)
                {
                    this.StandardFinder.ReferenceSpectraStorage.Restore(referencing_directory);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Cannot copy reference spectra (standard finder): {0}", ex);
            }

            try
            {
                if (this.InsertFinder != null)
                {
                    this.InsertFinder.ReferenceSpectraStorage.Restore(referencing_directory);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot copy reference spectra (standard finder): {0}", ex);
            }
        }
    }
}
