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

namespace Raytheon.Common
{
   /// <summary>
   /// 
   /// </summary>
   public abstract class ConfigurationFileBase : IConfigurationFile
   {
      /// <summary>
      /// configuration file info
      /// </summary>
      protected static FileInfo _fileInfo;

      /// <summary>
      /// configuration file name
      /// </summary>
      protected string _fileName;

      /// <summary>
      /// returns file name
      /// </summary>
      public string FileName => _fileName;

      /// <summary>
      /// configuration type derived based on file extension 
      /// or by the type of the IConfigurationFile implementation
      /// </summary>
      protected ConfigurationFileType _configurationType;

      /// <summary>
      /// returns configuration type
      /// </summary>
      public ConfigurationFileType ConfigurationFileType => _configurationType;

      /// <summary>
      /// constructor that takes file name
      /// </summary>
      /// <param name="fileName"></param>
      /// <exception cref="ArgumentException"></exception>
      public ConfigurationFileBase(string fileName)
      {
         _fileInfo = new FileInfo(fileName);
         _fileName = _fileInfo.FullName;
         _configurationType = ConfigurationTypeFromFileExtension(_fileInfo.Extension);
      }

      /// <summary>
      /// returns supported configuration type enum based on the file type
      /// </summary>
      /// <param name="fileExtension"></param>
      /// <returns></returns>
      protected static ConfigurationFileType ConfigurationTypeFromFileExtension(string fileExtension)
      {
         if (string.IsNullOrEmpty(fileExtension))
         {
            return ConfigurationFileType.OTHER;
         }
         return fileExtension.ToLower() switch
         {
            ".ini" => ConfigurationFileType.INI,
            ".xml" => ConfigurationFileType.XML,
            ".json" => ConfigurationFileType.JSON,
            ".toml" => ConfigurationFileType.TOML,
            ".yaml" => ConfigurationFileType.YAML,
            _ => ConfigurationFileType.OTHER,
         };
      }

      /// <summary>
      /// Reads all keys from a section of configuration file.
      /// </summary>
      /// <param name="section"></param>
      /// <returns></returns>
      public abstract List<string> ReadAllKeys(string section);

      /// <summary>
      /// Returns a list of all of the sections in the configuration file.
      /// </summary>
      /// <returns></returns>
      public abstract List<string> 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 abstract List<T> ReadList<T>(string section, string key, IList<T> defList = null);

      /// <summary>
      /// reads a single value from configuration file by section and key
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="section"></param>
      /// <param name="key"></param>
      /// <param name="defValue"></param>
      /// <returns></returns>
      public abstract T ReadValue<T>(string section, string key, T defValue = default);

      /// <summary>
      /// reads a single value from configuration file by section and key
      /// </summary>
      /// <param name="section"></param>
      /// <param name="key"></param>
      /// <returns></returns>
      public abstract string ReadValue(string section, string 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 abstract void WriteList<T>(string section, string key, IList<T> listToWrite);

      /// <summary>
      /// Write a single value to a given section by key
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="section"></param>
      /// <param name="key"></param>
      /// <param name="lineToWrite"></param>
      public abstract void WriteValue<T>(string section, string key, T lineToWrite);

      /// <summary>
      /// checks if the value exists in configuration file
      /// </summary>
      /// <param name="section"></param>
      /// <param name="key"></param>
      /// <returns></returns>
      /// <exception cref="NotImplementedException"></exception>
      virtual public bool ValueExists(string section, string key)
      {
         return ReadAllKeys(section).Contains(key);
      }
   }
}
