using System;

using CommonHelpers;

namespace SqlToCsvExport {

//**************************************************************************************************
static class AdditionalParams {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static class Names
{
   public const string
      InsertColHeaders     = nameof(InsertColHeaders),
      InsertRecNoColumn    = nameof(InsertRecNoColumn),
      ColSepChar           = nameof(ColSepChar),
      EmbraceAllWithQuotes = nameof(EmbraceAllWithQuotes),
      IncorrectCharSubst   = nameof(IncorrectCharSubst),
      RecNoCaption         = nameof(RecNoCaption),
      SuppressAlienLetters = nameof(SuppressAlienLetters),
      ExportInUTF8         = nameof(ExportInUTF8);
}
public static class DefaultValues
{
   public const bool    InsertColHeaders     = true;
   public const bool    InsertRecNoColumn    = true;
   public const char    ColSepChar           = ';';
   public const bool    EmbraceAllWithQuotes = true;
   public const char    IncorrectCharSubst   = '#';
   public const string  RecNoCaption         = "0";
   public const bool    SuppressAlienLetters = true;
   public const bool    ExportInUTF8         = true;
}

//**************************************************************************************************

public static string NamePrefix
{
   get { return SqlToCsvExport_Info.AssemblyName + "."; }
}

//==================================================================================================

/// <exception cref="OperationFailedException" />
public static void ObtainStr(string name,ref string value)
{
   string str = EnvironmentVariableValue( name );
   if ( !string.IsNullOrEmpty( str ) )
      value = str;
}

/// <exception cref="OperationFailedException" />
public static void ObtainChar(string name,ref char value)
{
   string str = EnvironmentVariableValue( name );
   if ( !string.IsNullOrEmpty( str ) )
      if ( str.Length == 1 )
         value = str[ 0 ];
      else
         throw AdditionalParamObtainmentError( name );
}

/// <exception cref="OperationFailedException" />
public static void ObtainFlag(string name,ref bool value)
{
   string str = EnvironmentVariableValue( name );
   if ( !string.IsNullOrEmpty( str ) )
      if ( str == "1" ) value = true;
      else
         if ( str == "0" ) value = false;
         else throw AdditionalParamObtainmentError( name );
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="OperationFailedException" />
public static void VerifyNames()
{
   foreach (object keyObject in Environment.GetEnvironmentVariables().Keys)
   {
      var substr = keyObject as string;
      if ( !string.IsNullOrEmpty( substr ) &&
           substr.StartsWith( NamePrefix, StringComparison.InvariantCultureIgnoreCase ) )
         VerifyNameSupported( substr );
   }
}

//==================================================================================================

static string ComposeName(string name)
{
   return NamePrefix + name;
}

static OperationFailedException AdditionalParamObtainmentError(string name)
{
   return OpFailedExceptionCreator.Create(HLocal.TEXT(
      "Incorrect value of additional parameter (environment variable): \"{0}\" (look syntax).",
      "Некорректное значение доп.параметра (переменная среды): \"{0}\" (см.синтаксис)." ),
      ComposeName( name ) );
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="OperationFailedException" />
static void VerifyNameSupported(string substr)
{
   var name = substr.Substring( NamePrefix.Length );
   if ( !Array.Exists( _SupportedParameters_, nm => HString.Equals( nm, name, true ) ) )
      throw OpFailedExceptionCreator.Create(HLocal.TEXT(
         "Unknown/unsupported additional parameter (environment variable): \"{0}\" (look syntax).",
         "Неизвестный/неподдерживаемый доп.параметр (переменная среды): \"{0}\" (см.синтаксис)." ),
         ComposeName( name ) );
}

//--------------------------------------------------------------------------------------------------

static string EnvironmentVariableValue(string name)
{
   string str = Environment.GetEnvironmentVariable( ComposeName( name ) );
   return str != null ? str.Trim() : null;
}

//==================================================================================================

static readonly string[]
   _SupportedParameters_ = new string[]
   {
      Names.InsertColHeaders,
      Names.InsertRecNoColumn,
      Names.ColSepChar,
      Names.EmbraceAllWithQuotes,
      Names.IncorrectCharSubst,
      Names.RecNoCaption,
      Names.SuppressAlienLetters,
      Names.ExportInUTF8
   };

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // AdditionalParams
//**************************************************************************************************

} // SqlToCsvExport
