// ******************************************************************************//
//  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>
   /// Read/Write to an XML file.
   /// </summary>
   public class XmlConfigurationFile : ConfigurationFileBase
   {
      #region PrivateMemberVariables

      /// <summary>
      /// Raytheon configuration
      /// </summary>
      private readonly IConfigurationManager _configurationManager;
      private readonly IConfiguration _configuration;

      #endregion

      #region PublicFunctions

      /// <summary>
      /// The constructor. It will check to make sure the file exists and throw an exception it if does not
      /// </summary>
      /// <param name="fileName">The ini file name (path).</param>
      public XmlConfigurationFile(string fileName)
         : base(fileName)
      {
         if (ConfigurationFileType != ConfigurationFileType.XML)
         {
            throw new ArgumentException("Expecting XML file configuration type");
         }

         _configurationManager = new RaytheonConfigurationManager(_fileInfo.DirectoryName);
         _configuration = _configurationManager.GetConfiguration(NameWithoutExtension(_fileInfo));
      }
      private static string NameWithoutExtension(FileInfo fileInfo)
      {
         return fileInfo.Name.Remove(fileInfo.Name.LastIndexOf("."));
      }

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

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

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

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

      /// <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)
      {
         _configuration.SetConfigurationListValue(section, key, listToWrite);
      }

      /// <summary>
      /// Reads all keys from a section of an ini file.
      /// </summary>
      /// <param name="section">The section.</param>
      /// <returns>A list of all keys.</returns>
      public override List<string> ReadAllKeys(string section)
      {
         return ConfigurationHelper.GetAvailableConfigSectionKeys(_fileName, section);
      }

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

      #endregion
   }
}