// ******************************************************************************//
//  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.
//
//  WARNING: THIS DOCUMENT CONTAINS TECHNICAL DATA AND / OR TECHNOLOGY WHOSE
//  EXPORT OR DISCLOSURE TO NON-U.S. PERSONS, WHEREVER LOCATED, IS RESTRICTED
//  BY THE INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) (22 C.F.R. SECTION
//  120-130) OR THE EXPORT ADMINISTRATION REGULATIONS (EAR) (15 C.F.R. SECTION
//  730-774). THIS DOCUMENT CANNOT BE EXPORTED (E.G., PROVIDED TO A SUPPLIER
//  OUTSIDE OF THE UNITED STATES) OR DISCLOSED TO A NON-U.S. PERSON, WHEREVER
//  LOCATED, UNTIL A FINAL JURISDICTION AND CLASSIFICATION DETERMINATION HAS
//  BEEN COMPLETED AND APPROVED BY RAYTHEON, AND ANY REQUIRED U.S. GOVERNMENT
//  APPROVALS HAVE BEEN OBTAINED. VIOLATIONS ARE SUBJECT TO SEVERE CRIMINAL
//  PENALTIES.
//
//  DOD 5220.22-M, INDUSTRIAL SECURITY MANUAL, CHAPTER 5, SECTION 1 THROUGH 9 :
//  FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN OR DOD 5200.1-R,
//  INFORMATION SECURITY PROGRAM, CHAPTER 6. FOR UNCLASSIFIED, LIMITED DOCUMENTS
//  DESTROY BY ANY  METHOD THAT WILL PREVENT DISCLOSURE OF CONTENTS OR
//  RECONSTRUCTION OF THE DOCUMENT.
// ****************************************************************************//
using System;
using System.Collections.Generic;

namespace Raytheon.Common
{

   /// <summary>
   /// main class to be instantiated when working with configuration files
   /// </summary>
   public class ConfigurationFile : ConfigurationFileBase
   {
      /// <summary>
      /// concrete implementation reference
      /// </summary>
      protected IConfigurationFile _configurationFile;

      /// <summary>
      /// Raytheon.Configuration will support INI and XML file formats. 
      /// For other formats have to use designated constructor 
      /// </summary>
      /// <param name="fileName"></param>
      /// <exception cref="InvalidOperationException"></exception>
      public ConfigurationFile(string fileName)
       : base(fileName)
      {
         if (!_fileInfo.Exists)
         {
            throw new Exception($"Can't find file: {fileName}");
         }
         var factory = GetFactory();
         _configurationFile = factory.GetConfigurationFile(fileName);
      }

      /// <summary>
      /// extension constructor, for other types based on the provided factory
      /// </summary>
      /// <param name="fileName"></param>
      /// <param name="factory"></param>
      public ConfigurationFile(string fileName, ConfigurationFileFactoryBase factory)
         : base(fileName)
      {
         if (!_fileInfo.Exists)
         {
            throw new Exception($"Can't find file: {fileName}");
         }
         _configurationFile = factory.GetConfigurationFile(fileName);
      }

      /// <summary>
      /// extension constructor, for other types based on provided type
      /// </summary>
      /// <param name="fileName"></param>
      /// <param name="type"></param>
      public ConfigurationFile(string fileName, string type)
         : base(fileName)
      {
         if (!_fileInfo.Exists)
         {
            throw new Exception($"Can't find file: {fileName}");
         }
         ConfigurationFileFactory factory = new(type);
         _configurationFile = factory.GetConfigurationFile(fileName);
      }

      /// <summary>
      /// returns a factory based on file extension
      /// </summary>
      /// <returns></returns>
      /// <exception cref="ArgumentException"></exception>
      private static ConfigurationFileFactoryBase GetFactory()
      {
         ConfigurationFileType configurationType = ConfigurationTypeFromFileExtension(_fileInfo.Extension);

         ConfigurationFileFactoryBase factory = configurationType switch
         {
            ConfigurationFileType.INI => new IniConfigurationFileFactory(),
            ConfigurationFileType.XML => new XmlConfigurationFileFactory(),
            ConfigurationFileType.JSON => new JsonConfigurationFileFactory(),
            ConfigurationFileType.TOML => new TomlConfigurationFileFactory(),
            _ => throw new ArgumentException($"Configuration Type ({configurationType}) not supported by Configuration Manager"),
         };
         return factory;
      }

      /// <summary>
      /// Reads all keys from a section of an ini file.
      /// </summary>
      /// <param name="section"></param>
      /// <returns></returns>
      public override List<string> ReadAllKeys(string section)
      {
         return _configurationFile.ReadAllKeys(section);
      }

      /// <summary>
      /// Returns a list of all of the sections in the ini file.
      /// </summary>
      /// <returns></returns>
      public override List<string> ReadAllSections()
      {
         return _configurationFile.ReadAllSections();
      }

      /// <summary>
      /// reads a list of values from configuration file
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="section"></param>
      /// <param name="key"></param>
      /// <param name="defList"></param>
      /// <returns></returns>
      public override List<T> ReadList<T>(string section, string key, IList<T> defList = null)
      {
         return _configurationFile.ReadList<T>(section, key, defList);
      }

      /// <summary>
      /// reads a value from configuration file
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="section"></param>
      /// <param name="key"></param>
      /// <param name="defValue"></param>
      /// <returns></returns>
      public override T ReadValue<T>(string section, string key, T defValue = default)
      {
         return _configurationFile.ReadValue<T>(section, key, defValue);
      }

      /// <summary>
      /// reads a single value in json format
      /// </summary>
      /// <param name="section"></param>
      /// <param name="key"></param>
      /// <returns>configuration value</returns>
      /// <exception cref="NotImplementedException"></exception>
      public override string ReadValue(string section, string key)
      {
         return _configurationFile.ReadValue(section, key);
      }

      /// <summary>
      /// writes a list of values to configuration file
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="section"></param>
      /// <param name="key"></param>
      /// <param name="listToWrite"></param>
      public override void WriteList<T>(string section, string key, IList<T> listToWrite)
      {
         _configurationFile.WriteList<T>(section, key, listToWrite);
      }

      /// <summary>
      /// Write string value
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="section"></param>
      /// <param name="key"></param>
      /// <param name="lineToWrite"></param>
      public override void WriteValue<T>(string section, string key, T lineToWrite)
      {
         _configurationFile.WriteValue(section, key, lineToWrite);
      }
   }
}
