/*-------------------------------------------------------------------------
//                           UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.

THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.

UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using Raytheon.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace ProgramLib
{
   /// <summary>
	/// Partial class... this is the main class responsible for parsing config.ini in order
   /// to construct file paths and folder paths
	/// </summary>
   internal partial class FileAndFolderManager
   {
      private Dictionary<Folders, string> foldersDict = new Dictionary<Folders, string>();
      private Dictionary<Files, string> filesDict = new Dictionary<Files, string>();

      private IConfigurationFile _programConfig;

      /// <summary>
		/// The private constructor
		/// </summary>
		/// <param name="programConfig">the UUT part number</param>
      public FileAndFolderManager(IConfigurationFile programConfig)
      {
         _programConfig = programConfig;

         ConstructFolderPaths();
         CreateFolders();
         ConstructFilePaths();
      }

      /// <summary>
		/// Build folder paths so we can acccess them later
		/// </summary>
		/// <param name="iniObj"></param>
		/// <returns></returns>
      private void ConstructFolderPaths()
      {
         string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

         string dataRootPath = _programConfig.ReadValue(ProgramConfigIni.GENERAL.ToString(), ProgramConfigIni.DATA_BASE_PATH.ToString(), "NOT SET");
         if (!Path.IsPathRooted(dataRootPath))
            dataRootPath = Path.Combine(assemblyFolder, dataRootPath);

         foldersDict[Folders.DATA] = Path.GetFullPath(dataRootPath);

         string val = _programConfig.ReadValue(ProgramConfigIni.GENERAL.ToString(), ProgramConfigIni.DATA_TEMP_PATH.ToString(), "NOT SET");
         foldersDict[Folders.DATA_TEMP] = Path.Combine(dataRootPath, val);
      }

      /// <summary>
		/// We create all the necessary folders
		/// </summary>
		/// <param name="iniObj"></param>
		/// <returns></returns>
      private void CreateFolders()
      {
         Directory.CreateDirectory(foldersDict[Folders.DATA_TEMP]);
      }

      /// <summary>
		/// Build file paths so we can acccess them later
		/// </summary>
		/// <param name="iniObj"></param>
		/// <returns></returns>
      private void ConstructFilePaths()
      {
         string val = _programConfig.ReadValue(ProgramConfigIni.GENERAL.ToString(), ProgramConfigIni.POWER_SUPPLY_SELF_TEST_DATETIME.ToString(), "NOT SET");
         filesDict[Files.POWER_SUPPLY_SELF_TEST_DATETIME] = Path.Combine(foldersDict[Folders.DATA_TEMP], val);
      }

      /// <summary>
      /// Return the full folder path
      /// </summary>
      /// <param name="folder"></param>
      /// <returns></returns>
      public string getFolder(Folders folder)
      {
         if (foldersDict.ContainsKey(folder))
         {
            return foldersDict[folder];
         }
         else
            throw new Exception($"{folder.ToString()} is invalid");
      }

      /// <summary>
		/// Return the full file path
		/// </summary>
		/// <param name="iniObj"></param>
		/// <returns></returns>
      public string getFile(Files file)
      {
         if (filesDict.ContainsKey(file))
         {
            return filesDict[file];
         }
         else
            throw new Exception($"{file.ToString()} is invalid");
      }
   }
}
