// 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.IO;
using System.Reflection;

using Hiperscan.FileIO;
using Hiperscan.I18n;
using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{   
	public static class FilesAndDirs
	{
		public const string QUERY_DIR = ".QueryMetaData";

		public static void Init(FinderWindow finder_winbdow)
		{
            FilesAndDirs.UserProfileChangedMode = finder_winbdow.host.ProfileChangedMode;
			FilesAndDirs.Config = finder_winbdow.Config;
			FilesAndDirs.ConfigDir = finder_winbdow.host.ConfigurationDirPath;
			FilesAndDirs.QuickStepWorkingDir = new FileInfo(Assembly.GetEntryAssembly().Location).Directory.FullName;
		}
      
		#region global cached data

        private static string CacheDataDir
        {
            get
            {
                return Path.Combine(Hiperscan.Unix.FileIO.CacheDir,
                                   "apo-ident-cache");
            }
        }

        private static string LocatedCacheDataDir
		{
			get
			{
				Locale.LocaleType location = FilesAndDirs.Config.AcquisitionSettings.Locale;

                return Path.Combine(FilesAndDirs.CacheDataDir,
									location.ToString());
			}
		}

		public static string UpdateCertificateDir
        {
            get
            {
				return Path.Combine(FilesAndDirs.LocatedCacheDataDir, "update_certificate");
            }
        }

        public static string UpdateCertificateFile
        {
            get
            {
                return Path.Combine(FilesAndDirs.UpdateCertificateDir, ApoIdentCommon.DefaultNames.IdentServer.UPDATE_CERTIFICATE_FILE);
            }
        }

		public static string SubstanceOverviewDir
        {
            get
            {
				return Path.Combine(FilesAndDirs.LocatedCacheDataDir, "substance_overwiew");
            }
        }
      
        public static string SubstanceOverviewAvailableFile
        {
            get
            {
                return Path.Combine(SubstanceOverviewDir, "available_substances.pdf");
            }
        }

        public static string SubstanceOverviewAddedFile
        {
            get
            {
                return Path.Combine(SubstanceOverviewDir, "added_substances.pdf");
            }
        }

        public static string SubstanceOverviewInsertFile
        {
            get
            {
                return Path.Combine(SubstanceOverviewDir, "insert_substances.pdf");
            }
        }


        public static string ValidationDir
        {
            get
            {
                return Path.Combine(FilesAndDirs.LocatedCacheDataDir, ApoIdentCommon.DefaultNames.IdentServer.VALIDATION_DIRECTORY);
            }
        }

        public static string ValidationFile(string classifier_name)
        {
            string corrected_name = FileIO.FileIOHelper.ConvertToValidFilename(classifier_name) + ".pdf";
            return Path.Combine(FilesAndDirs.ValidationDir, corrected_name);
        }

		public static string AdditionalDocuDir
        {
            get
            {
                return Path.Combine(FilesAndDirs.LocatedCacheDataDir, ApoIdentCommon.DefaultNames.IdentServer.ADDITIONAL_TESTS_DIRECTORY);
            }
        }

        public static string AdditionalDocuSolidFile
        {
            get
            {
                return Path.Combine(FilesAndDirs.AdditionalDocuDir, ApoIdentCommon.DefaultNames.IdentServer.ADDITIONAL_TESTS_SOLIDS_FILE);
            }
        }

        public static string AdditionalDocuLiquid1File
        {
            get
            {
                return Path.Combine(FilesAndDirs.AdditionalDocuDir, Hiperscan.Extensions.ApoIdentCommon.DefaultNames.IdentServer.ADDITIONAL_TESTS_LIQUIDS_1_FILE);
            }
        }

        public static string AdditionalDocuLiquid2File
        {
            get
            {
                return Path.Combine(FilesAndDirs.AdditionalDocuDir, Hiperscan.Extensions.ApoIdentCommon.DefaultNames.IdentServer.ADDITIONAL_TESTS_LIQUIDS_2_FILE);
            }
        }

		#endregion







		#region user defined apo-ident dir

        public static bool AnyProfileExists
        {
            get
            {
                return FilesAndDirs.Config.Profiles.Current != null;
            }
        }

		public static string Archive
        {
            get
            {
				return FilesAndDirs.Config.Profiles.Current.Archive;
            }
        }

		public static string ArchiveYearDir
        {
            get
            {
                return Path.Combine(FilesAndDirs.Archive, DateTime.Now.ToString("yyyy"));
            }
        }

        public static string ArchiveReferencingDir
        {
            get
            {
                return Path.Combine(FilesAndDirs.Archive, Catalog.GetString(".Referencing"));
            }
        }

		private static string ArchiveQueryDir
        {
            get
            {
				return Path.Combine(FilesAndDirs.ArchiveYearDir, FilesAndDirs.QUERY_DIR);
            }
        }


        public static string ProtocolCsvFileName(ReportInfo info)
        {
            return Path.Combine(FilesAndDirs.ArchiveYearDir, 
                                FilesAndDirs.GetProtocolNameWithoutFileExtension(info) + ".csv");
        }

        public static string ProtocolPdfFileName(ReportInfo info)
        {
            return Path.Combine(FilesAndDirs.ArchiveYearDir, 
                                FilesAndDirs.GetProtocolNameWithoutFileExtension(info) + ".pdf");
        }

        public static string ProtocolXmlFileName(ReportInfo info)
        {
			return Path.Combine(FilesAndDirs.ArchiveQueryDir, 
                                FilesAndDirs.GetProtocolNameWithoutFileExtension(info) + ".xml");
        }

        private static string GetProtocolNameWithoutFileExtension(ReportInfo info)
        {
            string specimen = info.DefaultName;

            if (String.IsNullOrEmpty(info.LatinName) == false)
            {
                if (FilesAndDirs.Config.Profiles.Current.FileNameStart == FilenameStartTypes.LatinName)
                    specimen = info.LatinName;
            }

            if (specimen.Length > 10)
                specimen = specimen.Remove(10);

            return FileIOHelper.ConvertToValidFilename(
                specimen + "__" + info.Batch + "__" + info.Date.ToString("yyyy-MM-dd_HH-mm-ss"));
        }

		public static string LocalizedMetaDir
		{
			get
			{
				Locale.LocaleType location = FilesAndDirs.Config.AcquisitionSettings.Locale;

				return Path.Combine(FilesAndDirs.Archive,
                                    ".meta",
                                    location.ToString());
			}
		}

		public static string ModelClassExtensionFile
        {
            get
            {
				return Path.Combine(FilesAndDirs.LocalizedMetaDir, "user_defined_substances.xml");
            }
        }

        public static string ModelClassExtensionBackupFile
        {
            get
            {
				return Path.Combine(FilesAndDirs.LocalizedMetaDir, "user_defined_substances_backup.xml");
            }
        }

		public static string AdditionalTestAutoCompletionFile
        {
            get
            {
				return Path.Combine(FilesAndDirs.LocalizedMetaDir, "additional_test_auto_completion.xml");
            }

        }

		public static string AdditionalTestAutoCompletionBackupFile
        {
            get
            {
                return Path.Combine(FilesAndDirs.LocalizedMetaDir, "additional_test_auto_completion_back_up.xml");
            }

        }
      
      
		public static string OldAdditionalTestAutoCompletionFile
        {
            get
            {
                return Path.Combine(FilesAndDirs.ConfigDir, "additional_test_auto_completion.xml");
            }           
        }
      
        #endregion
      

        #region extern programs
      
        public static string UpdateCertificateExeFile
        {
            get
            {
                return Path.Combine(FilesAndDirs.QuickStepWorkingDir, "Plugins", "ApoIdent", "UpdateCertificate.exe");
            }
        }

        public static string MonoExeFile
        {
            get
            {
                return Path.Combine(FilesAndDirs.QuickStepWorkingDir, "Mono", "bin", "mono.exe");
            }
        }

        public static string IdentServerWorkingDir
        {
            get
            {
                return Path.Combine(FilesAndDirs.QuickStepWorkingDir, "IdentServer");
            }
        }

        public static string IdentServerExeFile
        {
            get
            {
                return Path.Combine(FilesAndDirs.IdentServerWorkingDir, "IdentServer.exe");
            }
        }

        public static string IdentServerLogFile
        {
            get
            {
                return Path.Combine(FilesAndDirs.ConfigDir, ApoIdentCommon.DefaultNames.IdentServer.LOG_TXT_FILE);
            }
        }

        #endregion

        #region miscellaneous

        public static string DefaultDataExportDir
        {
            get
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                if (FilesAndDirs.UserProfileChangedMode == true)
                    path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);

                if (Directory.Exists(path) == false)
                {
                    path = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
                    path = Path.GetDirectoryName(path);
                }
                    
                return path;
            }
        }

        #endregion


        private static Configurations Config        {get; set;}                                     
        public static string ConfigDir              {get; private set;}
        private static string QuickStepWorkingDir   {get; set;} 
        private static bool UserProfileChangedMode  { get; set; }
	}
}

