// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2022, 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.ApoIdentCommon.ApoConfig;
using Hiperscan.Unix;

namespace Hiperscan.ApoIdentCore.ComplexFinder
{
    public partial class CombinedFinder
    {
        public void UpdateReferencingDir(ProfileSettings profile)
        {
            try
            {
                if (this.FinderExists == false)
                    return;

                if (profile == null || profile.ArchiveMissing)
                {
                    this.ResetFinderAutoSaveDirectory();
                    return;
                }

                this.CreateReferencingDir(profile);
                this.SetFinderAutoSaveDirectory(profile.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(ProfileSettings profile) => FileIO.CreateDirectoryIfNotExists(profile.ArchiveReferencingDir, 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;
            }
        }

        public bool ResetAllReferences()
        {
            try
            {
                if (this.FinderExists == false)
                    return false;

                this.StandardFinder.ResetReferences();
                this.StandardFinder.ResetRecalibrationTimestamp();

                this.InsertFinder.ResetReferences();
                this.InsertFinder.ResetRecalibrationTimestamp();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void ResetRecalibrationTimestamp()
        {

            if (this.FinderExists == false)
                return;

            this.Current.ResetRecalibrationTimestamp();
        }

        public bool IsAnyFinderExternalReferenced
        {
            get
            {
                try
                {
                    if (this.FinderExists == false)
                        return false;

                    return this.InsertFinder.ReferencingIsPending == false ||   
                               this.StandardFinder.ReferencingIsPending == false;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public bool IsStandardFinderRecalibrationPending
        {
            get
            {
                try
                {
                    return this.Current.RecalibrationIsPending;
                }
                catch (Exception)
                {
                    return true;
                }
            }
        }

        public DateTime RecalibrationTimeStamp
        {
            get
            {
                try
                {
                    return this.Current.RecalibrationTimestamp;
                }
                catch (Exception)
                {
                    return DateTime.Now.AddYears(-1);
                }
            }
        }

        public uint RecalibrationTimmeoutMilliSeconds
        {
            get
            {
                try
                {
                    return this.Current.RecalibrationTimeoutMilliseconds;
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
    }
}
